MQTT 4 ——MQTT的Spring Mvc 配置接收字节流数据

本篇记录一下MQTT整合Spring Mvc配置直接收发字节流数据


设备方是纯C开发,并且为了交互数据的安全,将传送的数据用了AES CBC进行了加密。

接下来正常方便做法应该是

将加密后的字节流转换成16进制字符串 或 Base64字符串进行传送;接收方收到字符串后,再转换成字节流进行解密即可。

但是,设备开发方坚持不改,直接把加密后的字节流数据放到MQTT里面传过来了。所以之前配置的SPRING MQTT,就无法正常转换数据了,接收到的字节流不论怎么进行转换都是乱码的格式,无法正常进行AES CBC解密。

解决办法

解决办法也很简单直接,修改原来的Spring-mqtt配置文件,引入一个新的converter,并且配置payloadAsBytes为true即可

与正常的配置文件相比,区别就是在MqttPahoMessageHandler 和 message-driven-channel-adapter 中都引入一个自定义的myConvert;然后配置myConvert bean,直接继承DefaultPahoMessageConverter,设置payloadAsBytes 为ture.

 

下面是详细的spring-mqtt.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:int-mqtt="http://www.springframework.org/schema/integration/mqtt"
       xsi:schemaLocation="
        http://www.springframework.org/schema/integration
        http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/integration/mqtt
        http://www.springframework.org/schema/integration/mqtt/spring-integration-mqtt-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd  ">

    <context:property-placeholder location="classpath:mqtt.properties" ignore-unresolvable="true"/>

    <!--MQTT配置-->
    <bean id="clientFactory"
          class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
        <property name="userName" value="${mqtt.username}"/>
        <property name="password" value="${mqtt.password}"/>
        <property name="cleanSession" value="${mqtt.cleanSession}"/>
        <property name="keepAliveInterval" value="${mqtt.keepAliveInterval}"/>
        <property name="serverURIs">
            <array>
                <value>${mqtt.serverURI1}</value>
            </array>
        </property>
    </bean>

    <bean id="mqttHandler" class="org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler">
        <constructor-arg name="clientId" value="${mqtt.clientId}"/>
        <constructor-arg name="clientFactory" ref="clientFactory"/>
        <property name="converter" ref="myConvert"/>
        <property name="async" value="${mqtt.async}"/>
        <property name="defaultQos" value="${mqtt.defaultQos}"/>
        <property name="completionTimeout" value="${mqtt.completionTimeout}"/>
    </bean>

    <!-- 消息适配器  -->
    <int-mqtt:message-driven-channel-adapter
            id="mqttInbound" client-id="${mqtt.clientId}" url="${mqtt.serverURI1}"
            topics="${mqtt.topic}" qos="${mqtt.defaultQos}" client-factory="clientFactory"  auto-startup="true"
            converter="myConvert"
            send-timeout="${mqtt.completionTimeout}" channel="startCase" />
    <int:channel id="startCase" />
    <!-- 消息处理类 -->
    <int:service-activator id="handlerService"
                           input-channel="startCase" ref="mqttCaseService" method="handler" />

    <bean id="myConvert" class="org.springframework.integration.mqtt.support.DefaultPahoMessageConverter">
        <property name="payloadAsBytes" value="true" />
    </bean>

    <!-- 消息处理 -->
    <bean id="mqttCaseService" class="com.loong.mqtt.service.impl.MqttServiceImpl" />

</beans>

 

posted @ 2020-05-21 09:47  jiansword  阅读(1587)  评论(2编辑  收藏  举报