如何使用Spring启动来解析thymeleaf-extras-springsecurity标签?

时间:2017-01-17 04:46:07

标签: spring spring-mvc spring-boot thymeleaf

我是第一次使用Spring Boot创建网站。我正在使用测试页面显示用户登录后,当用户登录时,屏幕上会显示“已验证”字样。

<!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配置更受欢迎。

4 个答案:

答案 0 :(得分:4)

请尝试在@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

答案 1 :(得分:0)

当您的百里香叶版本为2.1.x时,请使用thymeleaf-extras-springsecurity4(版本为2.1.3.RELEASE)。否则百里香叶模板页面中的hasRole将不起作用。

答案 2 :(得分:0)

您正在使用哪个版本的Spring Boot?

如果是2.2.7.RELEASE,则需要Spring Security 5的Thymeleaf附加功能:

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

不需要任何额外的配置。

答案 3 :(得分:-1)

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

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

2)在上一篇文章中提到的添加内容,将以下bean添加到WebSecurityConfig类中,该类扩展了WebSecurityConfigurerAdapter

@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>