【问题标题】:How to append a css class if user has specific role?如果用户具有特定角色,如何附加 CSS 类?
【发布时间】:2019-01-12 16:56:02
【问题描述】:

仅当用户具有特定角色时,如何将类附加到具有th:classappend 的元素?

类似这样的:

<div th:classappend="${hasRole('EDITOR')?'glow':''}"></div>

我正在使用 Spring Boot、Spring Security 和 Thymeleaf。

【问题讨论】:

    标签: spring-mvc spring-boot spring-security authorization thymeleaf


    【解决方案1】:

    我在Thymeleaf "extras" Spring Security github page找到了解决方案:

    <div th:classappend="${#authorization.expression('hasRole(''EDITOR'')')?'glow':''}"></div>
    

    【讨论】:

      【解决方案2】:

      你可以使用request变量,它指向当前的HttpServletRequest:

      <div th:classappend="${#request.isUserInRole('EDITOR')?'glow':''}"></div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 2021-10-25
        • 2016-07-13
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        相关资源
        最近更新 更多