Spring Security / OAuth:在Principal的权限和@RolesAllowed中的角色之间进行映射

时间:2016-08-31 11:24:55

标签: spring spring-security spring-security-oauth2

我正在使用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。

资源服务接受由授权服务器生成的令牌(密码授予流程),并向用户-info-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。

我是否错过了重要的事情?

提前致谢。

1 个答案:

答案 0 :(得分:2)

试试 @RolesAllowed("USER")代替@RolesAllowed("ROLE_USER")

最终,您可以使用hasAuthority("ROLE_USER")hasRole("USER")代替hasRole("ROLE_USER")

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