jsc*_*man 16 java authentication spring user-permissions spring-security-oauth2
首先,根据Spring doc ,如果我想将用户角色映射到范围,我应该使用setCheckUserScopes(true)到DefaultOAuth2RequestFactory.所以这样做的一种方法是注入我自己的DefaultOAuth2RequestFactory bean,正如doc所说:
The AuthorizationServerEndpointsConfigurer allows you to inject a custom OAuth2RequestFactory so you can use that feature to set up a factory if you use @EnableAuthorizationServer.
Run Code Online (Sandbox Code Playgroud)
然后我做
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我忽略了AuthorizationServerEndpointsConfigurer中的方法requestFactory().这是将它传递给Spring Security的正确方法.将OAuth2RequestFactory bean设置为primary不起作用.我删除了一些关注真正问题的东西:
经过这次观察,实际问题:
据我所知,如果用户有权限A和B,并且应用程序具有范围A,那么他只获得"A"范围.但这不会发生.真正发生的是,如果app具有范围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;
}
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?请告诉我,如果我错了.问候
您需要通过类似此处的代码连接您的 OAuth2RequestFactory 。
如果权限是由ClientDetailsService设置的,那么你应该没问题。如果您正在寻找映射登录用户权限,我也没有运气。
| 归档时间: |
|
| 查看次数: |
2068 次 |
| 最近记录: |