From 04c359a40d51c391fdfea626e7f147422a33b89f Mon Sep 17 00:00:00 2001
From: zhaoxiaohao <913652501@qq.com>
Date: Fri, 19 Jun 2020 09:43:08 +0800
Subject: [PATCH] Merge branch 'dev' of http://192.168.2.240:7070/r/kidgrow-microservices-platform into dev

---
 kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/config/AuthorizationServerConfig.java |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 54 insertions(+), 1 deletions(-)

diff --git a/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/config/AuthorizationServerConfig.java b/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/config/AuthorizationServerConfig.java
index 5672172..aca478e 100644
--- a/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/config/AuthorizationServerConfig.java
+++ b/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/config/AuthorizationServerConfig.java
@@ -1,10 +1,15 @@
 package com.kidgrow.oauth2.config;
 
+import com.kidgrow.oauth2.handler.SingleTokenServices;
 import com.kidgrow.oauth2.service.impl.RedisClientDetailsService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
 import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.ProviderManager;
+import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
 import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
@@ -17,6 +22,7 @@
 import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
 import org.springframework.security.oauth2.provider.token.TokenStore;
 import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
+import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
 
 import javax.annotation.Resource;
 import java.util.Arrays;
@@ -45,6 +51,9 @@
 
     @Autowired
     private TokenStore tokenStore;
+
+//    @Autowired
+//    private CustomRedisTokenStore tokenStore;
 
     @Autowired(required = false)
     private JwtAccessTokenConverter jwtAccessTokenConverter;
@@ -77,13 +86,57 @@
                 endpoints.accessTokenConverter(jwtAccessTokenConverter);
             }
         }
-        endpoints.tokenStore(tokenStore)
+        endpoints
+                //扩展tokenServices
+//                .tokenServices(tokenServices(endpoints))
+                .tokenServices(KidgrowTokenServices())
                 .authenticationManager(authenticationManager)
                 .userDetailsService(userDetailsService)
+//                .tokenStore(tokenStore)
                 .authorizationCodeServices(authorizationCodeServices)
                 .exceptionTranslator(webResponseExceptionTranslator);
     }
 
+
+    @Bean
+    @Primary
+    public SingleTokenServices KidgrowTokenServices(){
+        SingleTokenServices tokenServices = new SingleTokenServices();
+        tokenServices.setTokenStore(tokenStore);
+        tokenServices.setSupportRefreshToken(true);//支持刷新token
+        tokenServices.setReuseRefreshToken(true);
+        addUserDetailsService(tokenServices, this.userDetailsService);
+        return tokenServices;
+    }
+
+//    private SingleTokenServices tokenServices(AuthorizationServerEndpointsConfigurer endpoints) {
+//        SingleTokenServices tokenServices = new SingleTokenServices();
+//        tokenServices.setTokenStore(tokenStore);
+//        tokenServices.setSupportRefreshToken(true);//支持刷新token
+//        tokenServices.setReuseRefreshToken(true);
+//        tokenServices.setClientDetailsService(endpoints.getClientDetailsService());
+//        tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
+//        addUserDetailsService(tokenServices, this.userDetailsService);
+//        return tokenServices;
+//    }
+//    private void addUserDetailsService(SingleTokenServices tokenServices, UserDetailsService userDetailsService) {
+//        if (userDetailsService != null) {
+//            PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
+//            provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(
+//                    userDetailsService));
+//            tokenServices.setAuthenticationManager(new ProviderManager(Arrays.asList(provider)));
+//        }
+//    }
+
+    private void addUserDetailsService(SingleTokenServices tokenServices, UserDetailsService userDetailsService) {
+        if (userDetailsService != null) {
+            PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
+            provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(
+                    userDetailsService));
+            tokenServices.setAuthenticationManager(new ProviderManager(Arrays.asList(provider)));
+        }
+    }
+
     /**
      * 配置应用名称 应用id
      * 配置OAuth2的客户端相关信息

--
Gitblit v1.8.0