Thymeleaf 3.0 Spring Boot +安全集成不起作用

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

标签如,例如

根本就没有解析。

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

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

我正进入(状态:

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

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

  org.thymeleaf.extras thymeleaf-extras-springsecurity4  

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

在我手动添加Bean之后,我得到了提到的exception。

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

我的Thymeleaf版本是:

 3.0.2.RELEASE 3.0.0.RELEASE 2.1.2 

为了使它工作,如果你使用Thymeleaf 3.0.2与Spring Boot 1.4,你需要强制版本3.0.1.RELEASE of thymeleaf-extras-springsecurity4 (因为它inheritance了版本2.1.2,它不能与Thymeleaf结合使用) 3):

  org.thymeleaf.extras thymeleaf-extras-springsecurity4 3.0.1.RELEASE  

标签应该使用hasRole函数。

 

如果您使用Spring Boot 2.0.0.RELEASE :

  org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE    

您只需要以下依赖项:

  org.springframework.boot spring-boot-starter-security   org.thymeleaf.extras thymeleaf-extras-springsecurity4  

thymeleaf-extras-springsecurity4将inheritance自spring-boot-starter-parent并且将为3.0.2.RELEASE

感谢@ yglodt指出这一点。


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

 
...

我曾经遇到过同样的问题。 Thymeleaf SpringSecurity仅适用于thymeleaf的3.xx版本,而Spring-boot附带的版本类似于2.xx atm。

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

因此,您只需添加依赖项,然后在属性中添加以下内容,以覆盖默认版本的thymeleaf到您的依赖项:

 3.0.2.RELEASE 2.1.1