引入maven依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
导入标签库(第二个标签库springsecurity标签库在没导入时标签也起了作用):
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><!-- Thymeleaf为我们提供的Spring Security的标签支持 -->
在网页中使用标签:
<span th:text="${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}"></span>
或者(前提是导入了标签库)
<span sec:authentication="name"></span>