【问题标题】:sec:authentication is not working in Spring Boot Securitysec:authentication 在 Spring Boot Security 中不起作用
【发布时间】:2021-10-11 13:35:00
【问题描述】:

我在显示未显示经过身份验证的用户全名时遇到问题。

我看不到sec:authentication 中定义的任何值。

即使我定义了 sprng 安全性并将其嵌入到支持 thymeleaf 的 html 代码中,该代码也无法正常工作。

我该如何解决?

这是我的 CustomerUserDetails 类

public class CustomerUserDetails implements UserDetails{

public String getFullName() {
        
        LOGGER.info("CustomerUserDetails | getFullName: " + customer.getFirstName() + " " + customer.getLastName());
        
        return customer.getFirstName() + " " + customer.getLastName();
    }


}

这是下面显示的依赖项。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

下面是我的代码。

我只显示下面显示的这段代码来显示经过身份验证的用户全名

<a class="nav-link" th:href="@{/account_details}" ><b>[[${#request.userPrincipal.principal.fullName}]]</b></a>

为什么 sec:authentication 不起作用?

我该如何解决这个问题?

【问题讨论】:

    标签: spring-boot spring-security thymeleaf


    【解决方案1】:

    你能说明什么不起作用吗?

    尝试类似:

    <a class="nav-link" th:href="@{/account_details}"><b sec:authentication="name"/></a>
    

    验证您是否有权访问 Authentication 实例。然后试试:

    <a class="nav-link" th:href="@{/account_details}"><b sec:authentication="details"/></a>
    

    <a class="nav-link" th:href="@{/account_details}"><b sec:authentication="details.fullName"/></a>
    

    验证您的功能。

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2018-06-03
      • 2021-09-23
      • 2016-12-15
      相关资源
      最近更新 更多