websocket增加ssl配置

master
toesbieya 6 years ago
parent c5cf48a739
commit 7f4d04d888
  1. 2
      java/cloud/pom.xml
  2. 34
      java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketConfig.java
  3. 4
      java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/config/SocketProperty.java
  4. 15
      java/cloud/websocket/src/main/java/cn/toesbieya/jxc/socket/server/WebSocketServer.java
  5. 2
      java/local/pom.xml
  6. 31
      java/local/src/main/java/cn/toesbieya/jxc/config/SocketConfig.java
  7. 4
      java/local/src/main/java/cn/toesbieya/jxc/config/SocketProperty.java
  8. 16
      java/local/src/main/java/cn/toesbieya/jxc/module/WebSocketServer.java
  9. 2
      java/local/src/main/resources/application.yml

@ -31,7 +31,7 @@
<mybatis-plus.version>3.3.2</mybatis-plus.version>
<pagehelper.version>1.2.12</pagehelper.version>
<socketio.version>1.7.11</socketio.version>
<socketio.version>1.7.18</socketio.version>
<guava.version>28.1-jre</guava.version>
<fastjson.version>1.2.60</fastjson.version>

@ -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);
}
}
}

@ -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;
}

@ -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;
}
}
}
}

@ -18,7 +18,7 @@
<java.version>1.8</java.version>
<mybatis.version>2.1.0</mybatis.version>
<pagehelper.version>1.2.12</pagehelper.version>
<socketio.version>1.7.11</socketio.version>
<socketio.version>1.7.18</socketio.version>
<guava.version>28.1-jre</guava.version>
<fastjson.version>1.2.60</fastjson.version>

@ -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);
}
}
}

@ -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;
}

@ -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;
}
}
}
}

@ -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:

Loading…
Cancel
Save