Websocket - httpSession returns null

前端 未结 4 1236
小鲜肉
小鲜肉 2020-12-18 21:22

I would like to make the connection between a websocket handshake \\ session to a HttpSession object.

I\'ve used the following handshake modification:



        
相关标签:
4条回答
  • 2020-12-18 21:30

    Here is an impl for Pavel Bucek's Answer, after adding it, i got my session

    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpServletRequest;
    
    @WebListener
    public class RequestListener implements ServletRequestListener {
    
        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            ((HttpServletRequest) sre.getServletRequest()).getSession();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-18 21:32

    Building on @pavel-bucek 's answer, I wrote a simple HttpSessionInitializerFilter servlet filter.

    Just download the jar from the "Releases" page and save it anywhere in the classpath, then add the following snippet to your web.xml descriptor (modify the url-pattern as needed):

    <filter>
      <filter-name>HttpSessionInitializerFilter</filter-name>
      <filter-class>net.twentyonesolutions.servlet.filter.HttpSessionInitializerFilter</filter-class>
    </filter>
    
    <filter-mapping>
      <filter-name>HttpSessionInitializerFilter</filter-name>
      <url-pattern>/ws/*</url-pattern>
    </filter-mapping>
    
    0 讨论(0)
  • 2020-12-18 21:44

    first: create a new class

    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpServletRequest;
    
    @WebListener
    
    public class RequestListener implements ServletRequestListener {
    
        @Override
        public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
        }
    
        @Override
        public void requestInitialized(ServletRequestEvent servletRequestEvent) {
            ((HttpServletRequest)servletRequestEvent.getServletRequest()).getSession();
        }
    }
    

    and then add the "ServletComponentScan" annotation on the App main:

    @SpringBootApplication
    
    @ServletComponentScan
    
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 21:53

    This is intended behaviour, but I agree it might be confusing. From the HandshakeRequest.getHttpSession javadoc:

    /**
     * Return a reference to the HttpSession that the web socket handshake that 
     * started this conversation was part of, if the implementation
     * is part of a Java EE web container.
     *
     * @return the http session or {@code null} if either the websocket
     * implementation is not part of a Java EE web container, or there is
     * no HttpSession associated with the opening handshake request.
     */
    

    Problem is, that HttpSession was not yet created for your client connection and WebSocket API implementation just asks whether there is something created and if not, it does not create it. What you need to do is call httpServletRequest.getSession() sometime before WebSocket impl filter is invoked (doFilter(...) is called).

    This can be achieved for example by calling mentioned method in ServletRequestListener#requestInitalized or in different filter, etc..

    0 讨论(0)
提交回复
热议问题