Browse Source

token里面加入user_id,资源服务器解析

gaodm 6 years ago
parent
commit
5bc3e418f6

+ 19 - 0
bi-service/src/main/java/com/diagbot/config/CustomAccessTokenConverter.java

@@ -0,0 +1,19 @@
+package com.diagbot.config;
+
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
+import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
+
+    @Override
+    public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
+        OAuth2Authentication authentication = super.extractAuthentication(claims);
+        authentication.setDetails(claims);
+        return authentication;
+    }
+
+}

+ 3 - 2
bi-service/src/main/java/com/diagbot/config/JwtConfiguration.java

@@ -21,14 +21,14 @@ import java.io.IOException;
 @Configuration
 public class JwtConfiguration {
     @Autowired
-    JwtAccessTokenConverter jwtAccessTokenConverter;
+    private CustomAccessTokenConverter customAccessTokenConverter;
 
     @Bean
     @Qualifier("tokenStore")
     public TokenStore tokenStore() {
 
         System.out.println("Created JwtTokenStore");
-        return new JwtTokenStore(jwtAccessTokenConverter);
+        return new JwtTokenStore(jwtTokenEnhancer());
     }
 
     @Bean
@@ -42,6 +42,7 @@ public class JwtConfiguration {
             throw new RuntimeException(e);
         }
         converter.setVerifierKey(publicKey);
+        converter.setAccessTokenConverter(customAccessTokenConverter);
         return converter;
     }
 }

+ 13 - 1
bi-service/src/main/java/com/diagbot/util/UserUtils.java

