Spring 安全/OAuth : mapping between Principal's authority and role in @RolesAllowed

标签 spring spring-security spring-security-oauth2

我正在玩弄 Spring OAuth,实现了授权服务器和资源服务器。资源服务器使用 user-info-uri 解码 token 。

资源服务器 Controller 中的方法(部分)被@RolesAllowed保护(也试过@PreAuthorize,效果一样)。

@RolesAllowed("ROLE_USER")
//@PreAuthorize("hasRole('ROLE_USER')")
@RequestMapping(value = "/test-user", method = RequestMethod.GET)
public String testUser() {
    return "You are User!";
}

在授权服务器端管理三个用户:具有 ROLE_ADMIN 的用户 1、具有 ROLE_USER 的用户 2 和用户 3。

资源服务接受由授权服务器生成的 token (密码授予流程)并向用户信息 URI 询问主体详细信息。到目前为止按设计工作。

但是接下来发生的事情,是我不明白的。主体结构(例如,对于 user2,具有 ROLE_USER)包含正确的权限(出于示例目的,我手动调用了 user-info-uri):

  "principal": {
    "password": null,
    "username": "user2",
    "authorities": [
      {
        "authority": "ROLE_USER"
      }
    ],
    "accountNonExpired": true,
    "accountNonLocked": true,
    "credentialsNonExpired": true,
    "enabled": true
  },

而且它似乎在资源服务器端被正确反序列化了:

2016-08-31 12:30:37.530 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.a.i.a.MethodSecurityInterceptor    : Secure object: ReflectiveMethodInvocation: public java.lang.String org.cftap.OAuthResourceController.testUser(); target is of class [org.cftap.OAuthResourceController]; Attributes: [ROLE_USER, ROLE_USER]
2016-08-31 12:30:37.530 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.a.i.a.MethodSecurityInterceptor    : Previously Authenticated: org.springframework.security.oauth2.provider.OAuth2Authentication@ed03ae2: Principal: user2; Credentials: [PROTECTED]; Authenticated: true; Details: remoteAddress=0:0:0:0:0:0:0:1, tokenType=BearertokenValue=<TOKEN>; Granted Authorities: {authority=ROLE_USER}
2016-08-31 12:30:37.530 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter@4cf62e16, returned: 0
2016-08-31 12:30:37.530 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.access.annotation.Jsr250Voter@11e4338f, returned: -1
2016-08-31 12:30:37.530 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.access.vote.RoleVoter@3d5cb07f, returned: -1
2016-08-31 12:30:37.531 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.access.vote.AuthenticatedVoter@2724a21f, returned: 0
2016-08-31 12:30:37.536 DEBUG 32992 --- [nio-9998-exec-1] o.s.b.a.audit.listener.AuditListener     : AuditEvent [timestamp=Wed Aug 31 12:30:37 CEST 2016, principal=user2, type=AUTHORIZATION_FAILURE, data={type=org.springframework.security.access.AccessDeniedException, message=Access is denied}]
2016-08-31 12:30:37.546 DEBUG 32992 --- [nio-9998-exec-1] o.s.s.w.a.ExceptionTranslationFilter     : Access is denied (user is not anonymous); delegating to AccessDeniedHandler

但是,正如您在调试日志中看到的那样,RoleVoter(和 JSR250 之一)投票反对它(尽管允许的角色和委托(delegate)人的权限合在一起),因此发回 403。

我错过了什么重要的事情吗?

提前致谢。

最佳答案

试试看 @RolesAllowed("USER") 而不是 @RolesAllowed("ROLE_USER")

最终您可以使用 hasAuthority("ROLE_USER")hasRole("USER") 而不是 hasRole("ROLE_USER")

这些是 Spring 4 的变化,您可能正在使用一些旧的 Spring 3 文档/文章。

关于 Spring 安全/OAuth : mapping between Principal's authority and role in @RolesAllowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39248342/

相关文章:

spring - 重定向后如何保留请求参数?

java - 如何从@ComponentScan 包中获取接口(interface)列表

java - Spring Security 3.1 中的默认角色

java - 如何在spring中编写/检查auth的权限

带有 OAuth2 丢失 session 的 Spring Security

java - Spring oAuth2 中 OAuth2RestOperations 和 OAuth2RestTemplate 的区别

java - Spring MVC 返回 HTTP 406 响应

spring - 无法使用 CATALINA_HOME/lib 文件夹中的 Spring 和 Spring Security jar

java - spring oauth2自定义登录错误

java - 从 Java 代码调用 Spring 组件