content: package com.xzll.auth.config;
  
  import com.xzll.auth.component.JwtTokenEnhancer;
  import com.xzll.auth.service.impl.UserServiceImpl;
  import lombok.AllArgsConstructor;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.io.ClassPathResource;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.crypto.password.PasswordEncoder;
  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.token.TokenEnhancer;
  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.oauth2.provider.token.store.JwtTokenStore;
  import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
  
  import java.security.KeyPair;
  import java.util.ArrayList;
  import java.util.List;
  
  
  /**
   * @Author: hzz
   * @Date: 2024/6/10 11:06:10
   * @Description: 认证服务器配置
   */
  @AllArgsConstructor
  @Configuration
  @EnableAuthorizationServer
  public class Oauth2ServerConfig extends AuthorizationServerConfigurerAdapter {
  
      private final PasswordEncoder passwordEncoder;
      private final UserServiceImpl userDetailsService;
      private final AuthenticationManager authenticationManager;
      private final JwtTokenEnhancer jwtTokenEnhancer;
      private final Oauth2Config oauth2Config;
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
          clients.inMemory()
                  .withClient(oauth2Config.getClientId())
                  .secret(passwordEncoder.encode(oauth2Config.getPassword()))
                  .scopes(oauth2Config.getScopes().toArray(new String[0]))
                  .authorizedGrantTypes(oauth2Config.getAuthorizedGrantTypes().toArray(new String[0]))
                  .accessTokenValiditySeconds(3600)
                  .refreshTokenValiditySeconds(86400);
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
          TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
          List<TokenEnhancer> delegates = new ArrayList<>();
          delegates.add(jwtTokenEnhancer);
          delegates.add(accessTokenConverter());
          //配置JWT的内容增强器
          enhancerChain.setTokenEnhancers(delegates);
          endpoints.authenticationManager(authenticationManager)
                  //配置加载用户信息的服务
                  .userDetailsService(userDetailsService)
                  .accessTokenConverter(accessTokenConverter())
                  .tokenEnhancer(enhancerChain);
      }
  
      @Override
      public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
          security.allowFormAuthenticationForClients();
      }
  
      /**
       * token存储 目前是单机存储，当多实例部署时 需要存到redis或者mysql，具体可参考我study 项目的auth服务。
       *
       * @return
       */
      @Bean
      public TokenStore tokenStore() {
          return new JwtTokenStore(accessTokenConverter());
      }
  
  
      @Bean
      public JwtAccessTokenConverter accessTokenConverter() {
          JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
          jwtAccessTokenConverter.setKeyPair(keyPair());
          return jwtAccessTokenConverter;
      }
  
      @Bean
      public KeyPair keyPair() {
          //从classpath下的证书中获取秘钥对
          KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource(oauth2Config.getJwtFile()), oauth2Config.getJwtPassword().toCharArray());
          return keyStoreKeyFactory.getKeyPair(oauth2Config.getJwtAlias(), oauth2Config.getJwtPassword().toCharArray());
      }
  
  }
  