Thymeleaf不解释sec标签 [英] Thymeleaf not interpreting sec tags

查看:393
本文介绍了Thymeleaf不解释sec标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,在我的spring boot项目中,thymleaf无法识别sec标签.例如下面的sec:authentication未被解释,并显示在浏览器的html中

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?

推荐答案

可能有些东西可能没有正确设置.无论如何,通过添加缺少的依存关系或更改所使用的依存关系,往往可以解决此问题.因此,首先,尝试更改为springsecurity5.并添加以下@Bean.

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.

配置

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为您进行管理.

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.

这篇关于Thymeleaf不解释sec标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