问题描述
我很难让Thymeleaf在我的基于Spring Boot 1.4.3的项目中使用Spring Security。
I struggle to get Thymeleaf to work with Spring Security in my Spring Boot 1.4.3 based project.
标签如
<div sec:authorize="hasAuthority('ADMIN')">
根本就没有解析。
如果我尝试像这样手动添加 SpringSecurityDialect
:
If I try to add the SpringSecurityDialect
manually like this:
@Bean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}
我收到:
Exception in thread "main" java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect
我在我的依赖项中包含以下内容:
I have included the following in my dependencies:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
似乎没有添加 SpringSecurityDialect
通过自动配置。
The SpringSecurityDialect
does not seem to be added by the autoconfiguration.
在我手动添加Bean之后,我得到了提到的异常。
After I add the Bean manually, I get the mentioned exception.
这是一个我错过了什么?
Is this a bug or am I missing something?
我的Thymeleaf版本是:
My Thymeleaf versions are:
<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>
推荐答案
要使其正常工作,如果您使用 Thymeleaf
3.0.2使用Spring Boot 1.4,你需要强制 3.0.1.RELEASE
thymeleaf-extras-springsecurity4
(因为它继承了与Thymeleaf 3无法合作的版本2.1.2):
To get it working, if you are using Thymeleaf
3.0.2 with Spring Boot 1.4, you need to force version 3.0.1.RELEASE
of thymeleaf-extras-springsecurity4
(because it inherits version 2.1.2 which does not work in combination with Thymeleaf 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')">
这篇关于Thymeleaf 3.0 Spring Boot +安全集成不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!