如何让Spring Boot解析thymeleaf-extras-springsecurity标签?

7

我第一次使用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个回答

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

还可以参考这个相关答案


addDialect函数出现错误。 Exception in thread "main" java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect 以及 Caused by: java.lang.ClassNotFoundException: org.thymeleaf.dialect.IExpressionEnhancingDialect - needoriginalname
你使用的Thymeleaf版本是哪个?看起来你的类路径中没有thymeleaf jar!请确保在你的pom.xml文件中有以下行,并清理你的Maven项目: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> - Robert Hume
1
Thymeleaf版本为3.0.2,使用的thymeleaf布局方言为2.1.1。您输入的依赖标签已存在。我正在处理的网站的其他部分仅使用"th"前缀,运行正常。似乎只有thymeleaf-extra-springsecurity标签存在问题。 如果需要查看完整的pom.xml文件,请访问此Pastebin链接:http://pastebin.com/TpteH3X4 - needoriginalname
好的,你正在强制使用Thymeleaf版本3.0.2,因此请尝试将此版本标签添加到你的thymeleaf-extras-springsecurity4依赖项中:<version>3.0.1.RELEASE</version>(目前最新版本)。 - Robert Hume
如果最新版本无法使用,请考虑使用thymeleaf-extras-springsecurity42.1.3.RELEASE版本。 - WildDev
1
好的,搞定了。我只是停止强制版本,因为一些语法变化,不得不修改我的HTML模板文件。感谢您的帮助。 - needoriginalname

0
请在您的thymeleaf版本为2.1.x时使用thymeleaf-extras-springsecurity4(版本为2.1.3.RELEASE),否则在thymeleaf模板页面中使用hasRole将无法正常工作。

0
你使用的Spring Boot版本是哪个?
如果是2.2.7.RELEASE,那么你需要为Spring Security 5安装Thymeleaf extras:
     <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        <version>3.0.4.RELEASE</version>
    </dependency>

而且不需要额外的配置。


-1
1)请将以下内容添加到您的prom.xml文件中的标签内,以解决java.lang.ClassNotFoundException: org.thymeleaf.dom.Attribute异常。
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接