为什么我的 thymeleaf-extras-springsecurity6 不工作?我已将 thymeleaf-extras-springsecurity6 添加到 pom 文件中?我正在使用 Spring Security 版本 6.2.2 和 Thymeleaf 版本 3.1.2。
按照https://github.com/thymeleaf/thymeleaf-extras-springsecurity/tree/3.1-master的说明进行操作,但仍然不起作用。
这是我的pom文件。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>phone_store</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>phone_store</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的html
<!doctype html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg fixed-top" style="color: #6c6d73;background: #f2f4ff;" th:fragment="navbar">
<div class="container" style="max-width: 872px">
<a class="navbar-brand" th:href="@{/Home}">
<img th:src="@{/images/utils/logo.png}" alt="" style="max-width: 130px">
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar"
aria-controls="offcanvasNavbar" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasNavbar"
aria-labelledby="offcanvasNavbarLabel">
<div class="offcanvas-header">
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<ul class="navbar-nav justify-content-center d-flex align-items-center flex-grow-1">
<li class="nav-item">
<a class="nav-link active general_Text" aria-current="page" th:href="@{/Home}">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle general_Text" href="#" role="button"
data-bs-toggle="dropdown"
aria-expanded="false">
Collection
</a>
<ul class="dropdown-menu" style="min-width: 360px">
<div class="row">
<div class="col-md-4 col-sm-4">
<li><a class="dropdown-item" href="#">Iphone</a></li>
</div>
<div class="col-md-4 col-sm-4">
<li><a class="dropdown-item" href="#">SamSung</a></li>
</div>
<div class="col-md-4 col-sm-4">
<li><a class="dropdown-item" href="#">Nokia</a></li>
</div>
</div>
</ul>
</li>
<li class="nav-item col-8">
<form class="d-flex" role="search">
<input class="form-control no-border me-2 col-8" type="search" placeholder="Search"
aria-label="Search">
</form>
</li>
</ul>
<ul class="navbar-nav justify-content-end d-flex flex-column flex-lg-row align-items-center">
<li class="nav-item me-1">
<a class="nav-link"
th:href="@{/Auth/SignIn}" style="cursor: pointer">
<i class="fa-regular fa-user fa-xl me-1 icon"></i>
</a>
</li>
<li class="nav-item me-1">
<a class="nav-link "
href="#" style="cursor: pointer">
<i class="fa-solid fa-cart-shopping fa-xl me-1 icon"></i>
</a>
</li>
<li class="nav-item me-1" sec:authorize="isAuthenticated()">
<a class="nav-link "
href="#" style="cursor: pointer">
<i class="fa-solid fa-door-open fa-xl me-1 icon"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
</body>
</html>
*sec:authorize="isAuthenticated()" 不起作用
如果用户已通过身份验证,则应执行 sec:authorize="isAuthenticated()" 内的代码
我之前遇到过类似的问题,其中 sec:authentication="name" 不适用于我。阅读完https://github.com/thymeleaf/thymeleaf-extras-springsecurity/tree/3.1-master?tab=readme-ov-file后,为了解决我们需要org.thymeleaf.extras.springsecurity5.dialect .SpringSecurityDialect 或 org.thymeleaf.extras.springsecurity6.dialect.SpringSecurityDialect,我们需要在配置中配置此方言文件。
这是我的配置文件:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity6.dialect.SpringSecurityDialect;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
@Configuration
public class ThymeleafConfig {
@Bean
public SpringSecurityDialect springSecurityDialect() {
return new SpringSecurityDialect();
}
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(springSecurityDialect());
return templateEngine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
}
这是我的 html 上的示例代码:
<li class="nav-item">
<a class="nav-link" sec:authorize="isAuthenticated()" th:href="@{/orders}">Orders</a>
</li>
所有工作, 未经身份验证的用户: 未经身份验证的用户
经过身份验证的用户: 经过身份验证的用户