草庐IT

java - Spring OAuth2 checkUserScopes 未按预期工作

coder 2023-05-11 原文

首先,根据Spring doc ,如果我想将用户角色映射到范围,我应该使用 setCheckUserScopes(true) 到 DefaultOAuth2RequestFactory。因此,一种方法是注入(inject)我自己的 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;
} 

这是一个错误吗?如果我错了,请告诉我。问候

最佳答案

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

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

关于java - Spring OAuth2 checkUserScopes 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31391607/

有关java - Spring OAuth2 checkUserScopes 未按预期工作的更多相关文章

随机推荐