From 851f434e1e3f8d8b8115a00276a76a39500d3518 Mon Sep 17 00:00:00 2001 From: liyd-b Date: Tue, 27 Jun 2023 16:44:42 +0800 Subject: [PATCH 01/13] =?UTF-8?q?fix:=20=E5=8E=BB=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=A0=81=EF=BC=88sout=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E7=83=A6=E8=AF=B7=E4=BD=9C=E8=80=85=E9=80=9A=E8=BF=87pr?= =?UTF-8?q?=E5=93=88=EF=BC=8C=E8=AF=81=E6=98=8E=E6=88=91=E4=B9=9F=E6=98=AF?= =?UTF-8?q?=E4=B8=80=E5=90=8Dcontributor=20=20=E5=93=88=E5=93=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/abin/mallchat/custom/chat/controller/ChatController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java index ac0154e..61e862f 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java @@ -124,7 +124,6 @@ public class ChatController { private void filterBlackMsg(CursorPageBaseResp memberPage) { Set blackMembers = getBlackUidSet(); memberPage.getList().removeIf(a -> blackMembers.contains(a.getFromUser().getUid().toString())); - System.out.println(1); } @PostMapping("/msg") From e4051cb970180795ff67c46223eb0eeff83f68a5 Mon Sep 17 00:00:00 2001 From: zhaoyuhang <1045078399@qq.com> Date: Sat, 1 Jul 2023 17:43:42 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E4=BC=98=E5=8C=96chatAI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mallchat-common/src/main/resources/application.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index 2df7f5b..5fdfbdd 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -38,7 +38,7 @@ spring: # 连接超时时间 timeout: 1800000 # # 设置密码 -# password: ${mallchat.redis.password} + password: ${mallchat.redis.password} lettuce: pool: # 最大阻塞等待时间,负数表示没有限制 From 6dd7b21b39f92d54a4fa4cc1ab522d5b28442788 Mon Sep 17 00:00:00 2001 From: zhaoyuhang <1045078399@qq.com> Date: Sun, 2 Jul 2023 23:34:03 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mallchat-common/src/main/resources/application.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index 6f13a61..270d74f 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -38,7 +38,7 @@ spring: # 连接超时时间 timeout: 1800000 # # 设置密码 -# password: ${mallchat.redis.password} + password: ${mallchat.redis.password} lettuce: pool: # 最大阻塞等待时间,负数表示没有限制 @@ -67,9 +67,9 @@ chatai: chatgpt: use: true AIUserId: 10452 - key: xxxxx + key: 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 chatglm2: use: true - url: xxxxx + url: http://vastmiao.natapp1.cc minute: 3 # 每个用户每3分钟可以请求一次 - AIUserId: 10451 + From d8e2168b40f37e6d4f08559227dd7698b97c6bea Mon Sep 17 00:00:00 2001 From: zhaoyuhang <1045078399@qq.com> Date: Sun, 2 Jul 2023 23:43:03 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/application-pro.properties | 24 ++++++++++++------- .../resources/application-test.properties | 24 ++++++++++++------- .../src/main/resources/application.yml | 17 ++++++------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/mallchat-common/src/main/resources/application-pro.properties b/mallchat-common/src/main/resources/application-pro.properties index 3b71009..e847f8c 100644 --- a/mallchat-common/src/main/resources/application-pro.properties +++ b/mallchat-common/src/main/resources/application-pro.properties @@ -1,28 +1,36 @@ -#todo 记得把这些配置信息补充了 -##################mysql配置################## +#todo ???????????? +##################mysql??################## mallchat.mysql.ip=127.0.0.1 mallchat.mysql.port=3306 mallchat.mysql.db=mallchat mallchat.mysql.username=root mallchat.mysql.password=123456 -##################redis配置################## +##################redis??################## mallchat.redis.host=127.0.0.1 mallchat.redis.port=6379 mallchat.redis.password=123456 ##################jwt################## mallchat.jwt.secret=dsfsdfsdfsdfsd -##################微信公众号信息################## +##################???????################## mallchat.wx.callback=http://127.0.0.1:8080 mallchat.wx.appId=appid mallchat.wx.secret=380bfc1c9147fdsf4sf07 -# 接口配置里的Token值 +# ??????Token? mallchat.wx.token=sdfsf -# 接口配置里的EncodingAESKey值 +# ??????EncodingAESKey? mallchat.wx.aesKey=sha1 -##################OSS配置################## +##################OSS??################## oss.enabled=true oss.type=minio oss.endpoint=http://localhost:9000 oss.access-key=BEZ213 oss.secret-key=Ii4vCMIXuFfds1EZ8e7RXI2342342kV -oss.bucketName=default \ No newline at end of file +oss.bucketName=default +##################gpt??################## +mallchat.chatgpt.use=false +mallchat.chatgpt.uid=10001 +mallchat.chatgpt.key=sk-wvWM0xGcxFfsddfsgxixbXK5tHovM +mallchat.chatgpt.proxyUrl=https://123.cc +mallchat.chatglm2.use=false +mallchat.chatglm2.url=http://v32134.cc +mallchat.chatglm2.uid=10002 \ No newline at end of file diff --git a/mallchat-common/src/main/resources/application-test.properties b/mallchat-common/src/main/resources/application-test.properties index 45e7793..fb572d2 100644 --- a/mallchat-common/src/main/resources/application-test.properties +++ b/mallchat-common/src/main/resources/application-test.properties @@ -1,28 +1,36 @@ -#todo 记得把这些配置信息补充了 -##################mysql配置################## +#todo ???????????? +##################mysql??################## mallchat.mysql.ip=127.0.0.1 mallchat.mysql.port=3306 mallchat.mysql.db=mallchat mallchat.mysql.username=root mallchat.mysql.password=123456 -##################redis配置################## +##################redis??################## mallchat.redis.host=127.0.0.1 mallchat.redis.port=6379 mallchat.redis.password=123456 ##################jwt################## mallchat.jwt.secret=dsfsdfsdfsdfsd -##################微信公众号信息################## +##################???????################## mallchat.wx.callback=http://127.0.0.1:8080 mallchat.wx.appId=appid mallchat.wx.secret=380bfc1c9147fdsf4sf07 -# 接口配置里的Token值 +# ??????Token? mallchat.wx.token=sdfsf -# 接口配置里的EncodingAESKey值 +# ??????EncodingAESKey? mallchat.wx.aesKey=sha1 -##################OSS配置################## +##################OSS??################## oss.enabled=true oss.type=minio oss.endpoint=http://localhost:9000 oss.access-key=BEZ213 oss.secret-key=Ii4vCMIXuFe241dsfEZ8e7RXI2342342kV -oss.bucketName=default \ No newline at end of file +oss.bucketName=default +##################gpt??################## +mallchat.chatgpt.use=false +mallchat.chatgpt.uid=10001 +mallchat.chatgpt.key=sk-wvWM0xGcxFfsddfsgxixbXK5tHovM +mallchat.chatgpt.proxyUrl=https://123.cc +mallchat.chatglm2.use=false +mallchat.chatglm2.url=http://v32134.cc +mallchat.chatglm2.uid=10002 \ No newline at end of file diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index 270d74f..cb9bc48 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -12,7 +12,7 @@ mybatis-plus: spring: profiles: #运行的环境 - active: test + active: my-prod application: name: mallchat datasource: @@ -37,7 +37,7 @@ spring: database: 0 # 连接超时时间 timeout: 1800000 -# # 设置密码 + # 设置密码 password: ${mallchat.redis.password} lettuce: pool: @@ -65,11 +65,12 @@ wx: aesKey: ${mallchat.wx.aesKey} # 接口配置里的EncodingAESKey值 chatai: chatgpt: - use: true - AIUserId: 10452 - key: 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 + use: ${mallchat.chatgpt.use} + AIUserId: ${mallchat.chatgpt.uid} + key: ${mallchat.chatgpt.key} + proxyUrl: ${mallchat.chatgpt.proxyUrl} chatglm2: - use: true - url: http://vastmiao.natapp1.cc + use: ${mallchat.chatglm2.use} + url: ${mallchat.chatglm2.url} minute: 3 # 每个用户每3分钟可以请求一次 - + AIUserId: ${mallchat.chatglm2.uid} \ No newline at end of file From dedf13f7271a231f2682a91716a8451441902b5e Mon Sep 17 00:00:00 2001 From: zhaoyuhang <1045078399@qq.com> Date: Sun, 2 Jul 2023 23:45:17 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/application-pro.properties | 16 ++++++++-------- .../main/resources/application-test.properties | 16 ++++++++-------- .../src/main/resources/application.yml | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/mallchat-common/src/main/resources/application-pro.properties b/mallchat-common/src/main/resources/application-pro.properties index e847f8c..c6a31b5 100644 --- a/mallchat-common/src/main/resources/application-pro.properties +++ b/mallchat-common/src/main/resources/application-pro.properties @@ -1,32 +1,32 @@ -#todo ???????????? -##################mysql??################## +#todo 记得把这些配置信息补充了 +##################mysql配置################## mallchat.mysql.ip=127.0.0.1 mallchat.mysql.port=3306 mallchat.mysql.db=mallchat mallchat.mysql.username=root mallchat.mysql.password=123456 -##################redis??################## +##################redis配置################## mallchat.redis.host=127.0.0.1 mallchat.redis.port=6379 mallchat.redis.password=123456 ##################jwt################## mallchat.jwt.secret=dsfsdfsdfsdfsd -##################???????################## +##################微信公众号信息################## mallchat.wx.callback=http://127.0.0.1:8080 mallchat.wx.appId=appid mallchat.wx.secret=380bfc1c9147fdsf4sf07 -# ??????Token? +# 接口配置里的Token值 mallchat.wx.token=sdfsf -# ??????EncodingAESKey? +# 接口配置里的EncodingAESKey值 mallchat.wx.aesKey=sha1 -##################OSS??################## +##################OSS配置################## oss.enabled=true oss.type=minio oss.endpoint=http://localhost:9000 oss.access-key=BEZ213 oss.secret-key=Ii4vCMIXuFfds1EZ8e7RXI2342342kV oss.bucketName=default -##################gpt??################## +##################gpt配置################## mallchat.chatgpt.use=false mallchat.chatgpt.uid=10001 mallchat.chatgpt.key=sk-wvWM0xGcxFfsddfsgxixbXK5tHovM diff --git a/mallchat-common/src/main/resources/application-test.properties b/mallchat-common/src/main/resources/application-test.properties index fb572d2..a4967ae 100644 --- a/mallchat-common/src/main/resources/application-test.properties +++ b/mallchat-common/src/main/resources/application-test.properties @@ -1,32 +1,32 @@ -#todo ???????????? -##################mysql??################## +#todo 记得把这些配置信息补充了 +##################mysql配置################## mallchat.mysql.ip=127.0.0.1 mallchat.mysql.port=3306 mallchat.mysql.db=mallchat mallchat.mysql.username=root mallchat.mysql.password=123456 -##################redis??################## +##################redis配置################## mallchat.redis.host=127.0.0.1 mallchat.redis.port=6379 mallchat.redis.password=123456 ##################jwt################## mallchat.jwt.secret=dsfsdfsdfsdfsd -##################???????################## +##################微信公众号信息################## mallchat.wx.callback=http://127.0.0.1:8080 mallchat.wx.appId=appid mallchat.wx.secret=380bfc1c9147fdsf4sf07 -# ??????Token? +# 接口配置里的Token值 mallchat.wx.token=sdfsf -# ??????EncodingAESKey? +# 接口配置里的EncodingAESKey值 mallchat.wx.aesKey=sha1 -##################OSS??################## +##################OSS配置################## oss.enabled=true oss.type=minio oss.endpoint=http://localhost:9000 oss.access-key=BEZ213 oss.secret-key=Ii4vCMIXuFe241dsfEZ8e7RXI2342342kV oss.bucketName=default -##################gpt??################## +##################gpt配置################## mallchat.chatgpt.use=false mallchat.chatgpt.uid=10001 mallchat.chatgpt.key=sk-wvWM0xGcxFfsddfsgxixbXK5tHovM diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index cb9bc48..2f2ab12 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -73,4 +73,4 @@ chatai: use: ${mallchat.chatglm2.use} url: ${mallchat.chatglm2.url} minute: 3 # 每个用户每3分钟可以请求一次 - AIUserId: ${mallchat.chatglm2.uid} \ No newline at end of file + AIUserId: ${mallchat.chatglm2.uid} From ec76c2e337309d79e5bd0aafc7c5acc3aebdee9d Mon Sep 17 00:00:00 2001 From: zhaoyuhang <1045078399@qq.com> Date: Mon, 3 Jul 2023 11:31:11 +0800 Subject: [PATCH 06/13] =?UTF-8?q?fix:ChatAI=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatai/handler/AbstractChatAIHandler.java | 38 +++++++---- .../chatai/handler/ChatAIHandlerFactory.java | 16 +---- .../chatai/handler/ChatGLM2Handler.java | 42 ++++++++---- .../chatai/handler/GPTChatAIHandler.java | 65 ++++++++++++------- .../chatai/properties/ChatGLM2Properties.java | 5 -- .../chatai/properties/ChatGPTProperties.java | 10 +-- 6 files changed, 102 insertions(+), 74 deletions(-) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/AbstractChatAIHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/AbstractChatAIHandler.java index e644b4d..598e621 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/AbstractChatAIHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/AbstractChatAIHandler.java @@ -29,13 +29,21 @@ public abstract class AbstractChatAIHandler { protected UserService userService; @PostConstruct - private void init() { - ChatAIHandlerFactory.register(getChatAIUserId(), getChatAIName(), this); + protected void init() { + if (isUse()) { + ChatAIHandlerFactory.register(getChatAIUserId(), this); + } } + + /** + * 是否启用 + * + * @return boolean + */ + protected abstract boolean isUse(); + // 获取机器人id public abstract Long getChatAIUserId(); - // 获取机器人名称 - public abstract String getChatAIName(); public void chat(Message message) { if (!supports(message)) { @@ -44,7 +52,7 @@ public abstract class AbstractChatAIHandler { threadPoolTaskExecutor.execute(() -> { String text = doChat(message); if (StringUtils.isNotBlank(text)) { - answerMsg(text, message.getRoomId(), message.getFromUid()); + answerMsg(text, message); } }); } @@ -66,30 +74,34 @@ public abstract class AbstractChatAIHandler { protected abstract String doChat(Message message); - protected void answerMsg(String text, Long roomId, Long uid) { - UserInfoResp userInfo = userService.getUserInfo(uid); + protected void answerMsg(String text, Message replyMessage) { + UserInfoResp userInfo = userService.getUserInfo(replyMessage.getFromUid()); text = "@" + userInfo.getName() + " " + text; - if (text.length() < 450) { - save(text, roomId, uid); - }else { - int maxLen = 450; + if (text.length() < 800) { + save(text, replyMessage); + } else { + int maxLen = 800; int len = text.length(); int count = (len + maxLen - 1) / maxLen; for (int i = 0; i < count; i++) { int start = i * maxLen; int end = Math.min(start + maxLen, len); - save(text.substring(start, end), roomId, uid); + save(text.substring(start, end), replyMessage); } } } - private void save(String text, Long roomId, Long uid) { + private void save(String text, Message replyMessage) { + Long roomId = replyMessage.getRoomId(); + Long uid = replyMessage.getFromUid(); + Long id = replyMessage.getId(); ChatMessageReq answerReq = new ChatMessageReq(); answerReq.setRoomId(roomId); answerReq.setMsgType(MessageTypeEnum.TEXT.getType()); TextMsgReq textMsgReq = new TextMsgReq(); textMsgReq.setContent(text); + textMsgReq.setReplyMsgId(replyMessage.getId()); textMsgReq.setAtUidList(Collections.singletonList(uid)); answerReq.setBody(textMsgReq); chatService.sendMsg(answerReq, getChatAIUserId()); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatAIHandlerFactory.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatAIHandlerFactory.java index 8d6356d..a719ecb 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatAIHandlerFactory.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatAIHandlerFactory.java @@ -1,7 +1,6 @@ package com.abin.mallchat.custom.chatai.handler; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.baomidou.mybatisplus.core.toolkit.StringUtils; import java.util.List; import java.util.Map; @@ -9,11 +8,9 @@ import java.util.concurrent.ConcurrentHashMap; public class ChatAIHandlerFactory { private static final Map CHATAI_ID_MAP = new ConcurrentHashMap<>(); - private static final Map CHATAI_NAME_MAP = new ConcurrentHashMap<>(); - public static void register(Long aIUserId, String name, AbstractChatAIHandler chatAIHandler) { + public static void register(Long aIUserId, AbstractChatAIHandler chatAIHandler) { CHATAI_ID_MAP.put(aIUserId, chatAIHandler); - CHATAI_NAME_MAP.put(name, chatAIHandler); } public static AbstractChatAIHandler getChatAIHandlerById(List userIds) { @@ -28,15 +25,4 @@ public class ChatAIHandlerFactory { } return null; } - public static AbstractChatAIHandler getChatAIHandlerByName(String text) { - if (StringUtils.isBlank(text)) { - return null; - } - for (Map.Entry entry : CHATAI_NAME_MAP.entrySet()) { - if (text.contains("@"+entry.getKey())) { - return entry.getValue(); - } - } - return null; - } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java index 11350ca..0f7ff1b 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/ChatGLM2Handler.java @@ -7,6 +7,7 @@ import com.abin.mallchat.common.common.constant.RedisKey; import com.abin.mallchat.common.common.utils.RedisUtils; import com.abin.mallchat.custom.chatai.properties.ChatGLM2Properties; import com.abin.mallchat.custom.chatai.utils.ChatGLM2Utils; +import com.abin.mallchat.custom.user.domain.vo.response.user.UserInfoResp; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -36,27 +37,42 @@ public class ChatGLM2Handler extends AbstractChatAIHandler { private static final Random RANDOM = new Random(); + private static String AI_NAME; + @Autowired private ChatGLM2Properties glm2Properties; + @Override + protected void init() { + super.init(); + if (isUse()) { + UserInfoResp userInfo = userService.getUserInfo(glm2Properties.getAIUserId()); + if (userInfo == null) { + log.error("根据AIUserId:{} 找不到用户信息", glm2Properties.getAIUserId()); + throw new RuntimeException("根据AIUserId找不到用户信息"); + } + if (StringUtils.isBlank(userInfo.getName())) { + log.warn("根据AIUserId:{} 找到的用户信息没有name", glm2Properties.getAIUserId()); + throw new RuntimeException("根据AIUserId: " + glm2Properties.getAIUserId() + " 找到的用户没有名字"); + } + AI_NAME = userInfo.getName(); + } + } + + @Override + protected boolean isUse() { + return glm2Properties.isUse(); + } + @Override public Long getChatAIUserId() { return glm2Properties.getAIUserId(); } - @Override - public String getChatAIName() { - if (StringUtils.isNotBlank(glm2Properties.getAIUserName())) { - return glm2Properties.getAIUserName(); - } - String name = userService.getUserInfo(glm2Properties.getAIUserId()).getName(); - glm2Properties.setAIUserName(name); - return name; - } @Override protected String doChat(Message message) { - String content = message.getContent().replace("@" +glm2Properties.getAIUserName(), "").trim(); + String content = message.getContent().replace("@" + AI_NAME, "").trim(); Long uid = message.getFromUid(); Long minute; String text; @@ -73,7 +89,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler { .send(); text = ChatGLM2Utils.parseText(response); } catch (Exception e) { - e.printStackTrace(); + log.warn("glm2 doChat warn:", e); return getErrorText(); } if (StringUtils.isNotBlank(text)) { @@ -132,7 +148,7 @@ public class ChatGLM2Handler extends AbstractChatAIHandler { if (StringUtils.isBlank(message.getContent())) { return false; } - return StringUtils.contains(message.getContent(), "@" + glm2Properties.getAIUserName()) - && StringUtils.isNotBlank(message.getContent().replace(glm2Properties.getAIUserName(), "").trim()); + return StringUtils.contains(message.getContent(), "@" + AI_NAME) + && StringUtils.isNotBlank(message.getContent().replace(AI_NAME, "").trim()); } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java index f814713..440c1d9 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java @@ -2,41 +2,59 @@ package com.abin.mallchat.custom.chatai.handler; import cn.hutool.http.HttpResponse; import com.abin.mallchat.common.chat.domain.entity.Message; +import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra; import com.abin.mallchat.common.common.constant.RedisKey; import com.abin.mallchat.common.common.utils.DateUtils; import com.abin.mallchat.common.common.utils.RedisUtils; import com.abin.mallchat.custom.chatai.properties.ChatGPTProperties; import com.abin.mallchat.custom.chatai.utils.ChatGPTUtils; +import com.abin.mallchat.custom.user.domain.vo.response.user.UserInfoResp; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import java.util.concurrent.TimeUnit; +@Slf4j @Component public class GPTChatAIHandler extends AbstractChatAIHandler { @Autowired private ChatGPTProperties chatGPTProperties; + private static String AI_NAME; + + @Override + protected void init() { + super.init(); + UserInfoResp userInfo = userService.getUserInfo(chatGPTProperties.getAIUserId()); + if (userInfo == null) { + log.error("根据AIUserId:{} 找不到用户信息", chatGPTProperties.getAIUserId()); + throw new RuntimeException("根据AIUserId: " + chatGPTProperties.getAIUserId() + " 找不到用户信息"); + } + if (StringUtils.isBlank(userInfo.getName())) { + log.warn("根据AIUserId:{} 找到的用户信息没有name", chatGPTProperties.getAIUserId()); + throw new RuntimeException("根据AIUserId: " + chatGPTProperties.getAIUserId() + " 找到的用户没有名字"); + } + AI_NAME = userInfo.getName(); + } + + @Override + protected boolean isUse() { + return chatGPTProperties.isUse(); + } + @Override public Long getChatAIUserId() { return chatGPTProperties.getAIUserId(); } - @Override - public String getChatAIName() { - if (StringUtils.isNotBlank(chatGPTProperties.getAIUserName())) { - return chatGPTProperties.getAIUserName(); - } - String name = userService.getUserInfo(chatGPTProperties.getAIUserId()).getName(); - chatGPTProperties.setAIUserName(name); - return name; - } @Override protected String doChat(Message message) { - String content = message.getContent().replace("@" + chatGPTProperties.getAIUserName(), "").trim(); + String content = message.getContent().replace("@" + AI_NAME, "").trim(); Long uid = message.getFromUid(); Long chatNum; String text; @@ -48,12 +66,13 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { response = ChatGPTUtils.create(chatGPTProperties.getKey()) .proxyUrl(chatGPTProperties.getProxyUrl()) .model(chatGPTProperties.getModelName()) + .timeout(chatGPTProperties.getTimeout()) .prompt(content) .send(); text = ChatGPTUtils.parseText(response); userChatNumInrc(uid); } catch (Exception e) { - e.printStackTrace(); + log.warn("gpt doChat warn:", e); text = "我累了,明天再聊吧"; } } @@ -78,21 +97,21 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { } /* 前端传@信息后取消注释 */ -// MessageExtra extra = message.getExtra(); -// if (extra == null) { -// return false; -// } -// if (CollectionUtils.isEmpty(extra.getAtUidList())) { -// return false; -// } -// if (!extra.getAtUidList().contains(chatGPTProperties.getAIUserId())) { -// return false; -// } + MessageExtra extra = message.getExtra(); + if (extra == null) { + return false; + } + if (CollectionUtils.isEmpty(extra.getAtUidList())) { + return false; + } + if (!extra.getAtUidList().contains(chatGPTProperties.getAIUserId())) { + return false; + } if (StringUtils.isBlank(message.getContent())) { return false; } - return StringUtils.contains(message.getContent(), "@" + chatGPTProperties.getAIUserName()) - && StringUtils.isNotBlank(message.getContent().replace(chatGPTProperties.getAIUserName(), "").trim()); + return StringUtils.contains(message.getContent(), "@" + AI_NAME) + && StringUtils.isNotBlank(message.getContent().replace(AI_NAME, "").trim()); } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java index 2ccc99b..7c050b4 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java @@ -30,11 +30,6 @@ public class ChatGLM2Properties { */ private Long AIUserId; - /** - * 机器人名称 - */ - private String AIUserName; - /** * 每个用户每?分钟可以请求一次 */ diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java index b7a5c56..86fd564 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java @@ -18,11 +18,6 @@ public class ChatGPTProperties { * 机器人 id */ private Long AIUserId; - - /** - * 机器人名称 - */ - private String AIUserName; /** * 模型名称 */ @@ -36,6 +31,11 @@ public class ChatGPTProperties { */ private String proxyUrl; + /** + * 超时 + */ + private Integer timeout = 60*1000; + /** * 用户每天条数限制 */ From 6cf7379ac4d420464dea038f16adc9191a8f7004 Mon Sep 17 00:00:00 2001 From: zbzbzzz Date: Mon, 3 Jul 2023 18:06:04 +0800 Subject: [PATCH 07/13] =?UTF-8?q?fix:=E4=BC=98=E5=8C=96ws=E6=8F=A1?= =?UTF-8?q?=E6=89=8B=E5=90=8C=E6=97=B6=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/algorithm/ac/CreateTokenTest.java | 11 +- .../service/impl/WebSocketServiceImpl.java | 4 + .../custom/user/websocket/NettyUtil.java | 2 + .../user/websocket/NettyWebSocketServer.java | 4 +- .../NettyWebSocketServerHandler.java | 19 +- .../NettyWebSocketServerProtocolHandler.java | 61 +++++++ .../websocket/WebSocketHandshakeHandler.java | 165 +++++++++++++++--- 7 files changed, 223 insertions(+), 43 deletions(-) create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerProtocolHandler.java diff --git a/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java b/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java index 50908e4..34d4035 100644 --- a/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java +++ b/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java @@ -6,7 +6,6 @@ import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.JWTVerifier; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.junit.Test; import java.util.Date; @@ -18,7 +17,7 @@ public class CreateTokenTest { @Test public void create(){ String token = JWT.create() - .withClaim("uid", 123L) // 只存一个uid信息,其他的自己去redis查 + .withClaim("uid", 10004L) // 只存一个uid信息,其他的自己去redis查 .withClaim("createTime", new Date()) .sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature log.info("生成的token为 {}",token); @@ -33,12 +32,4 @@ public class CreateTokenTest { } } - @Test - public void verifyToken(){ - String token = JWT.create() - .withClaim("uid", 1) // 只存一个uid信息,其他的自己去redis查 - .withClaim("createTime", new Date()) - .sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature - log.info("生成的token为{}",token); - } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java index 7282473..96d0f68 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java @@ -29,6 +29,7 @@ import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; @@ -140,7 +141,10 @@ public class WebSocketServiceImpl implements WebSocketService { } } + // 这里可以加异步或者事件去处理登录 + // 因为登录和推送消息其实是不用同步的两个步骤 这样可以加快连接的速度不占用nio线程 @Override + @Async public void authorize(Channel channel, WSAuthorize wsAuthorize) { //校验token boolean verifySuccess = loginService.verify(wsAuthorize.getToken()); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyUtil.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyUtil.java index 79daafa..bcc5b05 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyUtil.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyUtil.java @@ -1,6 +1,7 @@ package com.abin.mallchat.custom.user.websocket; import io.netty.channel.Channel; +import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.util.Attribute; import io.netty.util.AttributeKey; @@ -15,6 +16,7 @@ public class NettyUtil { public static AttributeKey TOKEN = AttributeKey.valueOf("token"); public static AttributeKey IP = AttributeKey.valueOf("ip"); public static AttributeKey UID = AttributeKey.valueOf("uid"); + public static AttributeKey HANDSHAKER_ATTR_KEY = AttributeKey.valueOf(WebSocketServerHandshaker.class, "HANDSHAKER"); public static void setAttr(Channel channel, AttributeKey attributeKey, T data) { Attribute attr = channel.attr(attributeKey); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java index b70b867..b84bc06 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java @@ -10,6 +10,7 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.stream.ChunkedWriteHandler; @@ -88,7 +89,8 @@ public class NettyWebSocketServer { * 4. WebSocketServerProtocolHandler 核心功能是把 http协议升级为 ws 协议,保持长连接; * 是通过一个状态码 101 来切换的 */ - pipeline.addLast(new WebSocketHandshakeHandler()); + pipeline.addLast(new NettyWebSocketServerProtocolHandler("/")); + new WebSocketServerProtocolHandler("/"); // 自定义handler ,处理业务逻辑 pipeline.addLast(new NettyWebSocketServerHandler()); } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java index 3d75069..b4fbb0a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java @@ -1,6 +1,5 @@ package com.abin.mallchat.custom.user.websocket; -import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import cn.hutool.json.JSONUtil; import com.abin.mallchat.custom.user.domain.enums.WSReqTypeEnum; @@ -19,10 +18,11 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler { + private WebSocketService webSocketService; // 当web客户端连接后,触发该方法 @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { -// getService().connect(ctx.channel()); + this.webSocketService = getService(); } // 客户端离线 @@ -45,7 +45,7 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler out) throws Exception { + if (this.webSocketServerProtocolConfig.handleCloseFrames() && frame instanceof CloseWebSocketFrame) { + WebSocketServerHandshaker handshaker = NettyUtil.getAttr(ctx.channel(),NettyUtil.HANDSHAKER_ATTR_KEY); + if (handshaker != null) { + frame.retain(); + ChannelPromise promise = ctx.newPromise(); + Method closeSent = ReflectUtil.getMethod(super.getClass(), "closeSent", ChannelPromise.class); + closeSent.setAccessible(true); + closeSent.invoke(this,promise); + handshaker.close(ctx, (CloseWebSocketFrame)frame, promise); + } else { + ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); + } + } else { + super.decode(ctx, frame, out); + } + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java index f501602..8054c90 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java @@ -5,38 +5,159 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; -import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpObject; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.handler.codec.http.HttpVersion; +import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakeException; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; +import io.netty.handler.ssl.SslHandler; +import io.netty.util.ReferenceCountUtil; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.FutureListener; +import io.netty.util.internal.ObjectUtil; + +import java.util.concurrent.TimeUnit; public class WebSocketHandshakeHandler extends ChannelInboundHandlerAdapter { + + private final WebSocketServerProtocolConfig serverConfig; + private ChannelHandlerContext ctx; + private ChannelPromise handshakePromise; + private boolean isWebSocketPath; + + public WebSocketHandshakeHandler(WebSocketServerProtocolConfig serverConfig) { + this.serverConfig = (WebSocketServerProtocolConfig)ObjectUtil.checkNotNull(serverConfig, "serverConfig"); + } + @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - if (msg instanceof FullHttpRequest) { - FullHttpRequest request = (FullHttpRequest) msg; - String token = request.headers().get("Sec-Websocket-Protocol"); - NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token); - // 构建WebSocket握手处理器 - WebSocketServerHandshakerFactory handshakeFactory = new WebSocketServerHandshakerFactory( - request.uri(), token, false); - WebSocketServerHandshaker handshake = handshakeFactory.newHandshaker(request); - final ChannelFuture handshakeFuture = handshake.handshake(ctx.channel(), request); - ctx.pipeline().remove(this); - handshakeFuture.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) { - if (!future.isSuccess()) { - ctx.fireExceptionCaught(future.cause()); + public void handlerAdded(ChannelHandlerContext ctx) { + this.ctx = ctx; + this.handshakePromise = ctx.newPromise(); + } + + @Override + public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { + HttpObject httpObject = (HttpObject)msg; + if (httpObject instanceof HttpRequest) { + final HttpRequest req = (HttpRequest)httpObject; + this.isWebSocketPath = this.isWebSocketPath(req); + if (!this.isWebSocketPath) { + ctx.fireChannelRead(msg); + return; + } + try { + if (HttpMethod.GET.equals(req.method())) { + final String token = req.headers().get("Sec-Websocket-Protocol"); + WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ctx.pipeline(), req, this.serverConfig.websocketPath()), token, this.serverConfig.decoderConfig()); + final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); + NettyUtil.setAttr(ctx.channel(),NettyUtil.HANDSHAKER_ATTR_KEY,handshaker); + final ChannelPromise localHandshakePromise = this.handshakePromise; + if (handshaker == null) { + WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { - // 手动触发WebSocket握手状态事件 - ctx.fireUserEventTriggered( - WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE); + + ctx.pipeline().remove(this); + ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req); + handshakeFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) { + if (!future.isSuccess()) { + localHandshakePromise.tryFailure(future.cause()); + ctx.fireExceptionCaught(future.cause()); + } else { + localHandshakePromise.trySuccess(); + NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token); + ctx.fireUserEventTriggered(WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE); + } + } + }); + this.applyHandshakeTimeout(); } + + return; } - }); + + sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN, ctx.alloc().buffer(0))); + } finally { + ReferenceCountUtil.release(req); + } + + return; + } else if (!this.isWebSocketPath) { + ctx.fireChannelRead(msg); } else { - super.channelRead(ctx, msg); + ReferenceCountUtil.release(msg); + } + + } + + private boolean isWebSocketPath(HttpRequest req) { + String websocketPath = this.serverConfig.websocketPath(); + String uri = req.uri(); + boolean checkStartUri = uri.startsWith(websocketPath); + boolean checkNextUri = "/".equals(websocketPath) || this.checkNextUri(uri, websocketPath); + return this.serverConfig.checkStartsWith() ? checkStartUri && checkNextUri : uri.equals(websocketPath); + } + + private boolean checkNextUri(String uri, String websocketPath) { + int len = websocketPath.length(); + if (uri.length() <= len) { + return true; + } else { + char nextUri = uri.charAt(len); + return nextUri == '/' || nextUri == '?'; } } + + private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { + ChannelFuture f = ctx.channel().writeAndFlush(res); + if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { + f.addListener(ChannelFutureListener.CLOSE); + } + + } + + private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) { + String protocol = "ws"; + if (cp.get(SslHandler.class) != null) { + protocol = "wss"; + } + + String host = req.headers().get(HttpHeaderNames.HOST); + return protocol + "://" + host + path; + } + + private void applyHandshakeTimeout() { + final ChannelPromise localHandshakePromise = this.handshakePromise; + long handshakeTimeoutMillis = this.serverConfig.handshakeTimeoutMillis(); + if (handshakeTimeoutMillis > 0L && !localHandshakePromise.isDone()) { + final Future timeoutFuture = this.ctx.executor().schedule(new Runnable() { + @Override + public void run() { + if (!localHandshakePromise.isDone() && localHandshakePromise.tryFailure(new WebSocketServerHandshakeException("handshake timed out"))) { + WebSocketHandshakeHandler.this.ctx.flush().fireUserEventTriggered(WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_TIMEOUT).close(); + } + + } + }, handshakeTimeoutMillis, TimeUnit.MILLISECONDS); + localHandshakePromise.addListener(new FutureListener() { + @Override + public void operationComplete(Future f) { + timeoutFuture.cancel(false); + } + }); + } + } + } From f7a80fa8d9da58ae85d5725fb5aa9b4d5f2c8b2c Mon Sep 17 00:00:00 2001 From: zbzbzzz Date: Mon, 3 Jul 2023 18:09:27 +0800 Subject: [PATCH 08/13] =?UTF-8?q?fix:=E4=BC=98=E5=8C=96ws=E6=8F=A1?= =?UTF-8?q?=E6=89=8B=E5=90=8C=E6=97=B6=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mallchat/custom/user/websocket/NettyWebSocketServer.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java index b84bc06..3448528 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java @@ -10,7 +10,6 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; -import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.stream.ChunkedWriteHandler; @@ -90,7 +89,6 @@ public class NettyWebSocketServer { * 是通过一个状态码 101 来切换的 */ pipeline.addLast(new NettyWebSocketServerProtocolHandler("/")); - new WebSocketServerProtocolHandler("/"); // 自定义handler ,处理业务逻辑 pipeline.addLast(new NettyWebSocketServerHandler()); } From e671c66a26a90de73de339c14339dac93e7ad484 Mon Sep 17 00:00:00 2001 From: zbzbzzz Date: Mon, 3 Jul 2023 18:34:14 +0800 Subject: [PATCH 09/13] =?UTF-8?q?fix:=E4=BC=98=E5=8C=96ws=E6=8F=A1?= =?UTF-8?q?=E6=89=8B=E5=90=8C=E6=97=B6=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/websocket/NettyWebSocketServerProtocolHandler.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerProtocolHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerProtocolHandler.java index 4ada0c9..73c7471 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerProtocolHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerProtocolHandler.java @@ -8,6 +8,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; +import io.netty.handler.codec.http.websocketx.Utf8FrameValidator; import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig; @@ -37,6 +38,10 @@ public class NettyWebSocketServerProtocolHandler extends WebSocketServerProtocol cp.addBefore(ctx.name(), WebSocketHandshakeHandler.class.getName(), new WebSocketHandshakeHandler(this.webSocketServerProtocolConfig)); } + if (this.webSocketServerProtocolConfig.decoderConfig().withUTF8Validator() && cp.get(Utf8FrameValidator.class) == null) { + cp.addBefore(ctx.name(), Utf8FrameValidator.class.getName(), new Utf8FrameValidator(this.webSocketServerProtocolConfig.decoderConfig().closeOnProtocolViolation())); + } + } @Override From 5d8e32abf86646fc962801fe09d5d3268f2d2359 Mon Sep 17 00:00:00 2001 From: zbzbzzz Date: Tue, 4 Jul 2023 09:56:35 +0800 Subject: [PATCH 10/13] =?UTF-8?q?fix:=E4=BC=98=E5=8C=96ws=E6=8F=A1?= =?UTF-8?q?=E6=89=8B=E5=90=8C=E6=97=B6=E8=AE=A4=E8=AF=81=20=E5=8A=A0?= =?UTF-8?q?=E4=B8=8Atoken=E4=B8=BA=E7=A9=BA=E5=88=A4=E6=96=AD=20=E5=8E=BB?= =?UTF-8?q?=E6=8E=89=E5=BC=82=E6=AD=A5=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../custom/user/service/impl/WebSocketServiceImpl.java | 10 +++++----- .../user/websocket/NettyWebSocketServerHandler.java | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java index 96d0f68..8515083 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java @@ -29,11 +29,14 @@ import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; -import java.util.*; +import java.util.Date; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.locks.Condition; @@ -141,10 +144,7 @@ public class WebSocketServiceImpl implements WebSocketService { } } - // 这里可以加异步或者事件去处理登录 - // 因为登录和推送消息其实是不用同步的两个步骤 这样可以加快连接的速度不占用nio线程 @Override - @Async public void authorize(Channel channel, WSAuthorize wsAuthorize) { //校验token boolean verifySuccess = loginService.verify(wsAuthorize.getToken()); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java index b4fbb0a..ecbbb24 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java @@ -1,5 +1,6 @@ package com.abin.mallchat.custom.user.websocket; +import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import cn.hutool.json.JSONUtil; import com.abin.mallchat.custom.user.domain.enums.WSReqTypeEnum; @@ -19,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler { private WebSocketService webSocketService; + // 当web客户端连接后,触发该方法 @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { @@ -67,9 +69,10 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler Date: Tue, 4 Jul 2023 10:09:36 +0800 Subject: [PATCH 11/13] =?UTF-8?q?feat:=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mallchat/custom/common/intecepter/TokenInterceptor.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/intecepter/TokenInterceptor.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/intecepter/TokenInterceptor.java index 719fb0d..814297b 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/intecepter/TokenInterceptor.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/intecepter/TokenInterceptor.java @@ -45,6 +45,11 @@ public class TokenInterceptor implements HandlerInterceptor { return true; } + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + MDC.remove(MDCKey.UID); + } + /** * 判断是不是公共方法,可以未登录访问的 * From 919bdc7c9906fb8c974f47aa1f9236d050b41630 Mon Sep 17 00:00:00 2001 From: zbzbzzz Date: Tue, 4 Jul 2023 17:04:52 +0800 Subject: [PATCH 12/13] =?UTF-8?q?fix:=E6=8F=A1=E6=89=8B=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=94=B9=E4=B8=BA=E5=8F=82=E6=95=B0=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/algorithm/ac/CreateTokenTest.java | 1 - .../user/websocket/HttpHeadersHandler.java | 17 +- .../user/websocket/NettyWebSocketServer.java | 3 +- .../NettyWebSocketServerHandler.java | 2 +- .../NettyWebSocketServerProtocolHandler.java | 66 ------- .../websocket/WebSocketHandshakeHandler.java | 163 ------------------ 6 files changed, 18 insertions(+), 234 deletions(-) delete mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerProtocolHandler.java delete mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java diff --git a/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java b/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java index 34d4035..06c1845 100644 --- a/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java +++ b/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java @@ -31,5 +31,4 @@ public class CreateTokenTest { log.info("decode error,token:{}", token, e); } } - } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/HttpHeadersHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/HttpHeadersHandler.java index 30f6cdd..b6cdd43 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/HttpHeadersHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/HttpHeadersHandler.java @@ -1,5 +1,6 @@ package com.abin.mallchat.custom.user.websocket; +import cn.hutool.core.net.url.UrlBuilder; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.FullHttpRequest; @@ -13,7 +14,16 @@ public class HttpHeadersHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof FullHttpRequest) { - HttpHeaders headers = ((FullHttpRequest) msg).headers(); + FullHttpRequest request = (FullHttpRequest) msg; + UrlBuilder urlBuilder = UrlBuilder.ofHttp(request.uri()); + + // 获取token参数 + String token = urlBuilder.getQuery().get("token").toString(); + NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token); + + // 获取请求路径 + request.setUri(urlBuilder.getPath().toString()); + HttpHeaders headers = request.headers(); String ip = headers.get("X-Real-IP"); if (StringUtils.isEmpty(ip)) {//如果没经过nginx,就直接获取远端地址 InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress(); @@ -21,7 +31,10 @@ public class HttpHeadersHandler extends ChannelInboundHandlerAdapter { } NettyUtil.setAttr(ctx.channel(), NettyUtil.IP, ip); ctx.pipeline().remove(this); + ctx.fireChannelRead(request); + }else + { + ctx.fireChannelRead(msg); } - ctx.fireChannelRead(msg); } } \ No newline at end of file diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java index 3448528..4ebf470 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java @@ -10,6 +10,7 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.stream.ChunkedWriteHandler; @@ -88,7 +89,7 @@ public class NettyWebSocketServer { * 4. WebSocketServerProtocolHandler 核心功能是把 http协议升级为 ws 协议,保持长连接; * 是通过一个状态码 101 来切换的 */ - pipeline.addLast(new NettyWebSocketServerProtocolHandler("/")); + pipeline.addLast(new WebSocketServerProtocolHandler("/")); // 自定义handler ,处理业务逻辑 pipeline.addLast(new NettyWebSocketServerHandler()); } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java index ecbbb24..dbc62f9 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServerHandler.java @@ -71,7 +71,7 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler out) throws Exception { - if (this.webSocketServerProtocolConfig.handleCloseFrames() && frame instanceof CloseWebSocketFrame) { - WebSocketServerHandshaker handshaker = NettyUtil.getAttr(ctx.channel(),NettyUtil.HANDSHAKER_ATTR_KEY); - if (handshaker != null) { - frame.retain(); - ChannelPromise promise = ctx.newPromise(); - Method closeSent = ReflectUtil.getMethod(super.getClass(), "closeSent", ChannelPromise.class); - closeSent.setAccessible(true); - closeSent.invoke(this,promise); - handshaker.close(ctx, (CloseWebSocketFrame)frame, promise); - } else { - ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); - } - } else { - super.decode(ctx, frame, out); - } - } -} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java deleted file mode 100644 index 8054c90..0000000 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/WebSocketHandshakeHandler.java +++ /dev/null @@ -1,163 +0,0 @@ -package com.abin.mallchat.custom.user.websocket; - - -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.ChannelPromise; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.HttpHeaderNames; -import io.netty.handler.codec.http.HttpMethod; -import io.netty.handler.codec.http.HttpObject; -import io.netty.handler.codec.http.HttpRequest; -import io.netty.handler.codec.http.HttpResponse; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpUtil; -import io.netty.handler.codec.http.HttpVersion; -import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakeException; -import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; -import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; -import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig; -import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; -import io.netty.handler.ssl.SslHandler; -import io.netty.util.ReferenceCountUtil; -import io.netty.util.concurrent.Future; -import io.netty.util.concurrent.FutureListener; -import io.netty.util.internal.ObjectUtil; - -import java.util.concurrent.TimeUnit; - -public class WebSocketHandshakeHandler extends ChannelInboundHandlerAdapter { - - private final WebSocketServerProtocolConfig serverConfig; - private ChannelHandlerContext ctx; - private ChannelPromise handshakePromise; - private boolean isWebSocketPath; - - public WebSocketHandshakeHandler(WebSocketServerProtocolConfig serverConfig) { - this.serverConfig = (WebSocketServerProtocolConfig)ObjectUtil.checkNotNull(serverConfig, "serverConfig"); - } - - @Override - public void handlerAdded(ChannelHandlerContext ctx) { - this.ctx = ctx; - this.handshakePromise = ctx.newPromise(); - } - - @Override - public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { - HttpObject httpObject = (HttpObject)msg; - if (httpObject instanceof HttpRequest) { - final HttpRequest req = (HttpRequest)httpObject; - this.isWebSocketPath = this.isWebSocketPath(req); - if (!this.isWebSocketPath) { - ctx.fireChannelRead(msg); - return; - } - try { - if (HttpMethod.GET.equals(req.method())) { - final String token = req.headers().get("Sec-Websocket-Protocol"); - WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ctx.pipeline(), req, this.serverConfig.websocketPath()), token, this.serverConfig.decoderConfig()); - final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); - NettyUtil.setAttr(ctx.channel(),NettyUtil.HANDSHAKER_ATTR_KEY,handshaker); - final ChannelPromise localHandshakePromise = this.handshakePromise; - if (handshaker == null) { - WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); - } else { - - ctx.pipeline().remove(this); - ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req); - handshakeFuture.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) { - if (!future.isSuccess()) { - localHandshakePromise.tryFailure(future.cause()); - ctx.fireExceptionCaught(future.cause()); - } else { - localHandshakePromise.trySuccess(); - NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token); - ctx.fireUserEventTriggered(WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE); - } - } - }); - this.applyHandshakeTimeout(); - } - - return; - } - - sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN, ctx.alloc().buffer(0))); - } finally { - ReferenceCountUtil.release(req); - } - - return; - } else if (!this.isWebSocketPath) { - ctx.fireChannelRead(msg); - } else { - ReferenceCountUtil.release(msg); - } - - } - - private boolean isWebSocketPath(HttpRequest req) { - String websocketPath = this.serverConfig.websocketPath(); - String uri = req.uri(); - boolean checkStartUri = uri.startsWith(websocketPath); - boolean checkNextUri = "/".equals(websocketPath) || this.checkNextUri(uri, websocketPath); - return this.serverConfig.checkStartsWith() ? checkStartUri && checkNextUri : uri.equals(websocketPath); - } - - private boolean checkNextUri(String uri, String websocketPath) { - int len = websocketPath.length(); - if (uri.length() <= len) { - return true; - } else { - char nextUri = uri.charAt(len); - return nextUri == '/' || nextUri == '?'; - } - } - - private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { - ChannelFuture f = ctx.channel().writeAndFlush(res); - if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { - f.addListener(ChannelFutureListener.CLOSE); - } - - } - - private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) { - String protocol = "ws"; - if (cp.get(SslHandler.class) != null) { - protocol = "wss"; - } - - String host = req.headers().get(HttpHeaderNames.HOST); - return protocol + "://" + host + path; - } - - private void applyHandshakeTimeout() { - final ChannelPromise localHandshakePromise = this.handshakePromise; - long handshakeTimeoutMillis = this.serverConfig.handshakeTimeoutMillis(); - if (handshakeTimeoutMillis > 0L && !localHandshakePromise.isDone()) { - final Future timeoutFuture = this.ctx.executor().schedule(new Runnable() { - @Override - public void run() { - if (!localHandshakePromise.isDone() && localHandshakePromise.tryFailure(new WebSocketServerHandshakeException("handshake timed out"))) { - WebSocketHandshakeHandler.this.ctx.flush().fireUserEventTriggered(WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_TIMEOUT).close(); - } - - } - }, handshakeTimeoutMillis, TimeUnit.MILLISECONDS); - localHandshakePromise.addListener(new FutureListener() { - @Override - public void operationComplete(Future f) { - timeoutFuture.cancel(false); - } - }); - } - } - -} From a6635834908162a6c7009f0aa2df9359d36a1e63 Mon Sep 17 00:00:00 2001 From: zhongzb <972627721@qq.com> Date: Thu, 6 Jul 2023 00:10:57 +0800 Subject: [PATCH 13/13] fix:readme --- .../abin/mallchat/common/common/exception/CommonErrorEnum.java | 2 +- .../mallchat/common/common/algorithm/ac/CreateTokenTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java index edcd880..b1b7ec8 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/CommonErrorEnum.java @@ -13,7 +13,7 @@ import lombok.Getter; public enum CommonErrorEnum implements ErrorEnum { SYSTEM_ERROR(-1, "系统出小差了,请稍后再试哦~~"), - PARAM_VALID(-2, "参数校验失败"), + PARAM_VALID(-2, "参数校验失败{0}"), FREQUENCY_LIMIT(-3, "请求太频繁了,请稍后再试哦~~"), LOCK_LIMIT(-4, "请求太频繁了,请稍后再试哦~~"), ; diff --git a/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java b/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java index 06c1845..e73a9a9 100644 --- a/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java +++ b/mallchat-common/src/test/java/com/abin/mallchat/common/common/algorithm/ac/CreateTokenTest.java @@ -24,7 +24,7 @@ public class CreateTokenTest { try { - JWTVerifier verifier = JWT.require(Algorithm.HMAC256("dsfsdfsdfsdfsd")).build(); + JWTVerifier verifier = JWT.require(Algorithm.HMAC256("dsfsdfsdfsdfsc")).build(); DecodedJWT jwt = verifier.verify(token); log.info(jwt.getClaims().toString()); } catch (Exception e) {