|
|
@ -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并初始化本地集合
|
|
|
|