我很难让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>
3 回答
为了使其正常工作,如果您在Spring Boot 1.4中使用
Thymeleaf
3.0.2,则需要强制3.0.1.RELEASE
的thymeleaf-extras-springsecurity4
(因为它继承了与Thymeleaf 3无法兼容的版本2.1.2):标签应该使用
hasRole
功能 .如果您使用Spring Boot 2.0.0.RELEASE:
您只需要以下依赖项:
thymeleaf-extras-springsecurity4
的版本将从spring-boot-starter-parent
继承,并且将是3.0.2.RELEASE .感谢@ yglodt指出这一点 .
另外在模板中添加spring-security命名空间
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
并在<sec:authorize>
标记中使用hasRole
而不是hasAuthority
值:我曾经有过同样的问题 . 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到您的依赖项: