提交 cdb7fd9e 编写于 作者: 浅梦2013's avatar 浅梦2013

ssl 支持双向认证

上级 3da39eac
......@@ -18,5 +18,10 @@ mqtt:
buffer-allocator: heap # 堆内存和堆外内存,默认:堆内存
keep-alive-secs: 60 # keep-alive 时间,单位:秒
clean-session: true # mqtt clean session,默认:true
use-ssl: false # 是否启用 ssl,默认:false
ssl:
enabled: false # 是否开启 ssl 认证,2.1.0 开始支持双向认证
keystore-path: # 可选参数:ssl 双向认证 keystore 目录,支持 classpath:/ 路径。
keystore-pass: # 可选参数:ssl 双向认证 keystore 密码
truststore-path: # 可选参数:ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
truststore-pass: # 可选参数:ssl 双向认证 truststore 密码
topic1: /test2/#
......@@ -23,3 +23,10 @@ mqtt:
enable: false # 是否开启 http basic auth,默认: false
username: mica # http basic auth 用户名
password: mica # http basic auth 密码
ssl: # mqtt tcp ssl 认证
enabled: false # 是否开启 ssl 认证,2.1.0 开始支持双向认证
keystore-path: # 必须参数:ssl keystore 目录,支持 classpath:/ 路径。
keystore-pass: # 必选参数:ssl keystore 密码
truststore-path: # 可选参数:ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
truststore-pass: # 可选参数:ssl 双向认证 truststore 密码
client-auth: none # 是否需要客户端认证(双向认证),默认:NONE(不需要)
......@@ -33,6 +33,7 @@ import org.tio.utils.thread.pool.SynThreadPoolExecutor;
import org.tio.utils.timer.DefaultTimerTaskService;
import org.tio.utils.timer.TimerTaskService;
import java.io.InputStream;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
......@@ -344,12 +345,23 @@ public final class MqttClientCreator {
}
public MqttClientCreator useSsl() {
try {
this.sslConfig = SslConfig.forClient();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return this;
return sslConfig(SslConfig.forClient());
}
public MqttClientCreator useSsl(String trustStoreFile, String trustPassword) {
return sslConfig(SslConfig.forClient(trustStoreFile, trustPassword));
}
public MqttClientCreator useSsl(String keyStoreFile, String keyPasswd, String trustStoreFile, String trustPassword) {
return sslConfig(SslConfig.forClient(keyStoreFile, keyPasswd, trustStoreFile, trustPassword));
}
public MqttClientCreator useSsl(InputStream trustStoreInputStream, String trustPassword) {
return sslConfig(SslConfig.forClient(trustStoreInputStream, trustPassword));
}
public MqttClientCreator useSsl(InputStream keyStoreInputStream, String keyPasswd, InputStream trustStoreInputStream, String trustPassword) {
return sslConfig(SslConfig.forClient(keyStoreInputStream, keyPasswd, trustStoreInputStream, trustPassword));
}
public MqttClientCreator sslConfig(SslConfig sslConfig) {
......
......@@ -38,7 +38,12 @@ mqtt:
buffer-allocator: heap # 堆内存和堆外内存,默认:堆内存
keep-alive-secs: 60 # keep-alive 时间,单位:秒
clean-session: true # mqtt clean session,默认:true
use-ssl: false # 是否启用 ssl,默认:false
ssl:
enabled: false # 是否开启 ssl 认证,2.1.0 开始支持双向认证
keystore-path: # 可选参数:ssl 双向认证 keystore 目录,支持 classpath:/ 路径。
keystore-pass: # 可选参数:ssl 双向认证 keystore 密码
truststore-path: # 可选参数:ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
truststore-pass: # 可选参数:ssl 双向认证 truststore 密码
```
### 2.2 可实现接口(注册成 Spring Bean 即可)
......
......@@ -88,8 +88,9 @@ public class MqttClientConfiguration {
clientCreator.debug();
}
// 开启 ssl
if (properties.isUseSsl()) {
clientCreator.useSsl();
MqttClientProperties.Ssl ssl = properties.getSsl();
if (ssl.isEnabled()) {
clientCreator.useSsl(ssl.getKeystorePath(), ssl.getKeystorePass(), ssl.getTruststorePath(), ssl.getKeystorePass());
}
// 构造遗嘱消息
MqttClientProperties.WillMessage willMessage = properties.getWillMessage();
......
......@@ -132,9 +132,9 @@ public class MqttClientProperties {
*/
private boolean debug = false;
/**
* 开启 ssl
* ssl 配置
*/
private boolean useSsl = false;
private Ssl ssl = new Ssl();
@Getter
@Setter
......@@ -157,4 +157,29 @@ public class MqttClientProperties {
private boolean retain = false;
}
@Getter
@Setter
public static class Ssl {
/**
* 启用 ssl
*/
private boolean enabled = false;
/**
* keystore 证书路径
*/
private String keystorePath;
/**
* keystore 密码
*/
private String keystorePass;
/**
* truststore 证书路径
*/
private String truststorePath;
/**
* truststore 密码
*/
private String truststorePass;
}
}
......@@ -44,6 +44,13 @@ mqtt:
enable: false # 是否开启 http basic auth,默认: false
username: mica # http basic auth 用户名
password: mica # http basic auth 密码
ssl: # mqtt tcp ssl 认证
enabled: false # 是否开启 ssl 认证,2.1.0 开始支持双向认证
keystore-path: # 必须参数:ssl keystore 目录,支持 classpath:/ 路径。
keystore-pass: # 必选参数:ssl keystore 密码
truststore-path: # 可选参数:ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
truststore-pass: # 可选参数:ssl 双向认证 truststore 密码
client-auth: none # 是否需要客户端认证(双向认证),默认:NONE(不需要)
```
### 2.2 可实现接口(注册成 Spring Bean 即可)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册