caixiaofeng пре 10 месеци
родитељ
комит
e815bb738e

+ 4 - 11
flow-common/flow-common-websocket-starter/pom.xml

@@ -19,22 +19,15 @@
     </properties>
 
     <dependencies>
-        <dependency>
-            <groupId>com.flow</groupId>
-            <artifactId>flow-common-core</artifactId>
-            <version>0.0.1-SNAPSHOT</version>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
-        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-websocket</artifactId>
         </dependency>
         <dependency>
-            <groupId>com.fasterxml.jackson.core</groupId>
-            <artifactId>jackson-databind</artifactId>
+            <groupId>com.flow</groupId>
+            <artifactId>flow-common-oauth2-starter</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <optional>true</optional>
         </dependency>
     </dependencies>
 

+ 4 - 12
flow-common/flow-common-websocket-starter/src/main/java/com/flow/common/websocket/configure/WebSocketConfigure.java

@@ -1,8 +1,6 @@
 package com.flow.common.websocket.configure;
 
-import com.flow.common.websocket.core.ClientInboundChannelInterceptor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.context.annotation.Bean;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.messaging.simp.config.ChannelRegistration;
 import org.springframework.messaging.simp.config.MessageBrokerRegistry;
@@ -20,10 +18,11 @@ import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
 
 import java.security.Principal;
 
-@Slf4j
 @Configuration
 @EnableWebSocketMessageBroker
 public class WebSocketConfigure implements WebSocketMessageBrokerConfigurer {
+    @Autowired
+    private ChannelInterceptor channelInterceptor;
 
     /**
      * 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint
@@ -113,14 +112,7 @@ public class WebSocketConfigure implements WebSocketMessageBrokerConfigurer {
     @Override
     public void configureClientInboundChannel(ChannelRegistration registration) {
         // 添加通道拦截器
-        registration.interceptors(clientInboundChannelInterceptor());
+        registration.interceptors(channelInterceptor);
     }
 
-    /**
-     * @return stomp入站通道拦截器
-     */
-    @Bean
-    public ChannelInterceptor clientInboundChannelInterceptor() {
-        return new ClientInboundChannelInterceptor();
-    }
 }

+ 11 - 5
flow-common/flow-common-websocket-starter/src/main/java/com/flow/common/websocket/core/ClientInboundChannelInterceptor.java

@@ -1,7 +1,8 @@
 package com.flow.common.websocket.core;
 
-import com.flow.common.core.util.ApplicationContextUtil;
-import lombok.extern.slf4j.Slf4j;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.Ordered;
 import org.springframework.core.annotation.Order;
 import org.springframework.messaging.Message;
@@ -14,6 +15,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.oauth2.provider.OAuth2Authentication;
 import org.springframework.security.oauth2.provider.token.TokenStore;
+import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 
 import java.util.Objects;
@@ -23,8 +25,12 @@ import java.util.Objects;
  * 客户端入站通道拦截器
  */
 @Order(Ordered.HIGHEST_PRECEDENCE + 99)
-@Slf4j
+@Component
 public class ClientInboundChannelInterceptor implements ChannelInterceptor {
+    private static final Logger log = LoggerFactory.getLogger(ClientInboundChannelInterceptor.class);
+    @Autowired
+    private TokenStore tokenStore;
+
     /**
      * 实际消息发送频道之前调用
      *
@@ -38,7 +44,7 @@ public class ClientInboundChannelInterceptor implements ChannelInterceptor {
         if (Objects.nonNull(accessor)) {
             StompPrincipal user = (StompPrincipal) accessor.getUser();
             if (Objects.nonNull(user)) {
-                OAuth2Authentication auth2Authentication = ApplicationContextUtil.getBean(TokenStore.class).readAuthentication(user.getToken());
+                OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(user.getToken());
                 SecurityContextHolder.getContext().setAuthentication(auth2Authentication);
             }
             if (Objects.nonNull(accessor.getCommand())) {
@@ -48,7 +54,7 @@ public class ClientInboundChannelInterceptor implements ChannelInterceptor {
                     if (!StringUtils.isEmpty(authorization)) {
                         try {
                             String token = authorization.replace("Bearer ", "");
-                            OAuth2Authentication oAuth2Authentication = ApplicationContextUtil.getBean(TokenStore.class).readAuthentication(token);
+                            OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(token);
                             SecurityContextHolder.getContext().setAuthentication(oAuth2Authentication);
                             UserDetails userDetails = (UserDetails) oAuth2Authentication.getPrincipal();
                             // 设置当前访问器的认证用户

+ 16 - 7
flow-common/flow-common-websocket-starter/src/main/java/com/flow/common/websocket/core/StompPrincipal.java

@@ -1,18 +1,11 @@
 package com.flow.common.websocket.core;
 
 
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-
 import java.security.Principal;
 
 /**
  * stomp用户信息
  */
-@Data
-@AllArgsConstructor
-@NoArgsConstructor
 public class StompPrincipal implements Principal {
     /**
      * token
@@ -23,4 +16,20 @@ public class StompPrincipal implements Principal {
      */
     private String name;
 
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
 }