【问题标题】:Spring OAuth2 checkUserScopes is not working as expectedSpring OAuth2 checkUserScopes 未按预期工作
【发布时间】: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


    【解决方案1】:

    您需要通过代码 like here 连接您的 OAuth2RequestFactory。

    如果权限是由ClientDetailsService 设置的,那么你应该很好。如果您要映射已登录的用户权限,我 don't have luck there either

    【讨论】:

    • 我显然忽略了 requestFactory() 方法,因此通过该方法注入它可以解决第一个“错误”。另一方面,我还找不到第二个“错误”的任何解决方案。谢谢!
    • 我也有兴趣了解后一个问题。可能是@DaveSyer 会指导:)
    • 请@DaveSyer,我们需要你的帮助:)
    • 是的,这是我的。我把它贴在 jira 上,他在 github 上复制了这个问题
    • 当我看到票时,我正要自己打开jira。很高兴你做到了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 2020-01-18
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多