【问题标题】:Spring security web check if currently authenticated user has roleSpring security web 检查当前经过身份验证的用户是否具有角色
【发布时间】:2017-09-21 04:01:12
【问题描述】:

如何检查当前经过身份验证的用户是否具有特定角色?

我为旧版本的 Spring Security 找到了一些较旧的解决方案,但在 Spring 的当前版本中它不起作用,所以我正在寻找类似 @​​987654321@ 但在 spring-boot-starter-security 版本 1.5.2 中的解决方案。

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    验证 authorities 长度是否 >0 :

    Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();

    如果他有角色,然后与你的角色进行比较

    【讨论】:

    • 感谢快速回复,我自己解决了问题,但是我可以在 thymeleaf 中做到这一点有点没有意义:“sec:authorize="hasRole('ROLE_ADMIN')” 但在 java 中不行代码。
    • 你最好也验证三个级别的安全性,(1)在你的代码java中,(2)在(jsp或thymeleaf)中,和(3)在xml页面ACCEC配置中,如@ 987654322@
    猜你喜欢
    • 2015-08-09
    • 2014-12-20
    • 2016-02-13
    • 2020-10-20
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多