问题
I've ran into an issue where thymleaf is not recognising the sec tag in my spring boot project. e.g. the below the sec:authentication is not being intepreted and appears as is in the html in the browser
<!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>
From reading around i need the following dependency, which I've added to my project.
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
But still no luck. The above dependency seems to be the solution for most people, any ideas what else i could be missing?
回答1:
There could be a few things that might not be set correctly. Anyways, this issue tend to always resolve by adding missing dependencies or changing the ones you are using. So, first, try changing to springsecurity5. And add the following @Bean.
Configuration
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>
Also, if you are using <artifactId>spring-boot-starter-parent</artifactId>, don't add any version to your Thymeleaf Extras, let Spring Boot manage that for you.
来源:https://stackoverflow.com/questions/53559877/thymeleaf-not-interpreting-sec-tags