【问题标题】:How to list Spring Security authorities needed to access Spring Boot endpoints?如何列出访问 Spring Boot 端点所需的 Spring Security 权限?
【发布时间】:2019-08-07 16:36:56
【问题描述】:

我有一个由 Spring Security 保护的 Spring Boot 应用程序。为了保护对特定端点的访问,我们使用@PreAuthorize 注释,例如@PreAuthorize("hasAnyAuthority('first_role','second_role','third_role')")

我想知道是否有可能生成(静态或动态)端点和权限之间的映射列表,以查看哪个端点由哪些权限保护。

所以作为一个输出,我想要这样的东西:

first_role -> endpoint_A, endpoint_B, endpoint_C
second_role -> endpoint_B
third_role -> endpoint_A, endpoint_C

或至少:

endpoint_A -> first_role, third_role
endpoint_B -> first_role, second_role
endpoint_C -> first_role, third_role

你知道有什么解决办法吗?

【问题讨论】:

  • AFAIK 没有内置方式,但您可以自己编写。您必须检查所有类/方法并查看注释。为方便起见,将@PreAuthorize 更改为@Secured,它只允许授权,不允许SPEL。
  • 我的问题也需要同样的解决方案:我有一个网关,在我的 ResourceServerConfigurerAdapter 类中,我添加了所有端点的 antMatchers 以及所需的角色,我想知道哪个端点由哪个角色保护,我想要一个可以给我的服务,然后我会在我的招摇文档上显示这些有用的信息。

标签: spring spring-boot spring-security


【解决方案1】:

您可以在配置类中保护您的端点并删除 @PreAuthorize 注释。我建议您阅读有关security expressions 的春季文档。您可以使用 antmatchers 来保护特定权限组、角色等的 URL。

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2020-01-15
    • 2019-09-21
    • 2014-06-02
    • 2020-09-15
    相关资源
    最近更新 更多