增加websocket内容AES加解密配置
This commit is contained in:
parent
6d4647f557
commit
ab6cf6661a
@ -18,6 +18,7 @@ public class WebSocketProperties {
|
|||||||
private String url;
|
private String url;
|
||||||
private Integer port;
|
private Integer port;
|
||||||
private String context;
|
private String context;
|
||||||
|
private Boolean isEncrypt;
|
||||||
|
|
||||||
public String getScheme() {
|
public String getScheme() {
|
||||||
return scheme == null ? "ws" : scheme.trim();
|
return scheme == null ? "ws" : scheme.trim();
|
||||||
@ -50,4 +51,12 @@ public class WebSocketProperties {
|
|||||||
public void setContext(String context) {
|
public void setContext(String context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getEncrypt() {
|
||||||
|
return isEncrypt == null ? false : isEncrypt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEncrypt(Boolean encrypt) {
|
||||||
|
isEncrypt = encrypt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package ink.wgink.module.instantmessage.startup;
|
|||||||
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
|
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
|
||||||
import ink.wgink.module.instantmessage.websocket.manager.WebSocketChannelManager;
|
import ink.wgink.module.instantmessage.websocket.manager.WebSocketChannelManager;
|
||||||
import ink.wgink.module.instantmessage.websocket.server.WebSocketServer;
|
import ink.wgink.module.instantmessage.websocket.server.WebSocketServer;
|
||||||
|
import ink.wgink.properties.websocket.WebSocketProperties;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
@ -21,11 +22,15 @@ public class WebSocketStartUp implements ApplicationRunner {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private WebSocketServer webSocketServer;
|
private WebSocketServer webSocketServer;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
private WebSocketProperties webSocketProperties;
|
||||||
|
@Autowired
|
||||||
private IWebSocketUserSessionManager webSocketUserSessionManager;
|
private IWebSocketUserSessionManager webSocketUserSessionManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
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();
|
new Thread(webSocketServer).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +84,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler<Object> {
|
|||||||
WebSocketTextHandler webSocketTextHandler = new WebSocketTextHandler();
|
WebSocketTextHandler webSocketTextHandler = new WebSocketTextHandler();
|
||||||
webSocketTextHandler.setWebSocketTextCustomHandler(IWebSocketTextCustomService);
|
webSocketTextHandler.setWebSocketTextCustomHandler(IWebSocketTextCustomService);
|
||||||
webSocketTextHandler.setMessageService(messageService);
|
webSocketTextHandler.setMessageService(messageService);
|
||||||
|
webSocketTextHandler.setWebSocketProperties(webSocketProperties);
|
||||||
webSocketTextHandler.handler(ctx, (TextWebSocketFrame) frame);
|
webSocketTextHandler.handler(ctx, (TextWebSocketFrame) frame);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -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.properties.websocket.WebSocketProperties;
|
||||||
import ink.wgink.util.AesUtil;
|
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;
|
||||||
@ -42,13 +43,18 @@ public class WebSocketTextHandler {
|
|||||||
private static final Logger LOG = LoggerFactory.getLogger(WebSocketTextHandler.class);
|
private static final Logger LOG = LoggerFactory.getLogger(WebSocketTextHandler.class);
|
||||||
private IWebSocketTextCustomService IWebSocketTextCustomService;
|
private IWebSocketTextCustomService IWebSocketTextCustomService;
|
||||||
private IMessageService messageService;
|
private IMessageService messageService;
|
||||||
|
private WebSocketProperties webSocketProperties;
|
||||||
|
|
||||||
public void handler(ChannelHandlerContext ctx, TextWebSocketFrame textWebSocketFrame) {
|
public void handler(ChannelHandlerContext ctx, TextWebSocketFrame textWebSocketFrame) {
|
||||||
WebSocketClientMessage clientSocketMessage = null;
|
WebSocketClientMessage clientSocketMessage = null;
|
||||||
MessageSendStatusBody messageSendStatusBody = null;
|
MessageSendStatusBody messageSendStatusBody = null;
|
||||||
try {
|
try {
|
||||||
String decodeMessage = AesUtil.aesCommonDecoder(WebSocketChannelManager.MESSAGE_AES_KEY, textWebSocketFrame.text());
|
if (webSocketProperties.getEncrypt()) {
|
||||||
clientSocketMessage = JSONObject.parseObject(decodeMessage, WebSocketClientMessage.class);
|
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) {
|
if (clientSocketMessage.getType() == null) {
|
||||||
throw new TypeException("Type类型不能为空");
|
throw new TypeException("Type类型不能为空");
|
||||||
}
|
}
|
||||||
@ -265,4 +271,8 @@ public class WebSocketTextHandler {
|
|||||||
public void setMessageService(IMessageService messageService) {
|
public void setMessageService(IMessageService messageService) {
|
||||||
this.messageService = messageService;
|
this.messageService = messageService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setWebSocketProperties(WebSocketProperties webSocketProperties) {
|
||||||
|
this.webSocketProperties = webSocketProperties;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,11 @@ package ink.wgink.module.instantmessage.websocket.manager;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import ink.wgink.exceptions.websocket.SessionException;
|
import ink.wgink.exceptions.websocket.SessionException;
|
||||||
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
|
|
||||||
import ink.wgink.interfaces.manager.IWebSocketChannelManager;
|
import ink.wgink.interfaces.manager.IWebSocketChannelManager;
|
||||||
|
import ink.wgink.interfaces.manager.IWebSocketUserSessionManager;
|
||||||
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.properties.websocket.WebSocketProperties;
|
||||||
import ink.wgink.util.AesUtil;
|
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;
|
||||||
@ -28,6 +29,7 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
|
|||||||
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 WebSocketProperties webSocketProperties;
|
||||||
|
|
||||||
private WebSocketChannelManager() {
|
private WebSocketChannelManager() {
|
||||||
}
|
}
|
||||||
@ -154,7 +156,13 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
|
|||||||
if (toChannel == null || !toChannel.isOpen() || !toChannel.isActive()) {
|
if (toChannel == null || !toChannel.isOpen() || !toChannel.isActive()) {
|
||||||
return;
|
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);
|
toChannel.writeAndFlush(textWebSocketFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,6 +178,10 @@ public class WebSocketChannelManager implements IWebSocketChannelManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setWebSocketProperties(WebSocketProperties webSocketProperties) {
|
||||||
|
this.webSocketProperties = webSocketProperties;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setWebSocketUserSessionManager(IWebSocketUserSessionManager webSocketUserSessionManager) {
|
public void setWebSocketUserSessionManager(IWebSocketUserSessionManager webSocketUserSessionManager) {
|
||||||
this.webSocketUserSessionManager = webSocketUserSessionManager;
|
this.webSocketUserSessionManager = webSocketUserSessionManager;
|
||||||
|
Loading…
Reference in New Issue
Block a user