如果用户具有特定角色,如何附加 CSS 类?

Posted

技术标签:

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

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

类似这样的:

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

我正在使用 Spring Boot、Spring Security 和 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>

【讨论】:

以上是关于如果用户具有特定角色,如何附加 CSS 类?的主要内容,如果未能解决你的问题,请参考以下文章