【问题标题】:Thymeleaf , change sec:authorize hasRole string parameter to Java constantThymeleaf ,将 sec:authorize hasRole 字符串参数更改为 Java 常量
【发布时间】:2016-04-17 14:30:48
【问题描述】:

我想在我的 Thymeleaf 模板中使用我的 Java 类常量和 hasRole。

今天我用:

<div sec:authorize="${hasRole('USR')}">
...
</div>

但我想使用我的常量(在我的 java 类中声明)

public class Consts{
  public static final String USR_CONST = "USER"; 
}

如何将字符串值('USR',hasRole 参数)更改为常量 USR_CONST?

【问题讨论】:

标签: spring spring-security thymeleaf


【解决方案1】:

您可以使用 SpEl Type Operator T 访问静态常量。例如:

<div sec:authorize="${hasRole(T(Consts).USR_CONST)}">
    ...
</div>

您需要在T(...) 中指定Consts 类的完全限定路径

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 2020-06-12
    • 2015-02-24
    • 2022-01-19
    • 2021-08-10
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多