相关疑难解决方法(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万
查看次数

将用户角色映射到oauth2范围/权限

我们有一个权利数据库,其中包含应用程序ID,角色和用户映射到每个应用程序的角色.遵循线程建议如何基于resourceId将用户角色映射到oauth2范围/权限?

忽略我上面提到的权利数据库,我是否根据下面代码中的user和resourceId将角色"USER","READER","WRITER"映射到oauth2范围/权限?

用户认证/授权配置

@Configuration
@Order(-10)
protected static class LoginConfig extends WebSecurityConfigurerAdapter {

    ....

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // auth.parentAuthenticationManager(authenticationManager);
        // @formatter:off
        auth.inMemoryAuthentication()
            .withUser("admin").password("admin")
                .roles("ADMIN", "USER", "READER", "WRITER")
            .and()
            .withUser("user").password("password")
                .roles("USER")
            .and()
            .withUser("audit").password("audit")
                .roles("USER", "ADMIN", "READER");
        // @formatter:on
    }
}
Run Code Online (Sandbox Code Playgroud)

OAuth2配置

@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // @formatter:off
        clients.inMemory()
            .withClient("acme").secret("acmesecret")
                .authorizedGrantTypes("authorization_code", "refresh_token", "password")
                .scopes("openid")
            .and() …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-security-oauth2

15
推荐指数
1
解决办法
5763
查看次数