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

当前文章收录状态:
查询中...

引入maven依赖

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
       <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
<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的标签支持 -->
<html xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"><!-- Thymeleaf为我们提供的Spring Security的标签支持 -->
<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 th:text="${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}"></span>
<span th:text="${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}"></span>

或者(前提是导入了标签库)

<span sec:authentication="name"></span>
<span sec:authentication="name"></span>
<span sec:authentication="name"></span>

© 版权声明
THE END
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=270198dipw4ko
点赞5赞赏 分享
When your faith is stronger than your fears, you can make your dreams happen.
当你的信念强于你的胆怯时,你就可以将梦想变为现实了
评论 抢沙发

请登录后发表评论

    暂无评论内容