Jelajahi Sumber

:sparkles: 新增webSocket

396264893 6 tahun lalu
induk
melakukan
b002c09add

+ 5 - 0
pigx-auth/pom.xml

@@ -72,6 +72,11 @@
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-freemarker</artifactId>
 		</dependency>
+		<!--websocket-->
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-websocket</artifactId>
+		</dependency>
 		<!--spring security 、oauth、jwt依赖-->
 		<dependency>
 			<groupId>org.springframework.cloud</groupId>

+ 1 - 1
pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java

@@ -69,7 +69,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
 			.antMatchers(
 				"/token/**",
 				"/actuator/**",
-				"/mobile/**").permitAll()
+				"/mobile/**","/ws/**").permitAll()
 			.anyRequest().authenticated()
 			.and().csrf().disable()
 			.apply(mobileSecurityConfigurer());

+ 23 - 0
pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSocketConfig.java

@@ -0,0 +1,23 @@
+package com.pig4cloud.pigx.auth.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.messaging.simp.config.MessageBrokerRegistry;
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
+
+@Configuration
+@EnableWebSocketMessageBroker
+public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
+    @Override
+    public void registerStompEndpoints(StompEndpointRegistry registry) {
+        registry.addEndpoint("/ws")
+                .setAllowedOrigins("*")
+                .withSockJS();
+    }
+    @Override
+    public void configureMessageBroker(MessageBrokerRegistry registry) {
+        //registry.enableSimpleBroker("/loginToAll");
+        registry.setUserDestinationPrefix("/user/");
+    }
+}

+ 10 - 1
pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java

@@ -18,7 +18,10 @@
 package com.pig4cloud.pigx.auth.handler;
 
 import com.pig4cloud.pigx.common.security.handler.AbstractAuthenticationSuccessEventHandler;
+import com.pig4cloud.pigx.common.security.service.PigxUser;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.messaging.simp.SimpMessagingTemplate;
 import org.springframework.security.core.Authentication;
 import org.springframework.stereotype.Component;
 
@@ -30,6 +33,9 @@ import org.springframework.stereotype.Component;
 @Component
 public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler {
 
+	@Autowired
+	SimpMessagingTemplate simpMessagingTemplate;
+
 	/**
 	 * 处理登录成功方法
 	 * <p>
@@ -39,6 +45,9 @@ public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticatio
 	 */
 	@Override
 	public void handle(Authentication authentication) {
-		log.info("用户:{} 登录成功", authentication.getPrincipal());
+		Object principal = authentication.getPrincipal();
+		log.info("用户:{} 登录成功", principal);
+		PigxUser pigxUser = (PigxUser) principal;
+		simpMessagingTemplate.convertAndSendToUser(pigxUser.getTenantId().toString(),"/loginToAll",pigxUser.getUsername());
 	}
 }