了解spring-security-oauth2 @EnableAuthorizationServer

Abd*_*han 4 java spring-boot spring-security-oauth2

我有一个spring-security-oauth2项目,该类作为Authorization server顺利运行。

客户端ID,用户的令牌,刷新令牌都是由数据库管理。

@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");
    }
}
Run Code Online (Sandbox Code Playgroud)

一切工作正常,除了我不知道configure方法在做什么。即使我删除了完整的方法,oauth2进程仍然可以正常工作。

在这种情况下,configure方法的主要用途是什么?它在这里设置什么领域?

请帮助我理解它。

谢谢。

Sab*_*han 6

  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;
}
Run Code Online (Sandbox Code Playgroud)
  1. 目的 @EnableAuthorizationServer

先前的答案中已经提供了Javadoc说明。

用外行的语言,这是为了设置令牌生成端点,即,如果您提供属性security.oauth2.client.client-idsecurity.oauth2.client.client-secret,Spring将为您提供身份验证服务器,并在端点处提供标准Oauth2令牌/oauth/token

在实际情况下,这意味着您要在企业用户LDAP或用户数据库之上建立令牌生成Web应用程序(第7层),并且通常是与用户端应用程序(API等)分开的应用程序。


sha*_*zin 1

如果您查看@EnableAuthorizationServer的 JavaDoc 注释,您会发现它有以下内容:

用于启用授权服务器的便捷注释(即当前应用程序上下文中的 AuthorizationEndpoint 和 TokenEndpoint ,必须是 DispatcherServlet 上下文。服务器的许多功能可以使用 AuthorizationServerConfigurer 类型的 @Bean 进行自定义(例如通过扩展 AuthorizationServerConfigurerAdapter )。用户负责使用正常的 Spring Security 功能(EnableWebSecurity @EnableWebSecurity 等)保护授权端点(/oauth/authorize),但令牌端点(/oauth/token)将使用客户端凭据上的 HTTP 基本身份验证自动保护。客户端必须通过一个或多个 AuthorizationServerConfigurers 提供 ClientDetailsS​​ervice 进行注册。

扩展AuthorizationServerConfigurerAdapter仅用于授权服务器的定制。您可以通过注释 Bean 类轻松地在 Spring Security 中设置一个功能正常的授权服务器@EnableAuthorizationServer