From 7f4d04d888aac1dbd55d8b44cd422979bf4b6a83 Mon Sep 17 00:00:00 2001
From: toesbieya <1647775459@qq.com>
Date: Thu, 30 Jul 2020 15:10:16 +0800
Subject: [PATCH] =?UTF-8?q?websocket=E5=A2=9E=E5=8A=A0ssl=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
java/cloud/pom.xml | 2 +-
.../jxc/socket/config/SocketConfig.java | 34 ++++++++++++++++++-
.../jxc/socket/config/SocketProperty.java | 4 +++
.../jxc/socket/server/WebSocketServer.java | 15 ++++++--
java/local/pom.xml | 2 +-
.../cn/toesbieya/jxc/config/SocketConfig.java | 31 ++++++++++++++++-
.../toesbieya/jxc/config/SocketProperty.java | 4 +++
.../toesbieya/jxc/module/WebSocketServer.java | 16 +++++++--
java/local/src/main/resources/application.yml | 2 ++
9 files changed, 102 insertions(+), 8 deletions(-)
diff --git a/java/cloud/pom.xml b/java/cloud/pom.xml
index 225f0a3..77d29d5 100644
--- a/java/cloud/pom.xml
+++ b/java/cloud/pom.xml
@@ -31,7 +31,7 @@
3.3.2
1.2.12
- 1.7.11
+ 1.7.18
28.1-jre
1.2.60
diff --git a/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketConfig.java b/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketConfig.java
index 4a547e2..0fa517e 100644
--- a/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketConfig.java
+++ b/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketConfig.java
@@ -7,6 +7,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.StringUtils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
@Configuration
@EnableConfigurationProperties(SocketProperty.class)
@@ -15,9 +22,10 @@ public class SocketConfig {
private SocketProperty property;
@Bean
- public SocketIOServer socketIOServer() {
+ public SocketIOServer socketIOServer() throws IOException {
com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
+ setSSL(config);
config.setHostname(property.getHostname());
config.setPort(property.getPort());
config.setMaxFramePayloadLength(property.getMaxFramePayloadLength());
@@ -36,4 +44,28 @@ public class SocketConfig {
public void setSocketProperty(SocketProperty socketProperty) {
this.property = socketProperty;
}
+
+ private void setSSL(com.corundumstudio.socketio.Configuration config) throws IOException {
+ String keyStore = property.getKeyStore();
+
+ if (StringUtils.isEmpty(keyStore)) return;
+
+ InputStream inputStream;
+ String classpathPrefix = "classpath:";
+
+ if (keyStore.startsWith(classpathPrefix)) {
+ inputStream = new ClassPathResource(keyStore.substring(classpathPrefix.length())).getInputStream();
+ }
+ else {
+ inputStream = new FileInputStream(new File(keyStore));
+ }
+
+ config.setKeyStore(inputStream);
+
+ String password = property.getKeyStorePassword();
+
+ if (!StringUtils.isEmpty(password)) {
+ config.setKeyStorePassword(password);
+ }
+ }
}
diff --git a/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketProperty.java b/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketProperty.java
index 21665f1..07ec357 100644
--- a/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketProperty.java
+++ b/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketProperty.java
@@ -12,4 +12,8 @@ public class SocketProperty {
private Integer port = 12580;
private Integer maxFramePayloadLength = 1024 * 1024;
private Integer maxHttpContentLength = 1024 * 1024;
+
+ //ssl证书地址,jks格式,可以是classpath:xxx,也可以是C:\xxx这样的绝对路径
+ private String keyStore;
+ private String keyStorePassword;
}
diff --git a/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/server/WebSocketServer.java b/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/server/WebSocketServer.java
index e56fe40..11528f2 100644
--- a/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/server/WebSocketServer.java
+++ b/java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/server/WebSocketServer.java
@@ -82,7 +82,8 @@ public class WebSocketServer {
@OnConnect
public void onConnect(SocketIOClient client) {
- UserObject obj = new UserObject(client);
+ UserObject obj = UserObject.getInstance(client);
+ if (obj == null) return;
String sessionKey = obj.getKey();
Integer uid = obj.getUid();
@@ -109,7 +110,8 @@ public class WebSocketServer {
@OnDisconnect
public void onDisconnect(SocketIOClient client) {
long now = System.currentTimeMillis();
- UserObject obj = new UserObject(client);
+ UserObject obj = UserObject.getInstance(client);
+ if (obj == null) return;
Integer uid = obj.getUid();
String sessionKey = obj.getKey();
@@ -156,5 +158,14 @@ public class WebSocketServer {
this.uuid = client.getSessionId();
this.key = SessionConstant.REDIS_NAMESPACE + this.token;
}
+
+ public static UserObject getInstance(SocketIOClient client) {
+ try {
+ return new UserObject(client);
+ }
+ catch (Exception e) {
+ return null;
+ }
+ }
}
}
diff --git a/java/local/pom.xml b/java/local/pom.xml
index 9bdc84c..30cc8c4 100644
--- a/java/local/pom.xml
+++ b/java/local/pom.xml
@@ -18,7 +18,7 @@
1.8
2.1.0
1.2.12
- 1.7.11
+ 1.7.18
28.1-jre
1.2.60
diff --git a/java/local/src/main/java/cn/toesbieya/jxc/config/SocketConfig.java b/java/local/src/main/java/cn/toesbieya/jxc/config/SocketConfig.java
index ca2de2d..0e0cc13 100644
--- a/java/local/src/main/java/cn/toesbieya/jxc/config/SocketConfig.java
+++ b/java/local/src/main/java/cn/toesbieya/jxc/config/SocketConfig.java
@@ -7,6 +7,10 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.util.StringUtils;
+
+import java.io.*;
@Configuration
@EnableConfigurationProperties(SocketProperty.class)
@@ -15,9 +19,10 @@ public class SocketConfig {
private SocketProperty property;
@Bean
- public SocketIOServer socketIOServer() {
+ public SocketIOServer socketIOServer() throws IOException {
com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
+ setSSL(config);
config.setHostname(property.getHostname());
config.setPort(property.getPort());
config.setMaxFramePayloadLength(property.getMaxFramePayloadLength());
@@ -36,4 +41,28 @@ public class SocketConfig {
public void setProperty(SocketProperty property) {
this.property = property;
}
+
+ private void setSSL(com.corundumstudio.socketio.Configuration config) throws IOException {
+ String keyStore = property.getKeyStore();
+
+ if (StringUtils.isEmpty(keyStore)) return;
+
+ InputStream inputStream;
+ String classpathPrefix = "classpath:";
+
+ if (keyStore.startsWith(classpathPrefix)) {
+ inputStream = new ClassPathResource(keyStore.substring(classpathPrefix.length())).getInputStream();
+ }
+ else {
+ inputStream = new FileInputStream(new File(keyStore));
+ }
+
+ config.setKeyStore(inputStream);
+
+ String password = property.getKeyStorePassword();
+
+ if (!StringUtils.isEmpty(password)) {
+ config.setKeyStorePassword(password);
+ }
+ }
}
diff --git a/java/local/src/main/java/cn/toesbieya/jxc/config/SocketProperty.java b/java/local/src/main/java/cn/toesbieya/jxc/config/SocketProperty.java
index f00be86..ca564b0 100644
--- a/java/local/src/main/java/cn/toesbieya/jxc/config/SocketProperty.java
+++ b/java/local/src/main/java/cn/toesbieya/jxc/config/SocketProperty.java
@@ -10,4 +10,8 @@ public class SocketProperty {
private Integer port = 12580;
private Integer maxFramePayloadLength = 1024 * 1024;
private Integer maxHttpContentLength = 1024 * 1024;
+
+ //ssl证书地址,jks格式,可以是classpath:xxx,也可以是C:\xxx这样的绝对路径
+ private String keyStore;
+ private String keyStorePassword;
}
diff --git a/java/local/src/main/java/cn/toesbieya/jxc/module/WebSocketServer.java b/java/local/src/main/java/cn/toesbieya/jxc/module/WebSocketServer.java
index 80055fb..bebc116 100644
--- a/java/local/src/main/java/cn/toesbieya/jxc/module/WebSocketServer.java
+++ b/java/local/src/main/java/cn/toesbieya/jxc/module/WebSocketServer.java
@@ -82,7 +82,9 @@ public class WebSocketServer {
@OnConnect
public void onConnect(SocketIOClient client) {
- UserObject obj = new UserObject(client);
+ UserObject obj = UserObject.getInstance(client);
+ if (obj == null) return;
+
String sessionKey = obj.getKey();
Integer uid = obj.getUid();
@@ -109,7 +111,8 @@ public class WebSocketServer {
@OnDisconnect
public void onDisconnect(SocketIOClient client) {
long now = System.currentTimeMillis();
- UserObject obj = new UserObject(client);
+ UserObject obj = UserObject.getInstance(client);
+ if (obj == null) return;
Integer uid = obj.getUid();
String sessionKey = obj.getKey();
@@ -156,5 +159,14 @@ public class WebSocketServer {
this.uuid = client.getSessionId();
this.key = SessionConstant.REDIS_NAMESPACE + this.token;
}
+
+ public static UserObject getInstance(SocketIOClient client) {
+ try {
+ return new UserObject(client);
+ }
+ catch (Exception e) {
+ return null;
+ }
+ }
}
}
diff --git a/java/local/src/main/resources/application.yml b/java/local/src/main/resources/application.yml
index deca6a0..efe2bd4 100644
--- a/java/local/src/main/resources/application.yml
+++ b/java/local/src/main/resources/application.yml
@@ -45,6 +45,8 @@ socket:
port: 12580
max-frame-payload-length: 1048576
max-http-content-length: 1048576
+ #key-store: classpath:toesbieya.cn.jks
+ #key-store-password: PS17wnkE
qiniu:
access-key: