我有一个使用 Spring Security 和 Thymeleaf 模板的 Spring Boot 应用程序。当控制器由 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");
}
....
}
我的用户实体类看起来像这样
@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>
但这不适用于在我的类 MvcConfig 中注册的任何视图控制器。相反,我需要确保我的每个控制器都是单独的类。
所以,有人可以告诉我一种自动将登录用户详细信息插入到我的视图中的方法,例如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,然后将该对象推送到 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>
2023 年 3 月,为了显示来自本地或社交登录(如 Facebook)的用户名,我采用了这种方法:(当我使用时)
pom.xml
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
html 顶部
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
html 正文
<h3 th:inline="text">
user: <span sec:authentication="name">not found</span>
</h3>
豆类
@AllArgsConstructor
public class CustomOAuth2User implements OAuth2User {
private OAuth2User oAuth2User;
@Override
public String getName() {
return oAuth2User.getAttribute("name");
}
}