SpringSecurity在前端Thyemleaf页面中获取登录的用户名

_

引入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>

Spring Security 使用logoutUrl和logoutSuccessUrl跳转显示404异常错误 2022-01-05
IntelliJ IDEA 试用重置插件 2022-01-11