gpt4 book ai didi

grails - 以编程方式访问/检查 Spring Security 分层角色

转载 作者:行者123 更新时间:2023-12-02 14:04:07 34 4
gpt4 key购买 nike

在我的 Grails 项目中,我定义了多个 hierarchical roles使用 Spring Security 插件,例如ROLE_USER > SOME_OTHER_ROLE .当使用 @Secured 保护 Controller 方法时注释它工作得很好。但是,我也想在我的代码中以编程方式检查一个用例的角色。使用以下方法,我总是得到 false即使用户通过分层角色定义继承角色:

request.isUserInRole('SOME_OTHER_ROLE')

此外,以下调用永远不会直接返回继承的角色:
SecurityContextHolder.context?.authentication?.authorities
springSecurityService.getPrincipal().getAuthorities()

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

最佳答案

这似乎是 SecurityContextHolderAwareRequestWrapper 中的一个错误(或至少是一个遗漏)它添加了一个请求包装器来实现 isUserInRole方法。

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

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

关于grails - 以编程方式访问/检查 Spring Security 分层角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9421366/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com