增加websocket内容AES加解密配置

This commit is contained in:
wanggeng 2021-12-08 12:05:19 +08:00
parent 6d4647f557
commit ab6cf6661a
5 changed files with 42 additions and 5 deletions

View File

@ -18,6 +18,7 @@ public class WebSocketProperties {
private String url;
private Integer port;
private String context;
private Boolean isEncrypt;
public String getScheme() {
return scheme == null ? "ws" : scheme.trim();
@ -50,4 +51,12 @@ public class WebSocketProperties {
public void setContext(String context) {
this.context = context;
}
public Boolean getEncrypt() {
return isEncrypt == null ? false : isEncrypt;
}
public void setEncrypt(Boolean encrypt) {
isEncrypt = encrypt;
}
}

View File

@ -3,6 +3,7 @@ package ink.wgink.module.instantmessage.startup;
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
import ink.wgink.module.instantmessage.websocket.manager.WebSocketChannelManager;
import ink.wgink.module.instantmessage.websocket.server.WebSocketServer;
import ink.wgink.properties.websocket.WebSocketProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
@ -21,11 +22,15 @@ public class WebSocketStartUp implements ApplicationRunner {
@Autowired
private WebSocketServer webSocketServer;
@Autowired
private WebSocketProperties webSocketProperties;
@Autowired
private IWebSocketUserSessionManager webSocketUserSessionManager;
@Override
public void run(ApplicationArguments args) throws Exception {
WebSocketChannelManager.getInstance().setWebSocketUserSessionManager(webSocketUserSessionManager);
WebSocketChannelManager webSocketChannelManager = WebSocketChannelManager.getInstance();
webSocketChannelManager.setWebSocketUserSessionManager(webSocketUserSessionManager);
webSocketChannelManager.setWebSocketProperties(webSocketProperties);
new Thread(webSocketServer).start();
}

View File

@ -84,6 +84,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<Object> {
WebSocketTextHandler webSocketTextHandler = new WebSocketTextHandler();
webSocketTextHandler.setWebSocketTextCustomHandler(IWebSocketTextCustomService);
webSocketTextHandler.setMessageService(messageService);
webSocketTextHandler.setWebSocketProperties(webSocketProperties);
webSocketTextHandler.handler(ctx, (TextWebSocketFrame) frame);
return;
}

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.MessageSendStatusBody;
import ink.wgink.pojo.session.WebSocketSession;
import ink.wgink.properties.websocket.WebSocketProperties;
import ink.wgink.util.AesUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
@ -42,13 +43,18 @@ public class WebSocketTextHandler {
private static final Logger LOG = LoggerFactory.getLogger(WebSocketTextHandler.class);
private IWebSocketTextCustomService IWebSocketTextCustomService;
private IMessageService messageService;
private WebSocketProperties webSocketProperties;
public void handler(ChannelHandlerContext ctx, TextWebSocketFrame textWebSocketFrame) {
WebSocketClientMessage clientSocketMessage = null;
MessageSendStatusBody messageSendStatusBody = null;
try {
if (webSocketProperties.getEncrypt()) {
String decodeMessage = AesUtil.aesCommonDecoder(WebSocketChannelManager.MESSAGE_AES_KEY, textWebSocketFrame.text());
clientSocketMessage = JSONObject.parseObject(decodeMessage, WebSocketClientMessage.class);
} else {
clientSocketMessage = JSONObject.parseObject(textWebSocketFrame.text(), WebSocketClientMessage.class);
}
if (clientSocketMessage.getType() == null) {
throw new TypeException("Type类型不能为空");
}
@ -265,4 +271,8 @@ public class WebSocketTextHandler {
public void setMessageService(IMessageService messageService) {
this.messageService = messageService;
}
public void setWebSocketProperties(WebSocketProperties webSocketProperties) {
this.webSocketProperties = webSocketProperties;
}
}

View File

@ -2,10 +2,11 @@ package ink.wgink.module.instantmessage.websocket.manager;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.exceptions.websocket.SessionException;
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
import ink.wgink.interfaces.manager.IWebSocketChannelManager;
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
import ink.wgink.module.instantmessage.websocket.pojo.WebSocketClientMessage;
import ink.wgink.pojo.session.WebSocketSession;
import ink.wgink.properties.websocket.WebSocketProperties;
import ink.wgink.util.AesUtil;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
@ -28,6 +29,7 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
private static final WebSocketChannelManager webSocketChannelManager = WebSocketChannelManagerBuilder.webSocketChannelManager;
private ChannelGroup globalGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
private IWebSocketUserSessionManager webSocketUserSessionManager;
private WebSocketProperties webSocketProperties;
private WebSocketChannelManager() {
}
@ -154,7 +156,13 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
if (toChannel == null || !toChannel.isOpen() || !toChannel.isActive()) {
return;
}
TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(AesUtil.aesCommonEncoder(MESSAGE_AES_KEY, JSONObject.toJSONString(webSocketClientMessage)));
String message;
if (webSocketProperties.getEncrypt()) {
message = AesUtil.aesCommonEncoder(MESSAGE_AES_KEY, JSONObject.toJSONString(webSocketClientMessage));
} else {
message = JSONObject.toJSONString(webSocketClientMessage);
}
TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(message);
toChannel.writeAndFlush(textWebSocketFrame);
}
@ -170,6 +178,10 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
}
}
public void setWebSocketProperties(WebSocketProperties webSocketProperties) {
this.webSocketProperties = webSocketProperties;
}
@Override
public void setWebSocketUserSessionManager(IWebSocketUserSessionManager webSocketUserSessionManager) {
this.webSocketUserSessionManager = webSocketUserSessionManager;