【问题标题】:Spring security @Secured always need ROLE_ prefix, how we can remove the prefixSpring security @Secured 总是需要 ROLE_ 前缀,我们如何去掉前缀
【发布时间】:2020-06-10 11:51:54
【问题描述】:

我正在检查我的控制器中的角色“XXXXX”。但它只有在我添加前缀 "ROLE_" 时才有效。

@Secured("XXXXX") 不工作,但是当我添加 ROLE_ 前缀时,它工作正常(@Secured("ROLE_XXXXX") 工作正常)。如何在我的 Spring Boot 版本 2 项目中覆盖此功能?

【问题讨论】:

标签: spring-boot spring-security


【解决方案1】:

选项 1。您可以使用 @RolesAllowed("role_name") 代替 @Secured

您需要通过使用 @EnableGlobalMethodSecurity(jsr250Enabled=true) 注释 @Configuration 类来启用 JSR-250 注释支持

选项 2. 您可以使用 @PreAuthorize("hasRole('ROLE_NAME_WITHOUT_ROLE_PREFIX')") 代替 @Secured

您需要通过使用 @EnableGlobalMethodSecurity(prePostEnabled=true) 注释 @Configuration 类来启用 PreAuthorize 注释支持

【讨论】:

    猜你喜欢
    • 2012-01-09
    • 2016-11-03
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2020-07-01
    • 1970-01-01
    • 2014-03-04
    相关资源
    最近更新 更多