refactor(websocket): 更新WebSocket消息处理并重构缓存键配置

This commit is contained in:
2026-01-16 19:32:21 +08:00
parent 96647a055b
commit 1053669077
7 changed files with 50 additions and 115 deletions
@@ -61,12 +61,12 @@ public class KitWsServer {
* @param message 客户端发送过来的消息 * @param message 客户端发送过来的消息
*/ */
@OnMessage @OnMessage
public void onMessage(String message, Session session) { public void onMessage(String message, Session session) throws IOException {
KitWsMessage wsMessage = JSON.parseObject(message, KitWsMessage.class); KitWsMessage wsMessage = JSON.parseObject(message, KitWsMessage.class);
String bizCode = wsMessage.getBizCode(); String bizCode = wsMessage.getBizCode();
Object data = wsMessage.getData(); Object data = wsMessage.getData();
log.info("收到来 sid={} 的信息: message={}, bizCode={}, data={}", sid, message, bizCode, log.info("收到来 sid={} 的信息: message={}, bizCode={}, data={}", sid, message, bizCode, JSON.toJSONString(data));
JSON.toJSONString(data)); session.getBasicRemote().sendText(message);
} }
/** /**
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.odboy.system.dal.redis; package cn.odboy.system.constant;
/** /**
* 关于缓存的Key集合 * 关于缓存的Key集合
@@ -28,4 +28,8 @@ public interface SystemCacheKey {
* 登录验证码 * 登录验证码
*/ */
String CAPTCHA_LOGIN = "captcha::login:"; String CAPTCHA_LOGIN = "captcha::login:";
/**
* 用户信息
*/
String USER_INFO = "user::info:";
} }
@@ -1,69 +0,0 @@
/*
* Copyright 2021-2026 Odboy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.system.dal.redis;
/**
* 关于缓存的Key集合
*/
public interface SystemRedisKey {
/**
* 用户
*/
String USER_ID = "user::id:";
/**
* 数据
*/
String DATA_USER = "data::user:";
/**
* 菜单
*/
String MENU_ID = "menu::id:";
String MENU_USER = "menu::user:";
/**
* 角色授权
*/
String ROLE_AUTH = "role::auth:";
String ROLE_USER = "role::user:";
/**
* 角色信息
*/
String ROLE_ID = "role::id:";
/**
* 部门
*/
String DEPT_ID = "dept::id:";
/**
* 岗位
*/
String JOB_ID = "job::id:";
/**
* 数据字典
*/
String DICT_NAME = "dict::name:";
/**
* 在线用户
*/
String ONLINE_USER = "online::token:";
/**
* 登录验证码
*/
String CAPTCHA_LOGIN = "captcha::login:";
/**
* 用户信息
*/
String USER_INFO = "user::info:";
}
@@ -18,6 +18,7 @@ package cn.odboy.system.dal.redis;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.odboy.framework.redis.KitRedisHelper; import cn.odboy.framework.redis.KitRedisHelper;
import cn.odboy.system.constant.SystemCacheKey;
import cn.odboy.system.dal.model.response.SystemUserJwtVo; import cn.odboy.system.dal.model.response.SystemUserJwtVo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@@ -42,7 +43,7 @@ public class SystemUserInfoDAO {
if (StrUtil.isNotEmpty(userName)) { if (StrUtil.isNotEmpty(userName)) {
// 添加数据, 避免数据同时过期(2小时左右) // 添加数据, 避免数据同时过期(2小时左右)
long time = 7200 + RandomUtil.randomInt(900, 1800); long time = 7200 + RandomUtil.randomInt(900, 1800);
redisHelper.set(SystemRedisKey.USER_INFO + userName, user, time); redisHelper.set(SystemCacheKey.USER_INFO + userName, user, time);
} }
} }
@@ -55,7 +56,7 @@ public class SystemUserInfoDAO {
public void deleteUserLoginInfoByUserName(String userName) { public void deleteUserLoginInfoByUserName(String userName) {
if (StrUtil.isNotEmpty(userName)) { if (StrUtil.isNotEmpty(userName)) {
// 清除数据 // 清除数据
redisHelper.del(SystemRedisKey.USER_INFO + userName); redisHelper.del(SystemCacheKey.USER_INFO + userName);
} }
} }
@@ -68,7 +69,7 @@ public class SystemUserInfoDAO {
public SystemUserJwtVo getUserLoginInfoByUserName(String username) { public SystemUserJwtVo getUserLoginInfoByUserName(String username) {
if (StrUtil.isNotEmpty(username)) { if (StrUtil.isNotEmpty(username)) {
// 获取数据 // 获取数据
return redisHelper.get(SystemRedisKey.USER_INFO + username, SystemUserJwtVo.class); return redisHelper.get(SystemCacheKey.USER_INFO + username, SystemUserJwtVo.class);
} }
return null; return null;
} }
@@ -19,6 +19,7 @@ import cn.hutool.core.util.StrUtil;
import cn.odboy.base.KitPageResult; import cn.odboy.base.KitPageResult;
import cn.odboy.framework.properties.AppProperties; import cn.odboy.framework.properties.AppProperties;
import cn.odboy.framework.redis.KitRedisHelper; import cn.odboy.framework.redis.KitRedisHelper;
import cn.odboy.system.constant.SystemCacheKey;
import cn.odboy.system.dal.model.export.SystemUserOnlineExportRowVo; import cn.odboy.system.dal.model.export.SystemUserOnlineExportRowVo;
import cn.odboy.system.dal.model.response.SystemUserJwtVo; import cn.odboy.system.dal.model.response.SystemUserJwtVo;
import cn.odboy.system.dal.model.response.SystemUserOnlineVo; import cn.odboy.system.dal.model.response.SystemUserOnlineVo;
@@ -122,7 +123,7 @@ public class SystemUserOnlineInfoDAO {
* @param username / * @param username /
*/ */
public void kickOutByUsername(String username) { public void kickOutByUsername(String username) {
String loginKey = SystemRedisKey.ONLINE_USER + username + "*"; String loginKey = SystemCacheKey.ONLINE_USER + username + "*";
redisHelper.scanDel(loginKey); redisHelper.scanDel(loginKey);
} }
@@ -133,15 +134,13 @@ public class SystemUserOnlineInfoDAO {
* @param pageable / * @param pageable /
* @return / * @return /
*/ */
public KitPageResult<SystemUserOnlineVo> searchOnlineUser(SystemUserOnlineVo onlineVo, public KitPageResult<SystemUserOnlineVo> searchOnlineUser(SystemUserOnlineVo onlineVo, IPage<SystemUserOnlineVo> pageable) {
IPage<SystemUserOnlineVo> pageable) {
String username = null; String username = null;
if (onlineVo != null) { if (onlineVo != null) {
username = onlineVo.getUserName(); username = onlineVo.getUserName();
} }
List<SystemUserOnlineVo> onlineUserList = queryUserOnlineModelListByUsername(username); List<SystemUserOnlineVo> onlineUserList = this.queryUserOnlineModelListByUsername(username);
List<SystemUserOnlineVo> paging = List<SystemUserOnlineVo> paging = KitPageUtil.softPaging(pageable.getCurrent(), pageable.getSize(), onlineUserList);
KitPageUtil.softPaging(pageable.getCurrent(), pageable.getSize(), onlineUserList);
return KitPageUtil.toPage(paging, onlineUserList.size()); return KitPageUtil.toPage(paging, onlineUserList.size());
} }
@@ -152,7 +151,7 @@ public class SystemUserOnlineInfoDAO {
* @return / * @return /
*/ */
public List<SystemUserOnlineVo> queryUserOnlineModelListByUsername(String username) { public List<SystemUserOnlineVo> queryUserOnlineModelListByUsername(String username) {
String loginKey = SystemRedisKey.ONLINE_USER + (StrUtil.isBlank(username) ? "" : "*" + username); String loginKey = SystemCacheKey.ONLINE_USER + (StrUtil.isBlank(username) ? "" : "*" + username);
List<String> keys = redisHelper.scan(loginKey + "*"); List<String> keys = redisHelper.scan(loginKey + "*");
Collections.reverse(keys); Collections.reverse(keys);
List<SystemUserOnlineVo> onlineUserList = new ArrayList<>(); List<SystemUserOnlineVo> onlineUserList = new ArrayList<>();
@@ -22,7 +22,7 @@ import cn.odboy.constant.SystemConst;
import cn.odboy.framework.properties.AppProperties; import cn.odboy.framework.properties.AppProperties;
import cn.odboy.framework.redis.KitRedisHelper; import cn.odboy.framework.redis.KitRedisHelper;
import cn.odboy.system.dal.model.response.SystemUserJwtVo; import cn.odboy.system.dal.model.response.SystemUserJwtVo;
import cn.odboy.system.dal.redis.SystemCacheKey; import cn.odboy.system.constant.SystemCacheKey;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser; import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
@@ -23,7 +23,7 @@ import cn.odboy.framework.exception.BadRequestException;
import cn.odboy.framework.properties.AppProperties; import cn.odboy.framework.properties.AppProperties;
import cn.odboy.framework.redis.KitRedisHelper; import cn.odboy.framework.redis.KitRedisHelper;
import cn.odboy.system.dal.model.response.SystemCaptchaVo; import cn.odboy.system.dal.model.response.SystemCaptchaVo;
import cn.odboy.system.dal.redis.SystemCacheKey; import cn.odboy.system.constant.SystemCacheKey;
import com.wf.captcha.base.Captcha; import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;