[英]Spring boot + Security + Thymeleaf error using ${#authentication.name}
[英]Spring Boot and Thymeleaf using the #authentication utility
使用Spring Boot,spring security starter和thymeleaf,登錄后我無法訪問#authentication實用程序(更准確地說,它是null)。 我沒有做任何特殊的配置(假設啟動器會為我做這個)並且我沒有在我的模板中包含sec:namespace(再次,假設我不需要它 - 我見過的所有示例到目前為止''我也需要它。 我想稱之為: {#authentication.expression('isAuthenticated()')}
作為參考,這是在身份驗證后調用的控制器:
import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/site-admin")
public class VZSiteAdminController {
@RequestMapping(method=RequestMethod.GET)
public String mainScreen(Principal principal){
return "site-admin";
}
}
如果您想從主體對象訪問屬性,您應該這樣:
<div th:text="${#authentication.principal.something}">
The value of the "name" property of the authentication object should appear here.
</div>
這篇文章對我很有幫助,因為我添加了一個存儲在主體對象中的用戶圖像:
<img th:if="${#authentication.principal.image}"
class="img-circle" th:src="${#authentication.principal.image}"
width="100" height="100" alt="place-holder" />
春季靴子百里香啟動器不包含它,你需要添加thymeleaf-extras-springsecurity3 / 4/5作為你的依賴。 https://github.com/thymeleaf/thymeleaf-extras-springsecurity
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.