Contents

在 Spring Security 中获取用户详细信息

1. 概述

本教程将展示如何在 Spring Security 中检索用户详细信息。

当前经过身份验证的用户可通过 Spring 中的许多不同机制获得。让我们首先介绍最常见的解决方案——编程访问。

2. 获取 Bean 中的用户

检索当前经过身份验证的主体的最简单方法是通过对SecurityContextHolder的静态调用:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();

这个片段的一个改进是在尝试访问之前首先检查是否有经过身份验证的用户:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
    String currentUserName = authentication.getName();
    return currentUserName;
}

像这样进行静态调用当然有缺点,代码的可测试性降低是最明显的问题之一。相反,我们将探索针对这个非常常见的需求的替代解决方案。

3. 在控制器中获取用户

我们在*@Controller*注解的 bean中有其他选项。

我们可以直接将主体定义为方法参数,它会被框架正确解析:

@Controller
public class SecurityController {
    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserName(Principal principal) {
        return principal.getName();
    }
}

或者,我们也可以使用身份验证令牌

@Controller
public class SecurityController {
    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserName(Authentication authentication) {
        return authentication.getName();
    }
}

Authentication类的 API非常开放,因此框架尽可能保持灵活。因此,Spring Security 主体只能作为对象检索,并且需要转换为正确的UserDetails实例

UserDetails userDetails = (UserDetails) authentication.getPrincipal();
System.out.println("User has authorities: " + userDetails.getAuthorities());

最后,这里直接来自 HTTP 请求

@Controller
public class GetUserWithHTTPServletRequestController {
    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserNameSimple(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        return principal.getName();
    }
}

4.通过自定义界面获取用户

为了充分利用 Spring 依赖注入并能够在任何地方检索身份验证,而不仅仅是在*@Controller beans*中,我们需要将静态访问隐藏在一个简单的外观后面:

public interface IAuthenticationFacade {
    Authentication getAuthentication();
}
@Component
public class AuthenticationFacade implements IAuthenticationFacade {
    @Override
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}

外观暴露了Authentication对象,同时隐藏了静态状态并保持代码解耦和完全可测试:

@Controller
public class GetUserWithCustomInterfaceController {
    @Autowired
    private IAuthenticationFacade authenticationFacade;
    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserNameSimple() {
        Authentication authentication = authenticationFacade.getAuthentication();
        return authentication.getName();
    }
}

5. JSP中获取用户

通过利用 Spring Security Taglib 支持,还可以在 JSP 页面中访问当前经过身份验证的主体。 首先,我们需要在页面中定义标签:

<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

接下来,我们可以参考principal

<security:authorize access="isAuthenticated()">
    authenticated as <security:authentication property="principal.username" /> 
</security:authorize>

6. 在 Thymeleaf 中获取用户

Thymeleaf 是一个现代的服务器端 Web 模板引擎,与Spring MVC框架 具有良好的集成。

让我们看看如何使用 Thymeleaf 引擎访问页面中当前经过身份验证的主体。 首先,我们需要添加thymeleaf-spring5thymeleaf-extras-springsecurity5 依赖项来集成 Thymeleaf 和 Spring Security:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>

现在我们可以使用** sec:authorize 属性来引用 HTML 页面中的主体**:

<html xmlns:th="https://www.thymeleaf.org" 
  xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<body>
    <div sec:authorize="isAuthenticated()">
      Authenticated as <span sec:authentication="name"></span></div>
</body>
</html>