我有一个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方法的主要用途是什么?它在这里设置的是什么?
请帮助我理解它。
谢谢。
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;
}
@EnableAuthorizationServer
的目的前面的答案中已经提供了javadoc解释。
在外行人的语言中,这是设置你的令牌生成终点,即如果你提供属性security.oauth2.client.client-id
和security.oauth2.client.client-secret
,Spring将为你提供一个身份验证服务器,在端点/oauth/token
提供标准的Oauth2令牌
在实际情况中,这意味着您要在企业用户LDAP或用户数据库之上设置令牌生成Web应用程序(第7层),并且通常是与客户端应用程序(API等)分开的应用程序。
如果您查看@EnableAuthorizationServer的JavaDoc注释,您可以看到它说明如下;
用于启用授权服务器的便捷注释(即当前应用程序上下文中的AuthorizationEndpoint和TokenEndpoint,它必须是DispatcherServlet上下文。服务器的许多功能可以使用@Beans类型的AuthorizationServerConfigurer进行自定义(例如,通过扩展AuthorizationServerConfigurerAdapter。用户是负责使用普通的Spring Security功能(EnableWebSecurity @EnableWebSecurity等)保护授权端点(/ oauth / authorize),但令牌端点(/ oauth / token)将使用客户端凭据上的HTTP Basic身份验证自动保护。客户端必须通过一个或多个AuthorizationServerConfigurers提供ClientDetailsService来注册。
扩展AuthorizationServerConfigurerAdapter
仅用于定制授权服务器。您可以通过使用@EnableAuthorizationServer
使用Just Annotating Bean类轻松地在Spring Security中设置一个正常运行的Authorization Server