• SpringBoot在WebSocket长连接中获取到HttpSession


    SpringBoot在WebSocket长连接中获取到HttpSession

    Websocket是通过http协议握手后升级成为长连接,在握手的时候,可以读取到客户端http请求的所有信息,自然也包括 HttpSession

    自定义配置类,继承 Configurator ,覆写modifyHandshake方法

    ServerEndpointConfigImpl.Configurator,提供了一个modifyHandshake方法,可以在完成握手后,读取客户端的请求,以及修改对客户端的响应。HandshakeRequest 类已经提供了获取HttpSession的接口。

    import javax.servlet.http.HttpSession;
    import javax.websocket.HandshakeResponse;
    import javax.websocket.server.HandshakeRequest;
    import javax.websocket.server.ServerEndpointConfig;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import io.undertow.websockets.jsr.ServerEndpointConfigImpl;
    
    public class ServerEndpointConfigurator extends ServerEndpointConfigImpl.Configurator {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(ServerEndpointConfigurator.class);
        
        @Override
        public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
            // 尝试获取到当前Websocket链接的HttpSession
            HttpSession httpSession = (HttpSession)request.getHttpSession();
            
            LOGGER.info("session={}", httpSession);
            if (httpSession != null) {
                // session id
                LOGGER.info("sessionId={}", httpSession.getId());
                // 读取session域中存储的数据
                LOGGER.info("name={}", httpSession.getAttribute("name"));
            }
            
            super.modifyHandshake(sec, request, response);
        }
    }
    

    在@ServerEndpoint指定Configurator实现类

    通过 @ServerEndpoint 注解的 configurator 指定Configurator实现类

    import java.io.IOException;
    
    import javax.websocket.CloseReason;
    import javax.websocket.EndpointConfig;
    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.Session;
    import javax.websocket.server.ServerEndpoint;
    
    import io.springboot.twitter.websocket.ServerEndpointConfigurator;
    
    @ServerEndpoint(value = "/channel/test", 
        configurator = ServerEndpointConfigurator.class  // 指定服务端的配置类
    )
    public class TestChannel {
        @OnMessage
        public void onMessage(String message) throws IOException {
        }
        @OnOpen
        public void onOpen(Session session, EndpointConfig endpointConfig) throws IOException {
        }
        @OnClose
        public void onClose(CloseReason closeReason) {
        }
        @OnError
        public void onError(Throwable throwable) throws IOException {
        }
    }
    
    

    测试

    通过Controller渲染视图,并且创建Session

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.ModelAndView;
    
    @RestController
    @RequestMapping("/test")
    public class TestController {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
        
        @GetMapping
        public Object test (HttpServletRequest request) {
            HttpSession httpSesion = request.getSession();
            
            LOGGER.info("创建新的httpSession={}", httpSesion.getId());
            
            // 存储数据到Session域
            httpSesion.setAttribute("name", "SpringBoot中文社区");
            return new ModelAndView("test/test");
        }
    }
    
    

    视图(HTML)中加载的JS客户端

    const {protocol, host} = window.location;
    const websocket = new WebSocket(`${protocol === 'https:' ? 'wss:': 'ws:'}//${host}/channel/test`);
    websocket.onmessage = e => {
        const message = JSON.parse(e.data);
        console.log('收到消息:', message);
    }
    websocket.onclose = e => {
        let {code, reason} = e;
        console.log(`链接断开:code=${code}, reason=${reason}`);
    }
    websocket.onopen = () => {
        console.log(`链接建立...`);
    }
    websocket.onerror = e => {
        console.log('链接异常:', e);
    }
    

    控制台日志

    可以从日志中看到,在Controller创建了HttpSession,并且在Websocket链接握手中获取到了HttpSession,并且成功读取到了存储的数据。

    2020-07-03 14:09:20.322 DEBUG 2004 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : GET "/test", parameters={}
    2020-07-03 14:09:20.327 DEBUG 2004 --- [  XNIO-1 task-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to io.springboot.twitter.web.controller.TestController#test(HttpServletRequest)
    2020-07-03 14:09:20.348 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.session                      : Created session with id I2d7ub_oKNcOB7QSjl1xQ59_uBkgjvWhiTX9BmXk for exchange HttpServerExchange{ GET /test}
    2020-07-03 14:09:20.349  INFO 2004 --- [  XNIO-1 task-1] i.s.t.web.controller.TestController      : 创建新的httpSession=I2d7ub_oKNcOB7QSjl1xQ59_uBkgjvWhiTX9BmXk
    2020-07-03 14:09:20.431 DEBUG 2004 --- [  XNIO-1 task-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
    2020-07-03 14:09:20.432 DEBUG 2004 --- [  XNIO-1 task-1] o.s.w.s.view.freemarker.FreeMarkerView   : View name 'test/test', model {}
    2020-07-03 14:09:20.433 DEBUG 2004 --- [  XNIO-1 task-1] o.s.w.s.view.freemarker.FreeMarkerView   : Rendering [test/test.ftl]
    2020-07-03 14:09:20.470 DEBUG 2004 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
    2020-07-03 14:09:20.661 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request.security             : Attempting to authenticate /static/js/test.js, authentication required: false
    2020-07-03 14:09:20.661 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request.security             : Authentication outcome was NOT_ATTEMPTED with method io.undertow.security.impl.CachedAuthenticatedSessionMechanism@51c2d817 for /static/js/test.js
    2020-07-03 14:09:20.661 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request.security             : Authentication result was ATTEMPTED for /static/js/test.js
    2020-07-03 14:09:20.662 DEBUG 2004 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : GET "/static/js/test.js", parameters={}
    2020-07-03 14:09:20.675 DEBUG 2004 --- [  XNIO-1 task-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/static/", "/"]
    2020-07-03 14:09:20.694 DEBUG 2004 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
    2020-07-03 14:09:20.701 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request.security             : Attempting to authenticate /channel/test, authentication required: false
    2020-07-03 14:09:20.701 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request.security             : Authentication outcome was NOT_ATTEMPTED with method io.undertow.security.impl.CachedAuthenticatedSessionMechanism@51c2d817 for /channel/test
    2020-07-03 14:09:20.701 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request.security             : Authentication result was ATTEMPTED for /channel/test
    2020-07-03 14:09:20.707 DEBUG 2004 --- [  XNIO-1 task-1] io.undertow.request                      : Upgrading request HttpServerExchange{ GET /channel/test}
    2020-07-03 14:09:20.712  INFO 2004 --- [  XNIO-1 task-1] i.s.t.w.ServerEndpointConfigurator       : session=io.undertow.servlet.spec.HttpSessionImpl@570c2102
    2020-07-03 14:09:20.712  INFO 2004 --- [  XNIO-1 task-1] i.s.t.w.ServerEndpointConfigurator       : sessionId=I2d7ub_oKNcOB7QSjl1xQ59_uBkgjvWhiTX9BmXk
    2020-07-03 14:09:20.712  INFO 2004 --- [  XNIO-1 task-1] i.s.t.w.ServerEndpointConfigurator       : name=SpringBoot中文社区
    2020-07-03 14:10:20.744 DEBUG 2004 --- [   XNIO-1 I/O-1] io.undertow.request                      : Timing out idle connection from /0:0:0:0:0:0:0:1:14058
    

    本文来自: https://springboot.io/t/topic/2158

  • 相关阅读:
    JS使用Enter事件将输入的字符倒叙输出
    博客园 页面定制CSS代码
    C#将DataTable数据导出CSV文件
    js时间格式转换
    将十进制数据转换为二进制AND将二进制数据转换为十进制!
    ref和out的使用与区别
    形参和实参的区别
    为啥Underlay才是容器网络的最佳落地选择
    你问我答:容器篇(1)
    切忌一步到位,谈谈DevOps实施落地
  • 原文地址:https://www.cnblogs.com/kevinblandy/p/13230293.html
Copyright © 2020-2023  润新知