如何在所有模板中显示当前登录用户的信息,包括Spring Security应用程序中由WebMvcConfigurerAdapter管理的视图

时间:2016-01-29 17:19:50

标签: java spring spring-mvc spring-security thymeleaf

我有一个使用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?感谢

8 个答案:

答案 0 :(得分:13)

由于Balaji Krishnan的暗示,这很容易实现。

基本上,我必须将Thymeleaf Spring Security集成模块添加到我的build.gradle文件中,如下所示:

compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")

然后在我的模板中,我使用了以下标记:

<span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>

答案 1 :(得分:7)

使用Spring Security 4和Thymeleaf 3时:

<span th:text="${#authentication.getPrincipal().getUsername()}"></span>

答案 2 :(得分:2)

使用Spring Boot 2.2.1时

对于行家,将这些行添加到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>

答案 3 :(得分:1)

这个结构适合我(春季靴子1.5和2.0 /百里香叶3):
这里记录(页面底部)Thymeleaf + Spring Security integration basics

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>

我希望这有帮助!
祝你有愉快的一天! 托马斯

答案 4 :(得分:1)

对我来说,在使用Spring Boot 2.1.2时,我需要使用以下内容

<span th:text="${#authentication.getPrincipal()}"></span> <!-- No ".getUsername()"-->

使用thymeleaf-extras-springsecurity5

答案 5 :(得分:0)

正如@ B378所说,在使用Spring Security 4和Thymeleaf 3时,必须使用以下内容。

<span th:text="${#authentication.getPrincipal().getUsername()}"></span>

因为spring security在内部使用UserDetails。而且UserDetails包含一个名为getUsername()的函数。

答案 6 :(得分:0)

此页面很有用。感谢所有的答复。 请注意,如果您使用的是最新的Spring Boot,即版本2.1.0,则需要使用以下内容:thymeleaf-extras-springsecurity5

LINK

答案 7 :(得分:0)

对于thymleaf 4及更高版本,您必须修改一些类才能获取信息。这是您的操作方法:

首先,在UserDetails类中添加用户getUser(){return this.user;}的getter,然后将该对象推入UserDetailsS​​ervice对象。没有这些更改,thymleaf将无法解析您的HTML文件。

然后您可以获得以下信息:

<span sec:authentication="principal.user.name">