Springboot中websocket获取httpsession null 的情况 httpsession获取不到问题 前后端分离问题 添加配置依然为null

在校验令牌的时候把用户id存入到session中:

            // 2.获取session
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("userId", userId);

此时的sessionid为:

然后这是我的websocket配置类:

@Slf4j
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 获取HttpSession对象
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        log.info("websocket httpsessionId" + httpSession.getId());
        if (httpSession != null) {
            log.info("httpsession" + httpSession);
            sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
        } else {
            log.info("httpsession是空");
        }
    }
}

此时是获取不到session的:

然后通过上网一些列资料添加了如下配置:

@Component
public class RequestListener implements ServletRequestListener {
    public void requestInitialized(ServletRequestEvent sre)  {
        //将所有request请求都携带上httpSession
        ((HttpServletRequest) sre.getServletRequest()).getSession();
    }
}

因为websocket没有httpsession是不会默认创建的,所以在这里对每次请求之前都手动获取一个session,但是问题来了此时的sessionid:

虽然拿到了session 但不是同一个session数据里面的数据自然也是null,干脆采取存入redis,有好的方法欢迎大家分享交流一下。

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在使用WebSocket获取用户信息时,由于WebSocket协议与HTTP协议不同,直接获取HttpSession是无法实现的。然而,我们可以通过添加一个自定义的配置类来解决这个问题。通过扩展`javax.websocket.server.ServerEndpointConfig.Configurator`类,并重写`modifyHandshake`方法,在其获取HttpSession并将其添加到`getUserProperties`,以便在WebSocket使用。 具体实现的代码如下所示: ```java import javax.servlet.http.HttpSession; import javax.websocket.HandshakeResponse; import javax.websocket.server.HandshakeRequest; import javax.websocket.server.ServerEndpointConfig; import javax.websocket.server.ServerEndpointConfig.Configurator; public class HttpSessionConfigurator extends Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession) request.getHttpSession(); sec.getUserProperties().put(HttpSession.class.getName(), httpSession); } } ``` 通过使用这个自定义的配置类,我们就可以在WebSocket获取到用户的HttpSession信息了。这样,我们就能够根据用户的权限来接收不同的消息了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [java在websocket获取HttpSession,来得到用户信息(转)](https://blog.csdn.net/w_fengheling/article/details/88538944)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值