content: package com.flame.oauth.config;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.annotation.Order;
  import org.springframework.data.redis.connection.RedisConnectionFactory;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  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.store.JdbcTokenStore;
  import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  
  import javax.sql.DataSource;
  
  /**
   * 开启授权服务器
   *
   * 声明一个认证服务器，当用此注解后，应用启动后将自动生成几个Endpoint：
   * （注：其实实现一个认证服务器就是这么简单，加一个注解就搞定，当然真正用到生产环境还是要进行一些配置和复写工作的。）
   *
   * /oauth/authorize：验证
   * /oauth/token：获取token
   * /oauth/confirm_access：用户授权
   * /oauth/error：认证失败
   * /oauth/check_token：资源服务器用来校验token
   * /oauth/token_key：如果jwt模式则可以用此来从认证服务器获取公钥
   * Created by sungang on 2017/9/25.
   */
  @Configuration
  @EnableAuthorizationServer
  public class OAuth2ServerConfiguration extends AuthorizationServerConfigurerAdapter {
  
  
      @Autowired
      private AuthenticationManager auth;
  
      @Autowired
      private DataSource dataSource;
  
      private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  
      @Bean
      public JdbcTokenStore tokenStore() {
          return new JdbcTokenStore(dataSource);
      }
  
      @Override
      public void configure(AuthorizationServerSecurityConfigurer security)
              throws Exception {
          security.passwordEncoder(passwordEncoder);
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints)
              throws Exception {
          endpoints
                  .authenticationManager(auth)
                  .tokenStore(tokenStore())
          ;
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients)
              throws Exception {
  
          clients.jdbc(dataSource)
                  .passwordEncoder(passwordEncoder)
                  .withClient("client")
                  .secret("secret")
                  .authorizedGrantTypes("password", "refresh_token","authorization_code")
                  .scopes("read", "write")
                  .accessTokenValiditySeconds(3600) // 1 hour
                  .refreshTokenValiditySeconds(2592000) // 30 days
                  .and()
                  .withClient("genesis-provider-goods")
                  .secret("password")
                  .authorizedGrantTypes("client_credentials", "refresh_token","authorization_code")
                  .scopes("server")
                  .and()
                  .withClient("genesis-provider-order")
                  .secret("password")
                  .authorizedGrantTypes("client_credentials", "refresh_token","authorization_code")
                  .scopes("server")
          ;
  
      }
  //
  //    @Configuration
  //    @Order(-20)
  //    protected static class AuthenticationManagerConfiguration extends GlobalAuthenticationConfigurerAdapter {
  //
  //        @Autowired
  //        private DataSource dataSource;
  //
  //        @Override
  //        public void init(AuthenticationManagerBuilder auth) throws Exception {
  //            auth.jdbcAuthentication().dataSource(dataSource)
  //                    .withUser("dave").password("secret").roles("USER")
  //                    .and()
  //                    .withUser("anil").password("password").roles("ADMIN")
  //            ;
  //        }
  //    }
  
  }
  