|
21 | 21 | import io.netty.handler.codec.mqtt.MqttQoS; |
22 | 22 | import io.netty.handler.ssl.SslContext; |
23 | 23 | import io.netty.handler.ssl.SslContextBuilder; |
| 24 | +import io.netty.handler.ssl.SslProvider; |
24 | 25 | import io.netty.util.concurrent.Future; |
25 | 26 | import lombok.extern.slf4j.Slf4j; |
26 | 27 | import org.springframework.beans.factory.annotation.Value; |
27 | 28 | import org.thingsboard.mqtt.MqttClient; |
28 | 29 | import org.thingsboard.mqtt.MqttClientConfig; |
29 | 30 | import org.thingsboard.mqtt.MqttConnectResult; |
| 31 | +import org.thingsboard.server.common.data.StringUtils; |
30 | 32 | import org.thingsboard.tools.service.mqtt.DeviceClient; |
31 | 33 | import org.thingsboard.tools.service.msg.Msg; |
32 | 34 |
|
@@ -195,19 +197,26 @@ private MqttClient initClient(String token) throws Exception { |
195 | 197 |
|
196 | 198 | private SslContext getSslContext() { |
197 | 199 | if (mqttSslEnabled) { |
| 200 | + if (StringUtils.isNotBlank(mqttSslKeyStore)) { |
| 201 | + try { |
| 202 | + TrustManagerFactory trustFact = TrustManagerFactory.getInstance("SunX509"); |
| 203 | + KeyStore trustStore = KeyStore.getInstance("JKS"); |
| 204 | + FileInputStream stream = new FileInputStream(mqttSslKeyStore); |
| 205 | + trustStore.load(stream, mqttSslKeyStorePassword.toCharArray()); |
| 206 | + trustFact.init(trustStore); |
| 207 | + return SslContextBuilder.forClient().trustManager(trustFact).build(); |
| 208 | + } catch (Exception e) { |
| 209 | + log.warn("Error while initializing SSL context for keystore [{}]. Will try default SSLContext", mqttSslKeyStore, e); |
| 210 | + } |
| 211 | + } |
| 212 | + |
198 | 213 | try { |
199 | | - TrustManagerFactory trustFact = TrustManagerFactory.getInstance("SunX509"); |
200 | | - KeyStore trustStore = KeyStore.getInstance("JKS"); |
201 | | - FileInputStream stream = new FileInputStream(mqttSslKeyStore); |
202 | | - trustStore.load(stream, mqttSslKeyStorePassword.toCharArray()); |
203 | | - trustFact.init(trustStore); |
204 | | - return SslContextBuilder.forClient().trustManager(trustFact).build(); |
| 214 | + return SslContextBuilder.forClient().sslProvider(SslProvider.JDK).build(); |
205 | 215 | } catch (Exception e) { |
206 | | - throw new RuntimeException("Exception while creating SslContext", e); |
| 216 | + throw new RuntimeException("Error while initializing default SSL context", e); |
207 | 217 | } |
208 | | - } else { |
209 | | - return null; |
210 | 218 | } |
| 219 | + return null; |
211 | 220 | } |
212 | 221 |
|
213 | 222 | protected void reportMqttClientsStats() { |
|
0 commit comments