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>

© 版权声明
THE END
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=270198dipw4ko
点赞5赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容