问题描述
我有一个使用 Spring Security 和 Thymeleaf 模板的 Spring Boot 应用程序.当控制器由 WebConfigurerAdapter 的子类管理时,我试图在模板中显示登录用户的名字和姓氏.
所以,假设我的 WebConfigurerAdapter 子类看起来像这样
@Configuration公共类 MvcConfig 扩展了 WebMvcConfigurerAdapter{@覆盖public void addViewControllers(ViewControllerRegistry 注册表){registry.addViewController("/some-logged-in-page").setViewName("some-logged-in-page");registry.addViewController("/login").setViewName("login");}....}
我的 User 实体类看起来像这样
@Entity@Table(name = "用户")公共类用户{@ID@GeneratedValue(策略 = GenerationType.IDENTITY)@Column(name = "id", nullable = false, updateable = false)私人长ID;@Column(name="first_name", nullable = false)私人字符串名字;公共字符串 getFirstName() {返回名字;}...}
在我的模板中,我尝试使用像
这样的代码
但是没有用.
我知道可以使用 ControllerAdvise 如下:
@ControllerAdvice公共类 CurrentUserControllerAdvice {@ModelAttribute("当前用户")public UserDetails getCurrentUser(Authentication authentication) {返回(身份验证 == 空)?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>
I have a Spring Boot application that uses Spring Security and Thymeleaf template. I am trying to display the logged-in user's first name and last name in a template when the controller is managed by a subclass of WebConfigurerAdapter.
So, say my WebConfigurerAdapter subclass looks like this
@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");
}
....
}
My User entity class looks like this
@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;
}
...
}
In my template, I have tried using code like
<div sec:authentication="firstName"></div>
But it didn't work.
I know it is possible to use a ControllerAdvise as follows:
@ControllerAdvice
public class CurrentUserControllerAdvice {
@ModelAttribute("currentUser")
public UserDetails getCurrentUser(Authentication authentication) {
return (authentication == null) ? null : (UserDetails) authentication.getPrincipal();
}
}
and then access the details in the template using code like:
<span th:text ="${currentUser.getUser().getFirstName()}"></span>
But this doesn't work with any view controller registered with my class MvcConfig. Rather I will need to make sure each of my controllers are separate classes.
So, could someone kindly point me to a way to automatically insert the logged-in user details to my view, e.g. some-logged-in-page.html in this example? Thanks
It's quite easy to accomplish this, thanks to a hint from Balaji Krishnan.
Basically, I had to add the Thymeleaf Spring Security integration module to my build.gradle file as follows:
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")
Then in my template I just used the following markup:
<span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>
这篇关于如何在所有模板中显示当前登录用户的信息,包括 Spring Security 应用程序中由 WebMvcConfigurerAdapter 管理的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!