-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15c54f4
commit e99c99c
Showing
7 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/ssl/SslMqttClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & dreamlu.net). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.iot.mqtt.ssl; | ||
|
||
import net.dreamlu.iot.mqtt.core.client.MqttClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.tio.utils.buffer.ByteBufferUtil; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* 客户端测试 | ||
* | ||
* @author L.cm | ||
*/ | ||
public class SslMqttClientTest { | ||
private static final Logger logger = LoggerFactory.getLogger(SslMqttClientTest.class); | ||
|
||
public static void main(String[] args) { | ||
// 初始化 mqtt 客户端 | ||
MqttClient client = MqttClient.create() | ||
.ip("127.0.0.1") | ||
.port(1883) | ||
.username("mica") | ||
.password("mica") | ||
.useSsl("classpath:ssl/dreamlu.net.jks", "123456") | ||
.connect(); | ||
|
||
client.subQos0("/test/#", (context, topic, message, payload) -> { | ||
logger.info(topic + '\t' + ByteBufferUtil.toString(payload)); | ||
}); | ||
|
||
// 定时发送数据 | ||
client.schedule(() -> { | ||
String message = "mica最牛皮 " + System.currentTimeMillis(); | ||
client.publish("/test/123", message.getBytes(StandardCharsets.UTF_8)); | ||
}, 5000); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
example/mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/ssl/SslMqttServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & dreamlu.net). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.iot.mqtt.ssl; | ||
|
||
import net.dreamlu.iot.mqtt.core.server.MqttServer; | ||
import net.dreamlu.iot.mqtt.server.MqttConnectStatusListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.tio.utils.buffer.ByteBufferUtil; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* mqtt 服务端测试 | ||
* | ||
* @author L.cm | ||
*/ | ||
public class SslMqttServerTest { | ||
private static final Logger logger = LoggerFactory.getLogger(SslMqttServerTest.class); | ||
|
||
public static void main(String[] args) { | ||
MqttServer mqttServer = MqttServer.create() | ||
.port(1883) | ||
.useSsl("classpath:ssl/dreamlu.net.jks", "123456") | ||
.messageListener((context, clientId, topic, qoS, message) -> { | ||
logger.info("clientId:{} message:{} payload:{}", clientId, message, ByteBufferUtil.toString(message.getPayload())); | ||
}) | ||
.connectStatusListener(new MqttConnectStatusListener()) | ||
.debug() | ||
.start(); | ||
|
||
// 定时发送数据 | ||
mqttServer.schedule(() -> { | ||
String message = "mica最牛皮 " + System.currentTimeMillis(); | ||
mqttServer.publishAll("/test/123", message.getBytes(StandardCharsets.UTF_8)); | ||
}, 5000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ssl 自签双向证书详见:https://gitee.com/596392912/mica-mqtt/issues/I45GO7 |
16 changes: 16 additions & 0 deletions
16
example/mica-mqtt-example/src/main/resources/ssl/ca-cert.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIICejCCAeMCFGkzqgl8ilICj+PRCwIiUy3dPNuWMA0GCSqGSIb3DQEBCwUAMHwx | ||
CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJITjELMAkGA1UEBwwCQ1MxDTALBgNVBAoM | ||
BFJNSlMxDTALBgNVBAsMBFJNSlMxFDASBgNVBAMMC2RyZWFtbHUubmV0MR8wHQYJ | ||
KoZIhvcNAQkBFhA1OTYzOTI5MTJAcXEuY29tMB4XDTIyMDkwMzA4MjExNVoXDTMy | ||
MDgzMTA4MjExNVowfDELMAkGA1UEBhMCQ04xCzAJBgNVBAgMAkhOMQswCQYDVQQH | ||
DAJDUzENMAsGA1UECgwEUk1KUzENMAsGA1UECwwEUk1KUzEUMBIGA1UEAwwLZHJl | ||
YW1sdS5uZXQxHzAdBgkqhkiG9w0BCQEWEDU5NjM5MjkxMkBxcS5jb20wgZ8wDQYJ | ||
KoZIhvcNAQEBBQADgY0AMIGJAoGBAJxanDadRHd+D9jH5/mq7Pn3Fl915CiOaaJr | ||
QS0ZxCqRtTaS9+JkFQJ4TnAezlKx/xDZu7G9pk7CJ6w+JQwfI+AwsAkOlrynyFbe | ||
Hc9s6DZyZHXxkpgeQUqpnrkXkWG+jbh2aulWB4smQE/vPnpkjVGEe86+/JsYW3Sm | ||
rFhL2xddAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAP39PM0XpNzg3Bne63oXHRWDJ | ||
bafiCwloO/SaxH7JtCBj1I05W3owwHSqWYadK+lg//tKf6TL+94GtW8s2VtpLGu7 | ||
Y5R2aakhywzbWVfrEK+kyvTG/4nP9tvcKwh8Iqr2XlllDLfsQeyLacb4+pcfDY3G | ||
8nrbe0lBKepae8D0SvQ= | ||
-----END CERTIFICATE----- |
16 changes: 16 additions & 0 deletions
16
example/mica-mqtt-example/src/main/resources/ssl/client-cert.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIICejCCAeMCFBebamYN4nfF0CrZWuN68SXxTH/GMA0GCSqGSIb3DQEBCwUAMHwx | ||
CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJITjELMAkGA1UEBwwCQ1MxDTALBgNVBAoM | ||
BFJNSlMxDTALBgNVBAsMBFJNSlMxFDASBgNVBAMMC2RyZWFtbHUubmV0MR8wHQYJ | ||
KoZIhvcNAQkBFhA1OTYzOTI5MTJAcXEuY29tMB4XDTIyMDkwMzA4MjM1N1oXDTMy | ||
MDgzMTA4MjM1N1owfDELMAkGA1UEBhMCQ04xCzAJBgNVBAgMAkhOMQswCQYDVQQH | ||
DAJDUzENMAsGA1UECgwEUk1KUzENMAsGA1UECwwEUk1KUzEUMBIGA1UEAwwLZHJl | ||
YW1sdS5uZXQxHzAdBgkqhkiG9w0BCQEWEDU5NjM5MjkxMkBxcS5jb20wgZ8wDQYJ | ||
KoZIhvcNAQEBBQADgY0AMIGJAoGBAL3hH0vRYCsqs79ghUPskgr+bQyVkO/6U1H1 | ||
4yHJWyWeXT2tl+VTvXhou0Zk3yYtd6O3l241PgmAjwBDPyfmM2UU/zluXQzELEuf | ||
AnC4Q2wl0vpoj7mGQeMuRpjZN3FblS0bRifvctx3ubWVvSzCFIaKLZgdrnyWm5Lm | ||
kJQjRaEDAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAkm/Mv9nEGE4xy3L4NmwgyZMo | ||
j5CuarbCEv/MgXST8B/dJvtC7PbCzrb9BbUkiIUjCOnRC+2U0DbSAkHy7CCwHTLx | ||
g9Syn1SMWn9SF74l74qVtuaRjMKCGwSlqBLH5/t1FdPoevY2a+zi5dAqwIhkjY84 | ||
uYOgxODe+kFBUbT6W3A= | ||
-----END CERTIFICATE----- |
15 changes: 15 additions & 0 deletions
15
example/mica-mqtt-example/src/main/resources/ssl/client-key.pem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIICXQIBAAKBgQC94R9L0WArKrO/YIVD7JIK/m0MlZDv+lNR9eMhyVslnl09rZfl | ||
U714aLtGZN8mLXejt5duNT4JgI8AQz8n5jNlFP85bl0MxCxLnwJwuENsJdL6aI+5 | ||
hkHjLkaY2TdxW5UtG0Yn73Lcd7m1lb0swhSGii2YHa58lpuS5pCUI0WhAwIDAQAB | ||
AoGAR/T8azsZesJgA/KMDkWkws3QfahgmNEAqlrIjJFGHWd6ZllW6u1lLDBkaDTp | ||
7AnnAQAePwGmVOuHRc42LOSsLMX/D2wYsTGjLTT1w+fEDkQCVnDKV8ZYWA5fN7Zh | ||
m5cLB5IB23L/Xvs8UMYQ8qWufv6BxVPr+cawtXOK3O91AoECQQD5PwGSrMpsNkVG | ||
Ox1t1A9wVgelbq+9qANPl8TvdaZKvNsbWwI0PRqK7D1SJXJiMX4m/hc8YkIXJWsO | ||
Wd1yLg6TAkEAwwZLncJYlYOp0v8PXk75rDR7KV9fdBPPOdnZ1XU/yFW3anwOt0OX | ||
LvGL3X0A2f/tqOX9v+3LENCICF4gAv850QJBALo7Yal+giEoy8oWEX8mnAKLxVrO | ||
wXEsQI0QEY36ki31vqFJ9vOhVFvI+GiQok7MPD5WTHZJ1KgGxV8LtnLCBxECQCPD | ||
WcZ6RyhT1qaco0LWFK7hiNxTYvu0TkH7kxizwZiJL3NVgJVWzbiMDuv06l0Ps5NP | ||
abLydlSFCQ0PxasHBqECQQC0lhmqkNbaN3lJyGDoEWm6Kb9z3eh3+9Fk4j328aYW | ||
gQcBeRhKU8kdDTg2flOWS3sxrEysJYv8i9DPbX9RsRFd | ||
-----END RSA PRIVATE KEY----- |
Binary file not shown.