Thymeleaf 中 Spring Security 的使用
Spring Security 可以在一些视图技术中进行控制显示效果。例如:
JSP 或 Thymeleaf。在非前后端分离且使用 Spring Boot 的项目中多使用 Thymeleaf 作为视图展示技术。Thymeleaf对Spring Security 的支持都放在 thymeleaf-extras-springsecurityX 中,目前最新版本为 5。
所以需要在项目中添加此 jar 包的依赖和 thymeleaf 的依赖。
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在 html 页面中引入 thymeleaf 命名空间和 security 命名空间
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">