瀏覽代碼

1、mqtt访问添加用户名密码;2、登录消息异常捕捉去除

rengb 3 年之前
父節點
當前提交
5d09163c19

+ 4 - 1
message-service/src/main/java/com/lantone/message/config/MqttInboundConfig.java

@@ -7,6 +7,7 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.integration.annotation.ServiceActivator;
 import org.springframework.integration.channel.DirectChannel;
 import org.springframework.integration.core.MessageProducer;
+import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
 import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
 import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
 import org.springframework.messaging.Message;
@@ -25,6 +26,8 @@ public class MqttInboundConfig {
 
     @Autowired
     private MqttConfig mqttConfig;
+    @Autowired
+    private MqttPahoClientFactory mqttPahoClientFactory;
 
     @Bean
     public MessageChannel mqttInputChannel() {
@@ -34,7 +37,7 @@ public class MqttInboundConfig {
     @Bean
     public MessageProducer inbound() {
         MqttPahoMessageDrivenChannelAdapter adapter =
-                new MqttPahoMessageDrivenChannelAdapter(mqttConfig.getUrl(), "subscriberClient",
+                new MqttPahoMessageDrivenChannelAdapter("subscriberClient", mqttPahoClientFactory,
                         mqttConfig.getDefaultTopic());
         adapter.setCompletionTimeout(5000);
         adapter.setConverter(new DefaultPahoMessageConverter());

+ 7 - 0
message-service/src/main/java/com/lantone/message/config/MqttOutboundConfig.java

@@ -3,6 +3,7 @@ package com.lantone.message.config;
 import lombok.extern.slf4j.Slf4j;
 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.integration.annotation.ServiceActivator;
@@ -25,12 +26,18 @@ public class MqttOutboundConfig {
 
     @Autowired
     private MqttConfig mqttConfig;
+    @Value("${spring.rabbitmq.username}")
+    private String username;
+    @Value("${spring.rabbitmq.password}")
+    private String password;
 
     @Bean
     public MqttPahoClientFactory mqttClientFactory() {
         DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
         MqttConnectOptions options = new MqttConnectOptions();
         options.setServerURIs(new String[] { mqttConfig.getUrl() });
+        options.setUserName(username);
+        options.setPassword(password.toCharArray());
         factory.setConnectionOptions(options);
         return factory;
     }

+ 3 - 20
security-center/src/main/java/com/lantone/security/facade/UserManagementFacade.java

@@ -66,7 +66,6 @@ import com.lantone.security.enums.ReturnTypeEnum;
 import com.lantone.security.client.AuthService;
 import com.lantone.security.client.MessageService;
 import eu.bitwalker.useragentutils.UserAgent;
-import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -87,7 +86,6 @@ import java.util.stream.Collectors;
  * @time: 2021/7/20 12:39
  */
 @Component
-@Slf4j
 public class UserManagementFacade {
 
     @Autowired
@@ -152,23 +150,13 @@ public class UserManagementFacade {
                     sendToTopicDTO.setType("login");
                     sendToTopicDTO.setTopic(userId);
                     sendToTopics.add(sendToTopicDTO);
-                    try {
-                        messageService.sendToTopic(sendToTopics);
-                    }catch (Exception e){
-                        e.printStackTrace();
-                        log.error(e.getMessage());
-                    }
+                    messageService.sendToTopic(sendToTopics);
                 }
                 JwtStore jwtStore = new JwtStore();
                 jwtStore.setAccessToken(map.get(AuthConstant.ASSESS_TOKEN).toString());
                 jwtStore.setRefreshToken(map.get(AuthConstant.REFRESH_TOKEN).toString());
                 jwtStore.setIp(HttpUtils.getIpAddress());
-                try {
-                    sysTokenService.createToken(jwtStore);
-                }catch (Exception e){
-                    e.printStackTrace();
-                    log.error(e.getMessage());
-                }
+                sysTokenService.createToken(jwtStore);
             }
         }
     }
@@ -868,12 +856,7 @@ public class UserManagementFacade {
                 // 获取客户端浏览器
                 String browser = userAgent.getBrowser().getName();
                 loginLog.setLoginBrowser(browser);
-                try {
-                    messageService.loginLogHandle(loginLog);
-                }catch (Exception e){
-                    e.printStackTrace();
-                    log.error(e.getMessage());
-                }
+                messageService.loginLogHandle(loginLog);
             }
         }
     }