@@ -4,8 +4,10 @@ package com.diagbot.util;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 用户工具类
@@ -25,13 +27,23 @@ public class UserUtils {
     }
 
     /**
-     * 获取当前请求的用户Id
+     * 获取当前请求的用户名称
      * @return
      */
     public static String getCurrentPrinciple() {
         return (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     }
 
+    /**
+     * 获取当前请求的用户ID
+     * @return
+     */
+    public static String getCurrentPrincipleID() {
+        OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
+        Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
+        return details.get("user_id").toString();
+    }
+
     /**
      * 判读当前token用户是否为接口所需的参数username
      *

+ 19 - 0
diagbotman-service/src/main/java/com/diagbot/config/CustomAccessTokenConverter.java

@@ -0,0 +1,19 @@
+package com.diagbot.config;
+
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
+import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
+
+    @Override
+    public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
+        OAuth2Authentication authentication = super.extractAuthentication(claims);
+        authentication.setDetails(claims);
+        return authentication;
+    }
+
+}

+ 3 - 2
diagbotman-service/src/main/java/com/diagbot/config/JwtConfiguration.java

@@ -21,14 +21,14 @@ import java.io.IOException;
 @Configuration
 public class JwtConfiguration {
     @Autowired
-    JwtAccessTokenConverter jwtAccessTokenConverter;
+    private CustomAccessTokenConverter customAccessTokenConverter;
 
     @Bean
     @Qualifier("tokenStore")
     public TokenStore tokenStore() {
 
         System.out.println("Created JwtTokenStore");
-        return new JwtTokenStore(jwtAccessTokenConverter);
+        return new JwtTokenStore(jwtTokenEnhancer());
     }
 
     @Bean
@@ -42,6 +42,7 @@ public class JwtConfiguration {
             throw new RuntimeException(e);
         }
         converter.setVerifierKey(publicKey);
+        converter.setAccessTokenConverter(customAccessTokenConverter);
         return converter;
     }
 }

+ 13 - 1
diagbotman-service/src/main/java/com/diagbot/util/UserUtils.java

@@ -4,8 +4,10 @@ package com.diagbot.util;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 用户工具类
@@ -25,13 +27,23 @@ public class UserUtils {
     }
 
     /**
-     * 获取当前请求的用户Id
+     * 获取当前请求的用户名称
      * @return
      */
     public static String getCurrentPrinciple() {
         return (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     }
 
+    /**
+     * 获取当前请求的用户ID
+     * @return
+     */
+    public static String getCurrentPrincipleID() {
+        OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
+        Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
+        return details.get("user_id").toString();
+    }
+
     /**
      * 判读当前token用户是否为接口所需的参数username
      *

+ 19 - 0
feedback-service/src/main/java/com/diagbot/config/CustomAccessTokenConverter.java

@@ -0,0 +1,19 @@
+package com.diagbot.config;
+
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
+import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
+
+    @Override
+    public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
+        OAuth2Authentication authentication = super.extractAuthentication(claims);
+        authentication.setDetails(claims);
+        return authentication;
+    }
+
+}

+ 3 - 2
feedback-service/src/main/java/com/diagbot/config/JwtConfiguration.java

@@ -21,14 +21,14 @@ import java.io.IOException;
 @Configuration
 public class JwtConfiguration {
     @Autowired
-    JwtAccessTokenConverter jwtAccessTokenConverter;
+    private CustomAccessTokenConverter customAccessTokenConverter;
 
     @Bean
     @Qualifier("tokenStore")
     public TokenStore tokenStore() {
 
         System.out.println("Created JwtTokenStore");
-        return new JwtTokenStore(jwtAccessTokenConverter);
+        return new JwtTokenStore(jwtTokenEnhancer());
     }
 
     @Bean
@@ -42,6 +42,7 @@ public class JwtConfiguration {
             throw new RuntimeException(e);
         }
         converter.setVerifierKey(publicKey);
+        converter.setAccessTokenConverter(customAccessTokenConverter);
         return converter;
     }
 }

+ 13 - 1
feedback-service/src/main/java/com/diagbot/util/UserUtils.java

@@ -4,8 +4,10 @@ package com.diagbot.util;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 用户工具类
@@ -25,13 +27,23 @@ public class UserUtils {
     }
 
     /**
-     * 获取当前请求的用户Id
+     * 获取当前请求的用户名称
      * @return
      */
     public static String getCurrentPrinciple() {
         return (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     }
 
+    /**
+     * 获取当前请求的用户ID
+     * @return
+     */
+    public static String getCurrentPrincipleID() {
+        OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
+        Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
+        return details.get("user_id").toString();
+    }
+
     /**
      * 判读当前token用户是否为接口所需的参数username
      *

+ 19 - 0
knowledge-service/src/main/java/com/diagbot/config/CustomAccessTokenConverter.java

@@ -0,0 +1,19 @@
+package com.diagbot.config;
+
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
+import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
+
+    @Override
+    public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
+        OAuth2Authentication authentication = super.extractAuthentication(claims);
+        authentication.setDetails(claims);
+        return authentication;
+    }
+
+}

+ 3 - 2
knowledge-service/src/main/java/com/diagbot/config/JwtConfiguration.java

@@ -21,14 +21,14 @@ import java.io.IOException;
 @Configuration
 public class JwtConfiguration {
     @Autowired
-    JwtAccessTokenConverter jwtAccessTokenConverter;
+    private CustomAccessTokenConverter customAccessTokenConverter;
 
     @Bean
     @Qualifier("tokenStore")
     public TokenStore tokenStore() {
 
         System.out.println("Created JwtTokenStore");
-        return new JwtTokenStore(jwtAccessTokenConverter);
+        return new JwtTokenStore(jwtTokenEnhancer());
     }
 
     @Bean
@@ -42,6 +42,7 @@ public class JwtConfiguration {
             throw new RuntimeException(e);
         }
         converter.setVerifierKey(publicKey);
+        converter.setAccessTokenConverter(customAccessTokenConverter);
         return converter;
     }
 }

+ 13 - 1
knowledge-service/src/main/java/com/diagbot/util/UserUtils.java

@@ -4,8 +4,10 @@ package com.diagbot.util;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 用户工具类
@@ -25,13 +27,23 @@ public class UserUtils {
     }
 
     /**
-     * 获取当前请求的用户Id
+     * 获取当前请求的用户名称
      * @return
      */
     public static String getCurrentPrinciple() {
         return (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     }
 
+    /**
+     * 获取当前请求的用户ID
+     * @return
+     */
+    public static String getCurrentPrincipleID() {
+        OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
+        Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
+        return details.get("user_id").toString();
+    }
+
     /**
      * 判读当前token用户是否为接口所需的参数username
      *

+ 19 - 0
log-service/src/main/java/com/diagbot/config/CustomAccessTokenConverter.java

@@ -0,0 +1,19 @@
+package com.diagbot.config;
+
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
+import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {
+
+    @Override
+    public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
+        OAuth2Authentication authentication = super.extractAuthentication(claims);
+        authentication.setDetails(claims);
+        return authentication;
+    }
+
+}

+ 3 - 2
log-service/src/main/java/com/diagbot/config/JwtConfiguration.java

@@ -21,14 +21,14 @@ import java.io.IOException;
 @Configuration
 public class JwtConfiguration {
     @Autowired
-    JwtAccessTokenConverter jwtAccessTokenConverter;
+    private CustomAccessTokenConverter customAccessTokenConverter;
 
     @Bean
     @Qualifier("tokenStore")
     public TokenStore tokenStore() {
 
         System.out.println("Created JwtTokenStore");
-        return new JwtTokenStore(jwtAccessTokenConverter);
+        return new JwtTokenStore(jwtTokenEnhancer());
     }
 
     @Bean
@@ -42,6 +42,7 @@ public class JwtConfiguration {
             throw new RuntimeException(e);
         }
         converter.setVerifierKey(publicKey);
+        converter.setAccessTokenConverter(customAccessTokenConverter);
         return converter;
     }
 }

+ 13 - 1
log-service/src/main/java/com/diagbot/util/UserUtils.java

@@ -4,8 +4,10 @@ package com.diagbot.util;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 用户工具类
@@ -25,13 +27,23 @@ public class UserUtils {
     }
 
     /**
-     * 获取当前请求的用户Id
+     * 获取当前请求的用户名称
      * @return
      */
     public static String getCurrentPrinciple() {
         return (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
     }
 
+    /**
+     * 获取当前请求的用户ID
+     * @return
+     */
+    public static String getCurrentPrincipleID() {
+        OAuth2AuthenticationDetails oauthDetails = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
+        Map<String, Object> details = (Map<String, Object>) oauthDetails.getDecodedDetails();
+        return details.get("user_id").toString();
+    }
+
     /**
      * 判读当前token用户是否为接口所需的参数username
      *