add - broker客户端接入白名单,yml中进行配置

dev
Wen JY 1 year ago
parent 656c9b47a3
commit c8cc19330d

@ -8,6 +8,7 @@ import com.ruoyi.common.core.utils.StringUtils;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerAuthHandler; import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerAuthHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.tio.core.ChannelContext; import org.tio.core.ChannelContext;
@ -34,6 +35,9 @@ public class MqttAuthHandler implements IMqttServerAuthHandler {
*/ */
private List<DeviceInfoDto> deviceInfoDtos; private List<DeviceInfoDto> deviceInfoDtos;
@Value("${mqtt.whitelist}")
private String clientIdWhitelist;
private final StringRedisTemplate redisTemplate; private final StringRedisTemplate redisTemplate;
public MqttAuthHandler(List<DeviceInfoDto> deviceInfoDtos, StringRedisTemplate redisTemplate) { public MqttAuthHandler(List<DeviceInfoDto> 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 @Override
public boolean authenticate(ChannelContext context, String uniqueId, String clientId, String userName, String password) { public boolean authenticate(ChannelContext context, String uniqueId, String clientId, String userName, String password) {
// 客户端认证逻辑实现 //
try{ try{
//白名单过滤
if(clientIdWhitelist.contains(clientId)){
return true;
}
if(deviceInfoDtos!=null){ if(deviceInfoDtos!=null){
Optional<DeviceInfoDto> optionalDeviceInfoDto = deviceInfoDtos.stream().distinct().filter(x -> x.getDeviceCode().equals(clientId)).findFirst(); Optional<DeviceInfoDto> optionalDeviceInfoDto = deviceInfoDtos.stream().distinct().filter(x -> x.getDeviceCode().equals(clientId)).findFirst();
//判断本地集合中是否包含该设备信息如果不包含再次读取Redis并初始化本地集合 //判断本地集合中是否包含该设备信息如果不包含再次读取Redis并初始化本地集合

@ -7,6 +7,8 @@ mqtt:
server: server:
port: 1883 # MQTT端口默认1883 port: 1883 # MQTT端口默认1883
web-port: 8083 # http、websocket 端口默认8083 web-port: 8083 # http、websocket 端口默认8083
#客户端ClientId白名单不进行接入校验
whitelist: Test123,Test234,Test256
# Spring # Spring
spring: spring:

Loading…
Cancel
Save