java - Thymeleaf 不解释 sec 标签

标签 java maven spring-security thymeleaf

我遇到了一个问题,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/

相关文章:

javascript - Java 中的等效数据结构?

java - (a)Smack 在登录时返回 "service-unavailable (503)"错误()

maven - Maven讲的本地存储库和远程存储库是什么意思?

kotlin - 为什么在Kotlin异步方法中SecurityContextHolder.getContext()。authentication等于null?

java - 覆盖现有的 Spring Security 身份验证

java - 错误: type mismatch - how to persist with hibernate a bi-directional OneToOne mapping using spring-mvc forms

java - 测试二维对象数组中的 null

class - 是否可以使用 reflections-maven 扫描 web-Inf/lib 中 jar 内的类?

Maven Release Build Skip 测试用例失败 - Jenkins

java - 当我尝试发布到我的 spring api 时 403 被禁止?