diff --git a/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java b/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java index c9d5611..085d66d 100644 --- a/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java +++ b/ruoyi-modules/hw-mqtt-broker/src/main/java/com/hw/mqtt/auth/MqttAuthHandler.java @@ -8,6 +8,7 @@ import com.ruoyi.common.core.utils.StringUtils; import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerAuthHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.StringRedisTemplate; import org.tio.core.ChannelContext; @@ -34,6 +35,9 @@ public class MqttAuthHandler implements IMqttServerAuthHandler { */ private List deviceInfoDtos; + @Value("${mqtt.whitelist}") + private String clientIdWhitelist; + private final StringRedisTemplate redisTemplate; public MqttAuthHandler(List deviceInfoDtos, StringRedisTemplate redisTemplate) { @@ -58,10 +62,23 @@ public class MqttAuthHandler implements IMqttServerAuthHandler { } } + /** + * 客户端认证逻辑实现 + * @param context ChannelContext + * @param uniqueId mqtt 内唯一id,默认和 clientId 相同 + * @param clientId 客户端 ID + * @param userName 用户名 + * @param password 密码 + * @return + */ @Override public boolean authenticate(ChannelContext context, String uniqueId, String clientId, String userName, String password) { - // 客户端认证逻辑实现 + // try{ + //白名单过滤 + if(clientIdWhitelist.contains(clientId)){ + return true; + } if(deviceInfoDtos!=null){ Optional optionalDeviceInfoDto = deviceInfoDtos.stream().distinct().filter(x -> x.getDeviceCode().equals(clientId)).findFirst(); //判断本地集合中是否包含该设备信息,如果不包含再次读取Redis并初始化本地集合 diff --git a/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml b/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml index 4ba1e7d..94ae11f 100644 --- a/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml +++ b/ruoyi-modules/hw-mqtt-broker/src/main/resources/bootstrap.yml @@ -7,6 +7,8 @@ mqtt: server: port: 1883 # MQTT端口,默认:1883 web-port: 8083 # http、websocket 端口,默认:8083 + #客户端ClientId白名单,不进行接入校验 + whitelist: Test123,Test234,Test256 # Spring spring: