【问题标题】:Filter html content with Spring and Thymeleaf使用 Spring 和 Thymeleaf 过滤 html 内容
【发布时间】:2014-05-01 15:03:14
【问题描述】:

如何通过使用 Spring Boot 与 Spring Security 和 Thymeleaf 来根据身份验证状态过滤 html 内容? 注意:我使用的是 Spring Boot 自动配置。

【问题讨论】:

  • 过滤 html 内容到底是什么意思?您的意思是您希望仅当用户具有特定角色时才呈现 html 的某些部分?
  • 是的,例如登录按钮和注销按钮。

标签: spring spring-security thymeleaf spring-boot


【解决方案1】:

使用 Thymeleaf 的 Spring Security 集成可以实现您想要的(如果您还没有集成这两者,请查看link)。

例如,您可以执行以下操作:

<div sec:authorize="hasRole('ROLE_ADMIN')">
    This content is only shown to administrators.
</div>
<div sec:authorize="hasRole('ROLE_USER')">
    This content is only shown to users.
</div>

【讨论】:

  • 此解决方案不起作用:模板引擎不呈现“sec”关键字(并且自动配置默认允许安全方言)。
  • 你是否在 html 标签中添加了 xmlns:sec="thymeleaf.org/thymeleaf-extras-springsecurity3" ?如果是这样,您是否执行了上面链接中提到的集成步骤?
  • ttp://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd" rel="nofollow" target="_blank">thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> w3.org/1999/xhtml" xmlns:th="thymeleaf.org" xmlns:sec="thymeleaf.org/thymeleaf-extras-springsecurity3" lang="it ">
  • 你得到的堆栈跟踪是什么?
  • 什么都没有。当我查看页面的源代码时,我可以看到“sec”标签。
猜你喜欢
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2023-03-17
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2016-12-21
相关资源
最近更新 更多