Ruoyi Vue分离版 WebSocket后端鉴权认证解决方案

WebSocket 若依鉴权

使用WebSocket实现实时通信,Ruoyi官方教程做法是在SecurityConfig中将ws的地址设为匿名访问: ("/websocket/**").permitAll()

这种方式虽然能够让WebSocket成功连接,但我想在大多数生产环境中,无需鉴权的情况极为少见。而网络上缺乏明确的Ruoyi解决方案,于是动拙笔浅析下我个人的解决方案。

WebSocket Header携带Token,修改Ruoyi鉴权拦截器

前端vue,将Token放置于创建WebSocket连接请求的protocols参数中:

ini 复制代码
    this.ws = new WebSocket(wsuri,[getToken()]);

则其请求头的Sec-Websocket-Protocol参数会替换为Token,如下图所示:

JavaScript WebSocket无法自定义Header ,只有Sec-Websocket-Protocol内容能通过这种方式改变

在此基础上,我们需要让若依识别协议头Sec-Websocket-Protocol中的Token

在若依的JWT鉴权请求过滤器JwtAuthenticationTokenFilter.java中,其拦截逻辑是:

scss 复制代码
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException
{
    //从request请求中解析出LoginUser
    LoginUser loginUser = tokenService.getLoginUser(request);
    //使用这个LoginUser 完成JWT鉴权流程
    if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
    {
        tokenService.verifyToken(loginUser);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    }
    chain.doFilter(request, response);
}

进入这个getLoginUser函数,其内容如下:

ini 复制代码
/**
 * 获取用户身份信息
 * @return 用户信息
 */
public LoginUser getLoginUser(HttpServletRequest request)
{
    // **从request中获取携带的Token令牌**
    String token = getToken(request);
    if (StringUtils.isNotEmpty(token))
    {
        try
        {
            Claims claims = parseToken(token);
            // 解析对应的权限以及用户信息
            String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
            LoginUser user = CacheUtils.get(CacheConstants.LOGIN_TOKEN_KEY, uuid, LoginUser.class);
            return user;
        }
        catch (Exception e)
        {
        }
    }
    return null;
}

在第8行代码getToken中,进入即可看到获取Token的逻辑:

typescript 复制代码
/**
 * 获取请求token
 * @param request
 * @return token
 */
private String getToken(HttpServletRequest request)
{
    String token = request.getHeader(header);
    if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
    {
        token = token.replace(Constants.TOKEN_PREFIX, "");
    }
    return token;
}

其中的header变量是ruoyi的全局参数,内容会被装载为在application.yml中定义的Token协议头(默认是Authorization

TOKEN_PREFIX为全局常量,默认为"Bearer ",标识Token开始前的前缀,我们没有前缀因此可忽略

我们的修改逻辑只是在getHeader为空时再次getHeader我们自己的协议头Sec-Websocket-Protocol, 修改后内容为:

ini 复制代码
private String getToken(HttpServletRequest request)
{
    String token = request.getHeader(header);
    if (StringUtils.isEmpty(token)){
        //如果Authorization为空,那么尝试读取Sec-Websocket-Protocol的内容
        token = request.getHeader("Sec-Websocket-Protocol");
    }else if(token.startsWith(Constants.TOKEN_PREFIX)) {
        token = token.replace(Constants.TOKEN_PREFIX, "");
    }
    return token;
}

"Sec-Websocket-Protocol"硬编码即可,无需提取为全局参数,因为WebSocket协议头名称永远无法改变

于是ruoyi便能够从WebSocket连接请求中得到Token并成功鉴权,无需配置匿名访问了。

相关推荐
妙码生花10 小时前
全新的 TP8+Workerman+BuildAdmin 整合方案,已有近 2000 次下载使用。
websocket·php·thinkphp
爱学习的程序媛16 小时前
【Web前端】WebSocket 详解
前端·websocket·网络协议·web
Sheldon一蓑烟雨任平生18 小时前
WebSocket 聊天室项目(结合Socket.IO)
websocket·客户端·服务端·socket.io·聊天室
无籽西瓜a18 小时前
WebSocket详解含图解:协议特性、握手流程
网络·后端·websocket·网络协议·http
2501_9216494918 小时前
外汇实时汇率 API | 24 小时 架构设计与实战指南
大数据·python·websocket·金融·restful
honor_zhang2 天前
Spring Boot集成Websocket服务以及连接时需要注意的问题
spring boot·后端·websocket
小钻风33662 天前
在 Spring Boot 项目中使用 WebSocket 实现实时通信
websocket
Sean‘2 天前
Rancher 日志无法显示?WebSocket 代理配置是罪魁祸首
websocket·网络协议·rancher
曲幽3 天前
FastAPI实战:WebSocket vs Socket.IO,这回真给我整明白了!
python·websocket·nginx·socket·fastapi·web·async·socketio
Densen20143 天前
发布blazor应用到Linux, 使用nginx作为WebSocket代理
linux·websocket·nginx