[英]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.