簡體   English   中英

了解spring-security-oauth2 @EnableAuthorizationServer

[英]Understanding spring-security-oauth2 @EnableAuthorizationServer

我有一個spring-security-oauth2項目,它與一個類作為授權服務器順利運行。

client-id,user-tokens,refresh-tokens都由數據庫管理。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    private static String REALM = "MY_OAUTH_REALM";
    ...
    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.realm(REALM + "/client");
    }
}

一切都工作正常,除了我不知道配置方法正在做什么。 即使我刪除了完整的方法, oauth2進程仍然可以正常工作。

在這種情況下,configure方法的主要用途是什么?它在這里設置的是什么?

請幫助我理解它。

謝謝。

  1. configure方法的目的

AuthorizationServerConfigurerAdapter有三個configure(...)方法,這三個方法都可以被覆蓋,並且這些方法可以用於不同的目的。

在你的問題中,你只引用了一個。

它們的目的是為Authorization Server端點,客戶端和安全性提供自定義設置。 因此,由於存在一些預定義的默認設置,因此您需要覆蓋多少。

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
// This can be used to configure security of your authorization server itself 
// i.e. which user can generate tokens , changing default realm etc.
// Sample code below.

// We're allowing access to the token only for clients with  'ROLE_TRUSTED_CLIENT' authority.
// There are few more configurations and changing default realm is one of those 
    oauthServer
        .tokenKeyAccess("hasAuthority('ROLE_TRUSTED_CLIENT')")
        .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')");
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// Here you will specify about `ClientDetailsService` 
// i.e. information about OAuth2 clients & where their info is located - memory , DB , LDAP etc.
// Sample code below.
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
// This can be used to configure security of your authorization server itself
// i.e. which user can generate tokens , changing default realm etc - Sample code below.

    // we're allowing access to the token only for clients with  'ROLE_TRUSTED_CLIENT' authority.
    // There are few more configurations and changing default realm is one of those 
    oauthServer
        .tokenKeyAccess("hasAuthority('ROLE_TRUSTED_CLIENT')")
        .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')");
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    // Here you will specify about `ClientDetailsService` i.e.
    // information about OAuth2 clients & where their info is located - memory , DB , LDAP etc.
    // Sample code below 
    clients.inMemory()
        .withClient("trusted-app")
        .authorizedGrantTypes("client_credentials", "password", "refresh_token")
        .authorities("ROLE_TRUSTED_CLIENT")
        .scopes("read", "write")
        .resourceIds("oauth2_id")
        .accessTokenValiditySeconds(10000)
        .refreshTokenValiditySeconds(20000)
        .secret("secret");
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    // Here you will do non-security configs for end points associated with your Authorization Server
    // and can specify details about authentication manager, token generation etc. Sample code below 
    endpoints
        .authenticationManager(this.authenticationManager)
        .tokenServices(tokenServices())
        .tokenStore(tokenStore())
        .accessTokenConverter(accessTokenConverter());
}

@Bean
public TokenStore tokenStore() {
    return new JwtTokenStore(accessTokenConverter());
}   

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey("abcd");
    return converter;
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    defaultTokenServices.setSupportRefreshToken(true);
    defaultTokenServices.setTokenEnhancer(accessTokenConverter());
    return defaultTokenServices;
}
  1. @EnableAuthorizationServer目的

前面的答案中已經提供了javadoc解釋。

在外行的語言中,這是設置您的令牌生成端點,即如果您提供屬性security.oauth2.client.client-idsecurity.oauth2.client.client-secret ,Spring將為您提供一個身份驗證服務器,提供標准端點/oauth/token Oauth2 /oauth/token

在實際情況中,這意味着您要在企業用戶LDAP或用戶數據庫之上設置令牌生成Web應用程序(第7層),並且通常是與客戶端應用程序(API等)分開的應用程序。

如果您查看@EnableAuthorizationServer的JavaDoc注釋,您可以看到它顯示以下內容;

用於啟用授權服務器的便捷注釋(即當前應用程序上下文中的AuthorizationEndpoint和TokenEndpoint,它必須是DispatcherServlet上下文。服務器的許多功能可以使用@Beans類型的AuthorizationServerConfigurer進行自定義(例如,通過擴展AuthorizationServerConfigurerAdapter。用戶是負責使用普通的Spring Security功能(EnableWebSecurity @EnableWebSecurity等)保護授權端點(/ oauth / authorize),但令牌端點(/ oauth / token)將使用客戶端憑據上的HTTP Basic身份驗證自動保護。客戶端必須通過一個或多個AuthorizationServerConfigurers提供ClientDetailsS​​ervice來注冊。

擴展AuthorizationServerConfigurerAdapter僅用於自定義授權服務器。 您可以通過使用@EnableAuthorizationServer只注釋Bean類來輕松地在Spring Security中設置一個正常運行的Authorization Server

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM