• 在Spring Security中检索用户信息


    1. 概述

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

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

    延伸阅读:

    使用 Spring 安全性跟踪登录用户

    在使用 Spring 安全性构建的应用程序中跟踪登录用户的快速指南。

    Spring 安全性 – 角色和特权

    如何映射 Spring 安全性应用程序的角色和权限:设置、身份验证和注册过程。

    Spring 安全性 – 重置密码

    每个应用程序都应允许用户更改自己的密码,以防忘记密码。

    2. 在Bean中获取用户

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

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

    对此代码段的改进是,在尝试访问它之前,首先检查是否存在经过身份验证的用户:

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

    当然,像这样的静态调用也有缺点,代码的可测试性降低是更明显的缺点之一。相反,我们将探索满足这一非常常见要求的替代解决方案。

    3. 在控制器中获取用户

    我们在注释 bean 中还有其他选项@Controller

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

    1. @Controller
    2. public class SecurityController {
    3. @RequestMapping(value = "/username", method = RequestMethod.GET)
    4. @ResponseBody
    5. public String currentUserName(Principal principal) {
    6. return principal.getName();
    7. }
    8. }

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

    1. @Controller
    2. public class SecurityController {
    3. @RequestMapping(value = "/username", method = RequestMethod.GET)
    4. @ResponseBody
    5. public String currentUserName(Authentication authentication) {
    6. return authentication.getName();
    7. }
    8. }

    身份验证类的 API 非常开放,因此框架尽可能保持灵活性。因此,Spring 安全性主体只能作为对象检索,并且需要强制转换为正确的UserDetails实例

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

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

    1. @Controller
    2. public class GetUserWithHTTPServletRequestController {
    3. @RequestMapping(value = "/username", method = RequestMethod.GET)
    4. @ResponseBody
    5. public String currentUserNameSimple(HttpServletRequest request) {
    6. Principal principal = request.getUserPrincipal();
    7. return principal.getName();
    8. }
    9. }

    4. 通过自定义接口获取用户

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

    1. public interface IAuthenticationFacade {
    2. Authentication getAuthentication();
    3. }
    4. @Component
    5. public class AuthenticationFacade implements IAuthenticationFacade {
    6. @Override
    7. public Authentication getAuthentication() {
    8. return SecurityContextHolder.getContext().getAuthentication();
    9. }
    10. }

    外观公开身份验证对象,同时隐藏静态状态并保持代码分离且完全可测试:

    1. @Controller
    2. public class GetUserWithCustomInterfaceController {
    3. @Autowired
    4. private IAuthenticationFacade authenticationFacade;
    5. @RequestMapping(value = "/username", method = RequestMethod.GET)
    6. @ResponseBody
    7. public String currentUserNameSimple() {
    8. Authentication authentication = authenticationFacade.getAuthentication();
    9. return authentication.getName();
    10. }
    11. }

    5. 在 JSP 中获取用户

    通过利用 Spring Security Taglib 支持,也可以在JSP 页面中访问当前经过身份验证的主体。

    首先,我们需要在页面中定义标签:

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

    接下来,我们可以参考委托人

    1. authenticated as

    6. 在百里香叶中获取用户

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

    让我们看看如何使用 Thymeleaf 引擎在页面中访问当前经过身份验证的主体。

    首先,我们需要添加thymeleaf-spring5 和 thymeleaf-extras-springsecurity5依赖项,以将 Thymeleaf 与 Spring Security 集成:

    1. org.thymeleaf.extras
    2. thymeleaf-extras-springsecurity5
    3. org.thymeleaf
    4. thymeleaf-spring5

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

    1. xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
    2. Authenticated as
  • 7. 结论

    本文展示了如何在 Spring 应用程序中获取用户信息,从常见的静态访问机制开始,然后是几种更好的注入主体的方法。

    这些示例的实现可以在GitHub 项目中找到。这是一个基于 Eclipse 的项目,因此应该很容易导入和按原样运行。在本地运行项目时,我们可以在此处访问主页 HTML:

    http://localhost:8080/spring-security-rest-custom/foos/1

     

  • 相关阅读:
    Neo4j入门教程2(看不懂评论区随便骂)
    Redis单线程
    黑马JVM总结(九)
    Bilibili视频投稿经验
    css:清除浮动的n种方式
    全光谱台灯对孩子眼睛好吗有辐射吗?普通台灯和LED灯哪个辐射大
    【c++】——类和对象(下) 万字解答疑惑
    服装行业如何做数字化转型?
    4大软件测试策略的特点和区别(单元测试、集成测试、确认测试和系统测试)
    element ui this.$msgbox 自定义组件
  • 原文地址:https://blog.csdn.net/allway2/article/details/127929803