消息添加AES加解密

This commit is contained in:
wanggeng 2021-12-07 16:10:18 +08:00
parent aec514fa96
commit dd04255ea4
3 changed files with 25 additions and 7 deletions

View File

@ -20,11 +20,14 @@ public enum MessageSendStatusEnum {
FROM_ERROR(405, "来源错误"), FROM_ERROR(405, "来源错误"),
TO_ERROR(406, "接收人错误"), TO_ERROR(406, "接收人错误"),
RECEIVE_ERROR(407, "接收错误"), RECEIVE_ERROR(407, "接收错误"),
MESSAGE_ENCODE_ERROR(408, "消息加密错误"),
MESSAGE_DECODE_ERROR(409, "消息解密错误"),
USER_OFFLINE(410, "用户离线"), USER_OFFLINE(410, "用户离线"),
TEXT_MESSAGE_USER_OFFLINE(411, "文本消息用户离线"), TEXT_MESSAGE_USER_OFFLINE(411, "文本消息用户离线"),
RTC_MESSAGE_USER_OFFLINE(412, "实时语音用户离线"), RTC_MESSAGE_USER_OFFLINE(412, "实时语音用户离线"),
CUSTOM_HANDLE_ERROR(500, "自定义处理异常"); CUSTOM_HANDLE_ERROR(500, "自定义处理异常");
private int value; private int value;

View File

@ -15,6 +15,7 @@ import ink.wgink.module.instantmessage.websocket.pojo.body.IdsBody;
import ink.wgink.module.instantmessage.websocket.pojo.body.RegisterBody; import ink.wgink.module.instantmessage.websocket.pojo.body.RegisterBody;
import ink.wgink.module.instantmessage.websocket.pojo.body.MessageSendStatusBody; import ink.wgink.module.instantmessage.websocket.pojo.body.MessageSendStatusBody;
import ink.wgink.pojo.session.WebSocketSession; import ink.wgink.pojo.session.WebSocketSession;
import ink.wgink.util.AesUtil;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
@ -46,7 +47,8 @@ public class WebSocketTextHandler {
WebSocketClientMessage clientSocketMessage = null; WebSocketClientMessage clientSocketMessage = null;
MessageSendStatusBody messageSendStatusBody = null; MessageSendStatusBody messageSendStatusBody = null;
try { try {
clientSocketMessage = JSONObject.parseObject(textWebSocketFrame.text(), WebSocketClientMessage.class); String decodeMessage = AesUtil.aesCommonDecoder(WebSocketChannelManager.MESSAGE_AES_KEY, textWebSocketFrame.text());
clientSocketMessage = JSONObject.parseObject(decodeMessage, WebSocketClientMessage.class);
if (clientSocketMessage.getType() == null) { if (clientSocketMessage.getType() == null) {
throw new TypeException("Type类型不能为空"); throw new TypeException("Type类型不能为空");
} }
@ -120,7 +122,16 @@ public class WebSocketTextHandler {
} else { } else {
messageSendStatusBody = new MessageSendStatusBody(MessageSendStatusEnum.CUSTOM_HANDLE_ERROR.getValue(), MessageSendStatusEnum.CUSTOM_HANDLE_ERROR, e.getMessage()); messageSendStatusBody = new MessageSendStatusBody(MessageSendStatusEnum.CUSTOM_HANDLE_ERROR.getValue(), MessageSendStatusEnum.CUSTOM_HANDLE_ERROR, e.getMessage());
} }
} catch (AesUtil.AesEncodeException e) {
LOG.error(e.getMessage(), e);
clientSocketMessage = new WebSocketClientMessage();
messageSendStatusBody = new MessageSendStatusBody(MessageSendStatusEnum.MESSAGE_ENCODE_ERROR.getValue(), MessageSendStatusEnum.MESSAGE_ENCODE_ERROR, e.getMessage());
} catch (AesUtil.AesDecodeException e) {
LOG.error(e.getMessage(), e);
clientSocketMessage = new WebSocketClientMessage();
messageSendStatusBody = new MessageSendStatusBody(MessageSendStatusEnum.MESSAGE_DECODE_ERROR.getValue(), MessageSendStatusEnum.MESSAGE_DECODE_ERROR, e.getMessage());
} catch (JSONException e) { } catch (JSONException e) {
LOG.error(e.getMessage(), e);
clientSocketMessage = new WebSocketClientMessage(); clientSocketMessage = new WebSocketClientMessage();
messageSendStatusBody = new MessageSendStatusBody(MessageSendStatusEnum.TO_ERROR.getValue(), MessageSendStatusEnum.MESSAGE_ERROR, e.getMessage()); messageSendStatusBody = new MessageSendStatusBody(MessageSendStatusEnum.TO_ERROR.getValue(), MessageSendStatusEnum.MESSAGE_ERROR, e.getMessage());
} finally { } finally {
@ -170,7 +181,7 @@ public class WebSocketTextHandler {
* @param webSocketClientMessage 客户端消息 * @param webSocketClientMessage 客户端消息
* @throws ToException * @throws ToException
*/ */
private void sendText(Channel channel, WebSocketClientMessage webSocketClientMessage) throws ToException, TextUserOfflineException { private void sendText(Channel channel, WebSocketClientMessage webSocketClientMessage) throws ToException, TextUserOfflineException, AesUtil.AesEncodeException {
if (StringUtils.isBlank(webSocketClientMessage.getTo())) { if (StringUtils.isBlank(webSocketClientMessage.getTo())) {
throw new ToException("To 值不能为空"); throw new ToException("To 值不能为空");
} }
@ -194,7 +205,7 @@ public class WebSocketTextHandler {
* @throws ToException * @throws ToException
* @throws TextUserOfflineException * @throws TextUserOfflineException
*/ */
private void sendGroupText(Channel fromChannel, WebSocketClientMessage webSocketClientMessage) throws ToException, TextUserOfflineException { private void sendGroupText(Channel fromChannel, WebSocketClientMessage webSocketClientMessage) throws ToException, TextUserOfflineException, AesUtil.AesEncodeException {
if (StringUtils.isBlank(webSocketClientMessage.getTo())) { if (StringUtils.isBlank(webSocketClientMessage.getTo())) {
throw new ToException("To 值不能为空"); throw new ToException("To 值不能为空");
} }

View File

@ -6,6 +6,7 @@ import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
import ink.wgink.interfaces.manager.IWebSocketChannelManager; import ink.wgink.interfaces.manager.IWebSocketChannelManager;
import ink.wgink.module.instantmessage.websocket.pojo.WebSocketClientMessage; import ink.wgink.module.instantmessage.websocket.pojo.WebSocketClientMessage;
import ink.wgink.pojo.session.WebSocketSession; import ink.wgink.pojo.session.WebSocketSession;
import ink.wgink.util.AesUtil;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup; import io.netty.channel.group.DefaultChannelGroup;
@ -23,11 +24,13 @@ import java.util.List;
*/ */
public class WebSocketChannelManager implements IWebSocketChannelManager { public class WebSocketChannelManager implements IWebSocketChannelManager {
public static final String FORM_SYSTEM = "SYSTEM"; public static final String FORM_SYSTEM = "SYSTEM";
public static final String MESSAGE_AES_KEY = "SocKEtsEcReT_KeY";
private static final WebSocketChannelManager webSocketChannelManager = WebSocketChannelManagerBuilder.webSocketChannelManager; private static final WebSocketChannelManager webSocketChannelManager = WebSocketChannelManagerBuilder.webSocketChannelManager;
private ChannelGroup globalGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); private ChannelGroup globalGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
private IWebSocketUserSessionManager webSocketUserSessionManager; private IWebSocketUserSessionManager webSocketUserSessionManager;
private WebSocketChannelManager() {} private WebSocketChannelManager() {
}
public static WebSocketChannelManager getInstance() { public static WebSocketChannelManager getInstance() {
return webSocketChannelManager; return webSocketChannelManager;
@ -145,12 +148,13 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
* *
* @param toChannel 接收人通道 * @param toChannel 接收人通道
* @param webSocketClientMessage 发送内容 * @param webSocketClientMessage 发送内容
* @throws AesUtil.AesEncodeException
*/ */
public void sendText(Channel toChannel, WebSocketClientMessage webSocketClientMessage) { public void sendText(Channel toChannel, WebSocketClientMessage webSocketClientMessage) throws AesUtil.AesEncodeException {
if (toChannel == null || !toChannel.isOpen() || !toChannel.isActive()) { if (toChannel == null || !toChannel.isOpen() || !toChannel.isActive()) {
return; return;
} }
TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(JSONObject.toJSONString(webSocketClientMessage)); TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(AesUtil.aesCommonEncoder(MESSAGE_AES_KEY, JSONObject.toJSONString(webSocketClientMessage)));
toChannel.writeAndFlush(textWebSocketFrame); toChannel.writeAndFlush(textWebSocketFrame);
} }
@ -160,7 +164,7 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
* @param channels * @param channels
* @param webSocketClientMessage * @param webSocketClientMessage
*/ */
public void sendGroupText(List<Channel> channels, WebSocketClientMessage webSocketClientMessage) { public void sendGroupText(List<Channel> channels, WebSocketClientMessage webSocketClientMessage) throws AesUtil.AesEncodeException {
for (Channel channel : channels) { for (Channel channel : channels) {
sendText(channel, webSocketClientMessage); sendText(channel, webSocketClientMessage);
} }