springboot mqtt 双向认证代码实现

核心代码:

@Bean
 public MqttConnectOptions getMqttConnectOptions() {
  MqttConnectOptions options = new MqttConnectOptions();
  
  Properties sslClientProps = new Properties();
  sslClientProps.setProperty(SSLSocketFactoryFactory.SSLPROTOCOL, mqttProperties.getProtocol());
  sslClientProps.setProperty(SSLSocketFactoryFactory.KEYSTORE, mqttProperties.getKeyStore());
  sslClientProps.setProperty(SSLSocketFactoryFactory.KEYSTOREPWD, mqttProperties.getKeyStorePassword());
  sslClientProps.setProperty(SSLSocketFactoryFactory.KEYSTORETYPE, mqttProperties.getKeyStoreType());
  sslClientProps.setProperty(SSLSocketFactoryFactory.TRUSTSTORE, mqttProperties.getTrustStore());
  sslClientProps.setProperty(SSLSocketFactoryFactory.TRUSTSTOREPWD, mqttProperties.getTrustStorePassword());
  sslClientProps.setProperty(SSLSocketFactoryFactory.TRUSTSTORETYPE, mqttProperties.getTrustStoreType());
  options.setSSLProperties(sslClientProps);
 
  options.setServerURIs(mqttProperties.getHosts());
  options.setCleanSession(mqttProperties.isCleanSession());
  options.setUserName(mqttProperties.getUsername());
  options.setPassword(mqttProperties.getPassword().toCharArray());
  options.setConnectionTimeout(mqttProperties.getConnectionTimeout());
  options.setKeepAliveInterval(mqttProperties.getKeepalive());
  
  options.setWill("willTopic", "I'm offline.".getBytes(), 2, false);
  
  return options;
 }
 
 @Bean
 public MqttPahoClientFactory clientFactory() throws Exception {
  DefaultMqttPahoClientFactory clientFactory = new DefaultMqttPahoClientFactory();
  clientFactory.setConnectionOptions(getMqttConnectOptions());
  return clientFactory;
 }

 

// ---------------------------------订阅--------------------------------------
 @Bean
 public MessageChannel mqttInputChannel() {
  return new DirectChannel();
 }

 @Bean
 public MessageProducer inbound() throws Exception {
  String clientId = mqttProperties.getClientIdRecive() + System.currentTimeMillis();
  MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
           clientId, clientFactory(), mqttProperties.getReciveTopics());
  adapter.setCompletionTimeout(mqttProperties.getCompletionTimeout());
  adapter.setConverter(new DefaultPahoMessageConverter());
  adapter.setQos(1);
  adapter.setOutputChannel(mqttInputChannel());
  return adapter;
 }

@Bean
 @ServiceActivator(inputChannel = "mqttInputChannel")
 public MessageHandler handler() {
  return new MessageHandler() {
   @Override
   public void handleMessage(Message message) throws MessagingException {
    MessageHeaders headers = message.getHeaders();
    headers.forEach((x,y) ->logger.info("header::: " + x + ":" + headers.get(y)));
    String topic = (String) headers.get("mqtt_receivedTopic");
    logger.info("listen topic: " + topic + ", payload: " + message.getPayload());
   }
  };
 }

 

// ----------------------发布------------------------------------
 @Bean
 @ServiceActivator(inputChannel = "mqttOutboundChannel")
 public MessageHandler mqttOutbound() throws Exception {
  String clientId = mqttProperties.getCaCertificate() + System.currentTimeMillis();
  MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, clientFactory());
  messageHandler.setAsync(mqttProperties.isAsync());
  messageHandler.setDefaultTopic(mqttProperties.getDefaultTopic());
  return messageHandler;
 }

 @Bean
 public MessageChannel mqttOutboundChannel() {
  return new DirectChannel();
 }

 @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
 public interface MyGateway {
  void sendToMqtt(String payload, @Header(name = MqttHeaders.TOPIC) String topic);
 }

 

全部源码请参考:https://github.com/yinsl/springdemos/tree/master/mqtt/emq/emq-client1

你可能感兴趣的:(springboot mqtt 双向认证代码实现)