繁体   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