--author:scott--date:20261013--for:【安全 GHSA-5j5w-pm5g-5wr8】websocket绑定token身份禁止订阅他人消息;单人推送精确匹配;socketTest补admin角色

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
zhangdaiscott 2026-10-13 11:41:17 -04:00
parent 2b8b3ae1e5
commit 997404e615
3 changed files with 21 additions and 2 deletions

View File

@ -3,6 +3,8 @@ package org.jeecg.config.filter;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
@ -46,6 +48,20 @@ public class WebsocketFilter implements Filter {
log.debug("Websocket连接 Token安全校验失败,IP:{}, Token:{}, Path = {},异常:{}", oConvertUtils.getIpAddrByRequest(request), token, request.getRequestURI(), exception.getMessage());
return;
}
//update-begin---author:scott ---date:20261013 for:【安全 GHSA-5j5w-pm5g-5wr8】系统通知websocket(/websocket/{userId})绑定token身份,禁止订阅他人的实时消息-----------
String uri = request.getRequestURI();
int idx = uri.indexOf("/websocket/");
if (idx >= 0) {
String rest = uri.substring(idx + "/websocket/".length());
int slash = rest.indexOf('/');
String pathUserId = slash >= 0 ? rest.substring(0, slash) : rest;
LoginUser loginUser = commonApi.getUserByName(JwtUtil.getUsername(token));
if (loginUser == null || !loginUser.getId().equals(pathUserId)) {
log.warn("Websocket连接 用户身份与路径userId不一致,已拒绝。Path = {}", uri);
return;
}
}
//update-end---author:scott ---date:20261013 for:【安全 GHSA-5j5w-pm5g-5wr8】系统通知websocket(/websocket/{userId})绑定token身份,禁止订阅他人的实时消息-----------
HttpServletResponse response = (HttpServletResponse)servletResponse;
response.setHeader(TOKEN_KEY, token);
filterChain.doFilter(servletRequest, servletResponse);

View File

@ -1,5 +1,6 @@
package org.jeecg.modules.message.controller;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.WebsocketConst;
import org.jeecg.modules.message.websocket.WebSocket;
@ -16,6 +17,7 @@ import com.alibaba.fastjson.JSONObject;
* @author: jeecg-boot
*/
@RestController
@RequiresRoles("admin")
@RequestMapping("/sys/socketTest")
public class TestSocketController {

View File

@ -71,8 +71,9 @@ public class WebSocket {
public void pushMessage(String userId, String message) {
for (Map.Entry<String, Session> item : sessionPool.entrySet()) {
//userId key值= {用户id + "_"+ 登录token的md5串}
//TODO vue2未改key新规则,暂时不影响逻辑
if (item.getKey().contains(userId)) {
//update-begin---author:scott ---date:20261013 for:【安全 GHSA-5j5w-pm5g-5wr8】由contains子串匹配改为精确匹配,防止userId互为子串时错发给他人(如"1"命中"11")-----------
if (item.getKey().equals(userId) || item.getKey().startsWith(userId + "_")) {
//update-end---author:scott ---date:20261013 for:【安全 GHSA-5j5w-pm5g-5wr8】由contains子串匹配改为精确匹配,防止userId互为子串时错发给他人(如"1"命中"11")-----------
Session session = item.getValue();
try {
// 代码逻辑说明: websocket报错 https://gitee.com/jeecg/jeecg-boot/issues/I4C0MU