2017-04-12 222 views
0

我在我的项目中使用Spring Rest + OAUTH2 + React。为了创建授权服务器,我从示例中获得了一些代码。但问题是我无法理解代码。有人可以解释我这个代码:授权服务器

@Configuration 
@EnableAuthorizationServer 
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Bean 
    public JwtAccessTokenConverter jwtAccessTokenConverter() { 
     JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); 
     KeyPair keyPair = new KeyStoreKeyFactory(
       new ClassPathResource("keystore.jks"), "suleman123".toCharArray()) 
       .getKeyPair("resourcekey"); 
     converter.setKeyPair(keyPair); 
     return converter; 
    } 

    /** 
    * This method configure client details service by using inMemory implementation. JDBC Implementation can also used 
    */ 
    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("acme") // client id 
       .secret("acmesecret") // required for trusted clients 
       .authorizedGrantTypes("authorization_code", "refresh_token", 
         "password") // Grant types that are authorized for the client to use 
       .scopes("openid") // scope to which the client is limited 
       .autoApprove(true); 
    } 

    /** 
    * This method configure the grant types. By default all grant types are supported except password 
    */ 
    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
      throws Exception { 
     endpoints.authenticationManager(authenticationManager).accessTokenConverter(
       jwtAccessTokenConverter()); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) 
      throws Exception { 
     oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess(
       "isAuthenticated()"); 
    } 
} 

回答

1

春季开机与自动配置工程。你在这里看到的是有人扩展了一个弹簧自动配置类,以便根据他的需要进行定制。

TL; DR:

他们建立一个基于JWT的oauth2授权服务器。

详细的解答:

在这种情况下,通过结合@EnableAuthorizationServer和扩展AuthorizationServerConfigurerAdapter,您可以启用,操纵和修改自己的认证服务器。

  1. 在这个例子中,他们不是使用普通的字符串标记,而是想使用JWT。由于这个原因,初始化的第一个bean是JwtAccessTokenConverterMore on JWT
  2. configure(ClientDetailsServiceConfigurer clients) - 它们配置一个内存中的客户端以在应用程序中使用。
  3. configure(AuthorizationServerEndpointsConfigurer endpoints) - 它们将默认authenticationManager配置为在春季初始化并注入配置类的顶部,并将accessTokenConverter设置为使用#1中提到的jwtAccessTokenConverter。这样做可以让他们在对新令牌进行排队时生成JWT令牌。
  4. configure(AuthorizationServerSecurityConfigurer oauthServer) - 它们设置所有端点以允许在有令牌认证用户(oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");)时访问所有内容。