2012-02-23 102 views
2

在我的Grails项目中,我使用Spring Security插件定义了多个hierarchical rolesROLE_USER > SOME_OTHER_ROLE。当使用@Secured注释来保护控制器方法时,它的工作原理很好。不过,我也想在我的代码中以编程方式检查角色的用例。使用下面的方法,我总是得到false即使用户将继承通过分层角色定义的角色:以编程方式访问/检查Spring Security分层角色

request.isUserInRole('SOME_OTHER_ROLE') 

而且下面的调用不会直接返回所继承的角色:

SecurityContextHolder.context?.authentication?.authorities 
springSecurityService.getPrincipal().getAuthorities() 

是否有办法检查用户是否也具有继承角色?

回答

2

这看起来像是SecurityContextHolderAwareRequestWrapper中的一个错误(或至少是遗漏),它增加了一个请求包装来实现isUserInRole方法。

您可以使用roleVoter bean的extractAuthorities方法。为它添加依赖注入(def roleVoter),然后致电

def allRoles = roleVoter.extractAuthorities(
     SecurityContextHolder.context.authentication) 
+0

谢谢,Burt!奇迹般有效。我很快就会为此打开一张票。 – 2012-02-23 22:21:14

+0

这是Spring Security的[Jira门票](https://jira.springsource.org/browse/SEC-1925),供将来参考。 – 2012-02-23 22:54:29

相关问题