【问题标题】:How do I get a Spring boot to parse thymeleaf-extras-springsecurity tags?如何让 Spring Boot 解析 thymeleaf-extras-springsecurity 标签?
【发布时间】:2017-06-01 01:12:37
【问题描述】:

我是第一次使用 Spring Boot 创建网站。我正在使用一个测试页面来显示,一旦用户登录,当用户登录时,“Authenticated”字样就会出现在屏幕上。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
<h2>Thymleaf example</h2>
<p sec:authorize="hasRole('ROLE_USER')">
    Authenticated
</p>
</body>
</html>

但是,问题是带有 sec:authorize 的标签仍然未经编辑和解析。因此,无论用户是否登录,都会出现 Authenticated 字样。从控制器打印用户的权限可以确认这一点。

我的 pom.xml 文件具有以下依赖项。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    ... dependencies for mysql and jdbc are omitted.

感谢任何帮助。 注意,我使用的是 Spring Boot,所以 JAVA 配置优先于 XML 配置。

【问题讨论】:

    标签: spring spring-mvc spring-boot thymeleaf


    【解决方案1】:

    请尝试在您的@Configuration(或@SpringBootApplication)类中添加类似以下代码:

    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver, SpringSecurityDialect sec) {
        final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        templateEngine.addDialect(sec); // Enable use of "sec"
        return templateEngine;
    }
    

    请注意,如果您强制 Spring Boot 使用 Thymeleaf 版本 3,则还必须强制使用 thymeleaf-extras-springsecurity4 依赖项的版本 3:

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>3.0.1.RELEASE</version>
    </dependency>
    

    另见this related answer

    【讨论】:

    • addDialect 函数出错。 Exception in thread "main" java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialectCaused by: java.lang.ClassNotFoundException: org.thymeleaf.dialect.IExpressionEnhancingDialect
    • 您使用的是哪个版本的 Thymeleaf?您的类路径中似乎没有 thymeleaf jar!请确保您的 pom.xml 中有以下行并清理您的 Maven 项目:&lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt; &lt;/dependency&gt;
    • Thymeleaf 版本 3.0.2,百里香布局方言为 2.1.1。您键入的依赖项标记已经在其中。我正在处理的站点的其他部分仅使用“th”前缀就可以正常工作。似乎只是 thymeleaf-extra-springsecurity 标签成为问题。如果您需要查看,这是我的整个 pom.xml 文件的 Pastebin:pastebin.com/TpteH3X4
    • 好的,你强制 Thymeleaf 版本 3.0.2,所以尝试将此版本标签添加到您的 thymeleaf-extras-springsecurity4 依赖项:&lt;version&gt;3.0.1.RELEASE&lt;/version&gt;(目前最新版本)。
    • 好的,开始工作了。我只是停止强制版本,由于一些语法更改,不得不稍微修改我的 html 模板文件。感谢您的帮助。
    【解决方案2】:

    请使用thymeleaf-extras-springsecurity4(版本为2.1.3.RELEASE),当你的thymeleaf版本为2.1.x。否则thymeleaf模板页面中的角色将不起作用。

    【讨论】:

      【解决方案3】:

      您使用的是哪个版本的 Spring Boot?

      如果是 2.2.7.RELEASE,那么你需要 Thymeleaf extras for Spring Security 5:

           <dependency>
              <groupId>org.thymeleaf.extras</groupId>
              <artifactId>thymeleaf-extras-springsecurity5</artifactId>
              <version>3.0.4.RELEASE</version>
          </dependency>
      

      并且不需要额外的配置。

      【讨论】:

        【解决方案4】:

        1) 请在标签内的 prom.xml 文件中添加以下内容以解析 java.lang.ClassNotFoundException: org.thymeleaf.dom.Attribute Exeption。

        <thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
        

        2)添加上一篇文章中提到的,将以下bean添加到扩展WebSecurityConfigurerAdapterWebSecurityConfig类中

        @Bean
        public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver, SpringSecurityDialect sec) {
            final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver);
            templateEngine.addDialect(sec); // Enable use of "sec"
            return templateEngine;
        }
        

        3) 添加如下依赖,版本请备注,需要强制3.0.1.RELEASE版本

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-11
          • 2020-03-14
          • 1970-01-01
          • 2016-01-27
          • 1970-01-01
          • 1970-01-01
          • 2021-09-25
          • 2018-11-30
          相关资源
          最近更新 更多