Spring Security/OAuth:@RolesAllowed中Principal的权限与角色的映射

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

我正在研究 Spring OAuth,实现了授权服务器和资源服务器。资源服务器使用 user-info-uri 解码令牌。

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

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

有三个用户,在授权服务器端管理:user1 ROLE_ADMIN,user2 和 user3 ROLE_USER。

资源服务接受授权服务器生成的令牌(密码授予流程)并向用户信息 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 之一)投票反对它(尽管允许的角色和委托人的权限合在一起),因此发回 403。

我是不是漏掉了什么重要的东西?

提前致谢。

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

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

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