![](/img/trans.png)
[英]CQ5: How can I propagate a logged-in user's security context to an integrated web application?
[英]How to display current logged-in user's information in all templates including view managed by WebMvcConfigurerAdapter in Spring Security application
我有一個 Spring Boot 應用程序,它使用 Spring Security 和 Thymeleaf 模板。 當 controller 由 WebConfigurerAdapter 的子類管理時,我試圖在模板中顯示登錄用戶的名字和姓氏。
所以,假設我的 WebConfigurerAdapter 子類看起來像這樣
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/some-logged-in-page").setViewName("some-logged-in-page");
registry.addViewController("/login").setViewName("login");
}
....
}
我的用戶實體 class 看起來像這樣
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;
@Column(name="first_name", nullable = false)
private String firstName;
public String getFirstName() {
return firstName;
}
...
}
在我的模板中,我嘗試使用類似的代碼
<div sec:authentication="firstName"></div>
但它沒有用。
我知道可以按如下方式使用 ControllerAdvise:
@ControllerAdvice
public class CurrentUserControllerAdvice {
@ModelAttribute("currentUser")
public UserDetails getCurrentUser(Authentication authentication) {
return (authentication == null) ? null : (UserDetails) authentication.getPrincipal();
}
}
然后使用如下代碼訪問模板中的詳細信息:
<span th:text ="${currentUser.getUser().getFirstName()}"></span>
但這不適用於在我的 class MvcConfig 中注冊的任何視圖 controller。 相反,我需要確保我的每個控制器都是單獨的類。
那么,有人可以告訴我一種自動將登錄用戶詳細信息插入到我的視圖中的方法,例如本例中的 some-logged-in-page.html 嗎? 謝謝
多虧了 Balaji Krishnan 的提示,這很容易實現。
基本上,我必須將 Thymeleaf Spring Security 集成模塊添加到我的 build.gradle 文件中,如下所示:
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")
然后在我的模板中,我只使用了以下標記:
<span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>
使用 Spring Security 4 和 Thymeleaf 3 時:
<span th:text="${#authentication.getPrincipal().getUsername()}"></span>
使用 Spring Boot 2.2.1 時。
對於 maven,將這些行添加到 pom.xml
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency>
在百里香葉
<span th:text="${#authentication.getPrincipal().getUsername()}"></span> <span th:text="${#authentication.getPrincipal().authorities}"></span>
這個構造對我有用(spring boot 1.5 和 2.0/thymeleaf 3):
它記錄在此處(頁面底部) Thymeleaf + Spring Security 集成基礎知識
Logged user: <span sec:authentication="name">Bob</span>
不要忘記在視圖的 html 部分包含 sec 標記:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
</head>
<body>
我希望這有幫助!
祝你今天過得愉快!
托馬斯
正如@B378 所說,使用 Spring Security 4 和 Thymeleaf 3 時:您必須使用以下內容。
<span th:text="${#authentication.getPrincipal().getUsername()}"></span>
因為 spring security 在內部使用 UserDetails。 UserDetails 包含一個名為 getUsername() 的函數。
對我來說,使用 Spring boot 2.1.2 時,我需要使用以下內容
<span th:text="${#authentication.getPrincipal()}"></span> <!-- No ".getUsername()"-->
使用 thymeleaf-extras-springsecurity5
這個頁面很有用。 感謝所有的答復。 請注意,如果您使用的是最新的 Spring Boot 即版本2.1.0
那么您需要使用以下內容: thymeleaf-extras-springsecurity5
對於 thymleaf 4 及更高版本,您必須修改一些類才能獲取信息。 以下是您如何做到這一點:
首先,在 UserDetails 類中添加用戶getUser(){return this.user;}
的 getter,然后將該對象推getUser(){return this.user;}
UserDetailsService 對象中。 如果沒有這些更改,thymleaf 將不會解析您的 HTML 文件。
然后你可以得到如下信息:
<span sec:authentication="principal.user.name">
在嘗試打印他的詳細信息之前,不要忘記檢查用戶是否經過身份驗證
<th:block th:if="${#authorization.expression('isAuthenticated()')}">
<span>Hello</span>
<span th:text="${#authentication.getPrincipal().getFullName()}"></span>
</th:block>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.