【问题标题】:Understanding spring-security-oauth2 @EnableAuthorizationServer了解 spring-security-oauth2 @EnableAuthorizationServer
【发布时间】:2017-10-11 14:36:19
【问题描述】:

我有一个 spring-security-oauth2 项目,它使用一个类作为授权服务器顺利运行。

client-ids、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 方法在这个上下文中的主要用途是什么,它在这里设置了什么领域?

请帮助我理解它。

谢谢。

【问题讨论】:

    标签: java spring-boot spring-security-oauth2


    【解决方案1】:
    1. configure 方法的用途

    AuthorizationServerConfigurerAdapter 有三个 configure(...) 方法,所有三个都可以被覆盖,它们用于不同的目的。

    在您的问题中,您只引用了一个。

    它们的目的是为授权服务器端点、客户端和安全性提供您的自定义设置。因此,由于有一些预定义的默认设置,您希望覆盖多少取决于您。

    @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 令牌

    在实际场景中,这意味着您正在企业用户 LDAP 或用户数据库之上设置令牌生成 Web 应用程序(第 7 层),并且通常是与您的消费者端应用程序 (API) 分开的应用程序ETC )。

    【讨论】:

      【解决方案2】:

      如果您查看@EnableAuthorizationServer 的 JavaDoc 注释,您会看到它说以下内容;

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

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

      【讨论】:

        猜你喜欢
        • 2016-03-05
        • 2019-09-25
        • 2019-04-29
        • 2017-09-11
        • 2019-04-28
        • 2015-11-14
        • 2016-07-12
        • 2017-01-18
        • 2012-12-19
        相关资源
        最近更新 更多