【问题标题】:Thymeleaf + Spring Security Expression languageThymeleaf + Spring 安全表达式语言
【发布时间】:2015-12-31 23:15:56
【问题描述】:

我正在尝试从 Thymeleaf 的 Spring Security 中获取 UserDetails 对象。我尝试过各种 SpEl 的东西,比如:

${#authentication.principal.blahblah}

我正在尝试将其分配给 Javascript 变量:

var foo="${#authentication.principal.blahblah}";

我使用 Spring Boot 1.2.5 并声明了 thymeleaf-extras-springsecurity4 依赖项。

【问题讨论】:

  • 您是否尝试过先打印出类似的内容? <span sec:authentication="principal.firstName"></span>。你也可以查看这个帖子:stackoverflow.com/questions/29758675/…
  • Spring Boot 1.2 默认使用 Spring Security 3,但您使用的是 Thymeleaf 的 Spring Security 4 集成。这要么会破坏事情,要么你的问题不完整,因为你做了一些你没有描述的更多配置。

标签: spring-security spring-boot thymeleaf


【解决方案1】:

试试

<script th:inline="javascript">
var foo = /*[[${#authentication.principal.blahblah}]]*/;
</script>

【讨论】:

    【解决方案2】:

    仅使用模式/*[[${#authentication.principal.username}]]*/ 可能会发生类似 NPE 的异常。所以我通常使用下面的表达式。

    /*[# sec:authorize="isAuthenticated()"]*/
    const loginUserName = /*[[${#authentication.principal.username}]]*/ 'admin'
    /*[/]*/
    

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2018-11-23
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2019-09-12
      相关资源
      最近更新 更多