无法在Spring Boot 2 / Thymeleaf 3中显示已登录的用户

时间:2019-04-28 22:11:22

标签: java spring spring-boot thymeleaf

我一直在使用Spring Boot 2和Thymeleaf 3实现一个基本应用程序。我已经尽力使登录/注销工作正常进行,并且安全层要求在特定页面上进行登录。

我正在将一个非常基本的前端放在一起,并且努力使登录用户的用户名显示出来。

此特定部分的代码段是:

    <span class="user" sec:authorize="isAuthenticated()">
        <div sec:authentication="name"></div>
        <a th:href="@{/logout}">Logout</a>
    </span>

无论情况如何,我也都获得了“注销”链接,这似乎是由于anonymousUser对象中的authenticated=true设置Authentication造成的。

我的Principle对象在请求时登录,看起来像...

principal = {UsernamePasswordAuthenticationToken@10464} "org.springframework.security.authentication.UsernamePasswordAuthenticationToken@ffff4ca0: Principal: org.springframework.security.core.userdetails.User@586034f: Username: admin; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: admin; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: admin"
 principal = {User@11314} "org.springframework.security.core.userdetails.User@586034f: Username: admin; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: admin"
  password = null
  username = "admin"
  authorities = {Collections$UnmodifiableSet@11320}  size = 1
  accountNonExpired = true
  accountNonLocked = true
  credentialsNonExpired = true
  enabled = true
 credentials = null
 authorities = {Collections$UnmodifiableRandomAccessList@11315}  size = 1
 details = {WebAuthenticationDetails@11316} "org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null"
 authenticated = true

name值不起作用的明显原因吗?我还根据我的实际研究和尝试对usernameprinciple.usernameuser进行了尝试,但最终还是在DOM中做到了这一点:

enter image description here

任何帮助将不胜感激。

我的依存关系中有thymeleaf-extras-springsecurity4,模板xmlns:sec="http://www.thymeleaf.org/extras/spring-security"标记中有<html>

0 个答案:

没有答案