我遇到了一个问题,thymleaf 无法识别我的 Spring Boot 项目中的 sec 标签。例如下面的 sec:authentication 未被解释,并按浏览器中的 html 中的形式显示
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head th:replace="fragments/header"> </head>
<body>
<div id="container">
Roles <span sec:authentication="principal.authorities"></span>
</div>
<footer>
<div th:replace="fragments/footer"></div>
</footer>
</body>
</html>
通过阅读周围的内容,我需要以下依赖项,我已将其添加到我的项目中。
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
但还是没有运气。上述依赖似乎是大多数人的解决方案,你知道我还可能缺少什么吗?
最佳答案
可能有一些设置不正确。无论如何,这个问题往往可以通过添加缺少的依赖项或更改您正在使用的依赖项来解决。因此,首先尝试更改为 springsecurity5
。并添加以下@Bean
.
配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
@Configuration
public class LeafConfig {
@Bean
public SpringSecurityDialect springSecurityDialect(){
return new SpringSecurityDialect();
}
}
POM
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
此外,如果您使用 <artifactId>spring-boot-starter-parent</artifactId>
,不要向您的 Thymeleaf Extras 添加任何版本,让 Spring Boot 为您管理。
关于java - Thymeleaf 不解释 sec 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559877/