【问题标题】:Thymeleaf 3.0 Spring Boot + Security integration does not workThymeleaf 3.0 Spring Boot + 安全集成不起作用
【发布时间】:2017-05-14 06:55:01
【问题描述】:

我很难让 Thymeleaf 在基于 Spring Boot 1.4.3 的项目中与 Spring Security 一起使用。

标签,例如

<div sec:authorize="hasAuthority('ADMIN')">

根本没有被解析。

如果我尝试像这样手动添加SpringSecurityDialect

@Bean
public SpringSecurityDialect securityDialect() {
    return new SpringSecurityDialect();
}

我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect

我的依赖项中包含以下内容:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

SpringSecurityDialect 似乎没有被自动配置添加。

在我手动添加 Bean 后,我得到了提到的异常。

这是一个错误还是我遗漏了什么?

我的 Thymeleaf 版本是:

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-extras-java8time.version>3.0.0.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>

【问题讨论】:

标签: java spring-boot spring-security thymeleaf


【解决方案1】:

如果你使用Spring Boot 2.0.0.RELEASE

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

您只需要以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

thymeleaf-extras-springsecurity4 的版本将从spring-boot-starter-parent 继承,并将为3.0.2.RELEASE

感谢@yglodt 指出这一点。


同样在您的模板中添加 spring-security 命名空间 xmlns:sec="http://www.thymeleaf.org/extras/spring-security" 并在 &lt;sec:authorize&gt; 标签中使用 hasRole 而不是 hasAuthority 值:

<div sec:authorize="hasRole('ROLE_ADMIN')">
    ...
</div>

【讨论】:

  • 继承自 Spring Boot 的 thymeleaf-extras-springsecurity4 版本为 2.1.3。它最终通过强制版本为 3.0.1.RELEASE 来工作
  • 您是否需要手动创建模板引擎才能使其工作?
  • @yglodt 感谢您的评论,您是对的,我已将答案更新为spring-boot-starter-parent 版本2.0.0.RELEASE
  • @Jolley71717 Thymeleaf 实际上是一个服务器端 Java 模板引擎,它处理 HTML(也包括 XML、TEXT、JS、CSS、RAW)模板并“动态”生成静态 HTML 页面。但是模板呢 - 当然,您需要手动创建它们并将您的逻辑放在那里。
【解决方案2】:

我以前也有同样的问题。 Thymeleaf SpringSecurity 仅适用于 thymeleaf 的 3.x.x 版本,Spring-boot 附带的版本类似于 2.x.x atm。

查看如何将 v3.x.x 添加到我的项目中,我进入了以下文档页面: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-thymeleaf-3

所以你只需要添加你的依赖,然后在你的属性中添加以下内容来覆盖你的依赖的默认版本的thymeleaf:

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>

【讨论】:

    【解决方案3】:

    要使其正常工作,如果您将 Thymeleaf 3.0.2 与 Spring Boot 1.4 一起使用,则需要强制使用 thymeleaf-extras-springsecurity4 的版本 3.0.1.RELEASE(因为它继承了版本 2.1.2,它不能与百里香叶 3):

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.1.RELEASE</version>
    </dependency>
    

    标签应该使用hasRole函数。

    <div sec:authorize="hasRole('ROLE_ADMIN')">
    

    【讨论】:

      猜你喜欢
      • 2019-10-04
      • 2020-08-05
      • 2014-06-14
      • 2016-10-26
      • 2019-09-12
      • 2020-11-05
      • 2019-08-12
      • 2019-08-25
      • 2014-07-04
      相关资源
      最近更新 更多