2017-04-12 127 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()");)時訪問所有內容。