相关疑难解决方法(0)

在Spring Security OAuth2(提供程序)中使用作用域作为角色

让我们考虑一个相当简单的假设应用程序,用户可以在其中阅读或撰写帖子.

有些用户可以阅读和撰写文章,而有些用户只能阅读.使用Spring Security(3.2.1),我通过两个角色对此进行了建模:

  • ROLE_WRITE:此角色授予用户访问撰写帖子的权限.
  • ROLE_READ:此角色授予用户访问阅读帖子的权限.

使用Spring安全性实现这一点非常简单......

现在,我想通过使用Spring Security OAuth(版本2.0.0.M3 ATM)实现OAuth2提供程序,允许第三方应用程序代表用户读写帖子.

在授权步骤期间,应用程序询问用户是否愿意代表他们授予阅读和/或撰写帖子的权利.这里的用户在这里授予范围(不是角色).

然后,当OAuth2使用者调用我的REST API时,Spring Sec OAuth会授权所授予的令牌并创建一个身份验证,其中包含具有所有角色的用户以及仅授予的作用域.

问题(和问题)是我现在必须编写不同的安全逻辑,具体取决于API是否由正常身份验证的用户调用(只检查角色)或是否通过OAuth2调用(检查角色+范围).

是否可以在Spring Security OAuth2中"合并"角色和范围的概念,以便在授权步骤中,用户向应用程序授予他们拥有的角色的子集(并且让OAuth2身份验证仅在授予的权限中报告这些角色) ?这样,当第三方应用程序进行API调用时,身份验证中的角色是授予的角色?这样我就不必编写任何特定于OAuth2的安全逻辑.

spring oauth spring-security oauth-2.0 spring-security-oauth2

20
推荐指数
1
解决办法
2万
查看次数

Spring OAuth2 checkUserScopes未按预期工作

首先,根据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 …

java authentication spring user-permissions spring-security-oauth2

16
推荐指数
1
解决办法
2068
查看次数