Explorar el Código

鉴权服务器代码修改

gaodm hace 5 años
padre
commit
5049a320a1

+ 0 - 51
src/main/java/com/diagbot/config/JwtConfigurer.java

@@ -1,51 +0,0 @@
-package com.diagbot.config;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.core.io.ClassPathResource;
-import org.springframework.core.io.Resource;
-import org.springframework.security.jwt.crypto.sign.RsaVerifier;
-import org.springframework.security.oauth2.provider.token.TokenStore;
-import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
-import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
-import org.springframework.util.FileCopyUtils;
-
-import java.io.IOException;
-
-/**
- * @Description: JWT配置类
- * @author: gaodm
- * @time: 2018/8/2 13:38
- */
-@Configuration
-public class JwtConfigurer {
-    @Autowired
-    private CustomAccessTokenConverter customAccessTokenConverter;
-
-    @Bean
-    @Qualifier("tokenStoreClient")
-    public TokenStore tokenStoreClient() {
-        System.out.println("Created JwtTokenStoreClient");
-        return new JwtTokenStore(jwtTokenEnhancerClient());
-    }
-
-    @Bean("jwtTokenEnhancerClient")
-    protected JwtAccessTokenConverter jwtTokenEnhancerClient() {
-        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
-        Resource resource = new ClassPathResource("public.cert");
-        String publicKey;
-        try {
-            publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-        converter.setVerifierKey(publicKey);
-        //不设置这个会出现 Cannot convert access token to JSON
-        converter.setVerifier(new RsaVerifier(publicKey));
-        converter.setAccessTokenConverter(customAccessTokenConverter);
-        System.out.println("Created jwtTokenEnhancerClient");
-        return converter;
-    }
-}

+ 8 - 9
src/main/java/com/diagbot/config/OAuth2Configurer.java

@@ -1,6 +1,7 @@
 package com.diagbot.config;
 
 import com.diagbot.service.UrlUserService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.context.annotation.Bean;
@@ -28,9 +29,11 @@ import java.util.Arrays;
  */
 @Configuration
 @EnableAuthorizationServer
+@Slf4j
 public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
     @Autowired
     private UrlUserService urlUserService;
+
     @Override
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
         clients.inMemory()
@@ -58,10 +61,10 @@ public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
         //指定认证管理器
         endpoints.authenticationManager(authenticationManager).userDetailsService(urlUserService);
         //指定token存储位置
-        endpoints.tokenStore(tokenStore());
+        endpoints.tokenStore(new JwtTokenStore(jwtTokenEnhancerServer()));
         // 自定义token生成方式
         TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
-        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(customerEnhancer(), jwtTokenEnhancer()));
+        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(customerEnhancer(), jwtTokenEnhancerServer()));
         endpoints.tokenEnhancer(tokenEnhancerChain);
     }
 
@@ -76,16 +79,12 @@ public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
     @Qualifier("authenticationManagerBean")
     private AuthenticationManager authenticationManager;
 
-    @Bean
-    public TokenStore tokenStore() {
-        return new JwtTokenStore(jwtTokenEnhancer());
-    }
-
-    @Bean
-    protected JwtAccessTokenConverter jwtTokenEnhancer() {
+    @Bean("JwtTokenEnhancerServer")
+    protected JwtAccessTokenConverter jwtTokenEnhancerServer() {
         KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("diagbot-jwt.jks"), "diagbot123456".toCharArray());
         JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
         converter.setKeyPair(keyStoreKeyFactory.getKeyPair("diagbot-jwt"));
+        log.info("Created JwtTokenEnhancerServer");
         return converter;
     }
 }

+ 32 - 4
src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -4,12 +4,22 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.jwt.crypto.sign.RsaVerifier;
 import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
 import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
 import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
 import org.springframework.security.oauth2.provider.token.TokenStore;
+import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
+import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
+import org.springframework.util.FileCopyUtils;
+
+import java.io.IOException;
 
 /**
  * @Description: 权限资源配置类
@@ -18,6 +28,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
  */
 @Configuration
 @EnableResourceServer
+@ComponentScan({"com.diagbot.config"})
 public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
     Logger log = LoggerFactory.getLogger(ResourceServerConfigurer.class);
 
@@ -44,11 +55,28 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
 
     @Override
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
-        log.info("Configuring ResourceServerSecurityConfigurer ");
-        resources.resourceId("user-service").tokenStore(tokenStore);
+        log.info("Configuring ResourceServerSecurityConfigurer");
+        resources.resourceId("user-service").tokenStore(new JwtTokenStore(jwtTokenEnhancerClient()));
     }
 
     @Autowired
-    @Qualifier("tokenStoreClient")
-    TokenStore tokenStore;
+    private CustomAccessTokenConverter customAccessTokenConverter;
+
+    @Bean("jwtTokenEnhancerClient")
+    protected JwtAccessTokenConverter jwtTokenEnhancerClient() {
+        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
+        Resource resource = new ClassPathResource("public.cert");
+        String publicKey;
+        try {
+            publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        converter.setVerifierKey(publicKey);
+        //不设置这个会出现 Cannot convert access token to JSON
+        converter.setVerifier(new RsaVerifier(publicKey));
+        converter.setAccessTokenConverter(customAccessTokenConverter);
+        log.info("Created jwtTokenEnhancerClient");
+        return converter;
+    }
 }