content: package com.alibaba.tesla.authproxy.oauth2;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpMethod;
  import org.springframework.security.authentication.AuthenticationManager;
  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;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  import org.springframework.security.oauth2.provider.ClientDetailsService;
  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 java.util.Arrays;
  
  /**
   * 鉴权 Server 配置
   *
   * @author yaoxing.gyx@alibaba-inc.com
   */
  @Configuration
  public class TeslaOAuth2ServerConfig {
  
      @Configuration
      @EnableAuthorizationServer
      protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  
          @Autowired
          private AuthenticationManager authenticationManager;
  
          @Autowired
          private JwtAccessTokenConverter jwtAccessTokenConverter;
  
          @Autowired
          private TokenStore tokenStore;
  
          @Autowired
          private TeslaOAuth2TokenEnhancer tokenEnhancer;
  
          @Autowired
          @Qualifier("teslaUserDetailsService")
          private UserDetailsService userDetailsService;
  
          @Autowired
          @Qualifier("teslaClientDetailsService")
          private ClientDetailsService clientDetailsService;
  
          @Override
          public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
              clients.withClientDetails(clientDetailsService);
          }
  
          @Override
          public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
              TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
              tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer, jwtAccessTokenConverter));
              endpoints.tokenStore(tokenStore)
                  .accessTokenConverter(jwtAccessTokenConverter)
                  .authenticationManager(authenticationManager)
                  .userDetailsService(userDetailsService)
                  .tokenEnhancer(tokenEnhancerChain)
                  .allowedTokenEndpointRequestMethods(HttpMethod.POST);
              endpoints.reuseRefreshTokens(true);
          }
  
          @Override
          public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
              oauthServer.allowFormAuthenticationForClients()
                  .checkTokenAccess("permitAll()");
          }
      }
  }
  