【发布时间】:2015-10-02 04:53:24
【问题描述】:
首先根据Spring doc ,如果我想将用户角色映射到范围,我应该使用 setCheckUserScopes(true) 到 DefaultOAuth2RequestFactory。因此,一种方法是注入我自己的 DefaultOAuth2RequestFactory bean,正如文档所说:
The AuthorizationServerEndpointsConfigurer allows you to inject a custom OAuth2RequestFactory so you can use that feature to set up a factory if you use @EnableAuthorizationServer.
那我做
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends
AuthorizationServerConfigurerAdapter {
...
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.authenticationManager(authenticationManager)
.tokenStore(tokenStore)
.tokenServices(tokenServices());
endpoints
.getOAuth2RequestFactory(); // this doesn't return me my own DefaultOAuth2RequestFactory
}
@Bean
@Primary
public OAuth2RequestFactory defaultOAuth2RequestFactory() {
DefaultOAuth2RequestFactory defaultOAuth2RequestFactory = new DefaultOAuth2RequestFactory(
clientDetailsService);
defaultOAuth2RequestFactory.setCheckUserScopes(true);
return defaultOAuth2RequestFactory;
}
}
编辑
我忽略了 AuthorizationServerEndpointsConfigurer 中的 requestFactory() 方法。这是将它传递给 Spring Security 的正确方法。将 OAuth2RequestFactory bean 设置为主无效。我删除了一些内容以关注真正的问题:
经过这次观察,实际问题:
据我了解,如果用户具有权限 A 和 B,并且应用程序具有范围 A,那么他只会获得“A”范围。但这并没有发生。真正发生的是,如果应用程序具有范围 A,并且 APP(不是用户)具有权限 A 和 B,那么用户将获得 A。但这没有任何意义。 这是解析用户范围的 DefaultOAuth2RequestFactory 方法:
private Set<String> extractScopes(Map<String, String> requestParameters, String clientId) {
... // I avoid some unimportant lines to not make this post so long
if ((scopes == null || scopes.isEmpty())) {
scopes = clientDetails.getScope();
}
if (checkUserScopes) {
scopes = checkUserScopes(scopes, clientDetails);
}
return scopes;
}
private Set<String> checkUserScopes(Set<String> scopes, ClientDetails clientDetails) {
if (!securityContextAccessor.isUser()) {
return scopes;
}
Set<String> result = new LinkedHashSet<String>();
Set<String> authorities = AuthorityUtils.authorityListToSet(securityContextAccessor.getAuthorities());
for (String scope : scopes) {
if (authorities.contains(scope) || authorities.contains(scope.toUpperCase())
|| authorities.contains("ROLE_" + scope.toUpperCase())) {
result.add(scope);
}
}
return result;
}
这是一个错误吗?如果我错了,请告诉我。问候
【问题讨论】:
标签: java spring authentication user-permissions spring-security-oauth2