热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

深入解析SpringSecurity用户认证机制

本文将详细介绍SpringSecurity中用户登录认证的核心流程,重点分析AbstractAuthenticationProcessingFilter和AuthenticationManager的工作原理。通过理解这些组件的实现,读者可以更好地掌握SpringSecurity的认证机制。

1. 引言

在上一篇文章中,我们探讨了 UsernamePasswordAuthenticationFilter 的工作流程,并留下了一个悬念:作为一个 Servlet Filter,它并没有直接实现 doFilter 方法,而是由其父类 AbstractAuthenticationProcessingFilter 提供了具体实现。本文将继续深入探讨这一实现,引出 AuthenticationManager 并详细讲解用户的认证过程。

2. AbstractAuthenticationProcessingFilter 深度剖析

AbstractAuthenticationProcessingFilter 是 UsernamePasswordAuthenticationFilter 的父类,负责处理认证过滤器的逻辑。我们来看一下它的核心方法 doFilter 的实现:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse respOnse= (HttpServletResponse) res; if (!requiresAuthentication(request, response)) { chain.doFilter(request, response); return; } if (logger.isDebugEnabled()) { logger.debug("Request is to process authentication"); } try { Authentication authResult = attemptAuthentication(request, response); if (authResult == null) { return; } sessionStrategy.onAuthentication(authResult, request, response); } catch (InternalAuthenticationServiceException failed) { logger.error("An internal error occurred while trying to authenticate the user.", failed); unsuccessfulAuthentication(request, response, failed); return; } catch (AuthenticationException failed) { unsuccessfulAuthentication(request, response, failed); return; } if (continueChainBeforeSuccessfulAuthentication) { chain.doFilter(request, response); } successfulAuthentication(request, response, chain, authResult); }

doFilter 方法的主要逻辑是判断请求是否需要认证,并调用子类的 attemptAuthentication 方法获取认证结果。如果认证成功,则继续执行后续过滤器链;否则,触发认证失败处理器。

3. AuthenticationManager 详解

AuthenticationManager 接口用于对未授信凭据进行认证。该接口的输入和输出类型都是 Authentication。认证通过则返回授信状态的凭据,否则抛出 AuthenticationException。

3.1 AuthenticationManager 初始化流程

AuthenticationManager 在 WebSecurityConfigurerAdapter 的 configure 方法中进行配置。以下是初始化流程的总结:


AuthenticationManager 的初始化流程

需要注意的是,自定义配置时应避免类似以下错误示范:

@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { DaoAuthenticationProvider daoAuthenticatiOnProvider= new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(weChatSecurityConfigProperties.getUserDetailsService()); daoAuthenticationProvider.setPasswordEncoder(multiPasswordEncoder()); auth.authenticationProvider(daoAuthenticationProvider); // 不要调用 super.configure(auth); }

3.2 AuthenticationManager 认证过程

AuthenticationManager 的实现类 ProviderManager 管理多个 AuthenticationProvider。每个 AuthenticationProvider 只支持特定类型的 Authentication,如果不支持则跳过。只要有一个认证成功,就认为整体认证成功,所有都失败则认为认证失败。


ProviderManager 认证 Token 的流程

从这里可以看出,AuthenticationManager 针对不同类型的 Authentication 提供了灵活的认证功能,可以实现多种认证方式并存。

4. 总结

本文详细分析了 Spring Security 中 AuthenticationManager 的初始化和认证过程。熟悉这些内容有助于开发者实现复杂的认证逻辑,为项目集成 Spring Security 提供坚实的基础。欢迎关注「码农小胖哥」获取更多原创干货。

往期推荐:

你没见过Java台式计算机和Java操作系统吧

Spring Security 实战干货:UsernamePasswordAuthenticationFilter 源码分析

为什么我推荐Nginx作为后端服务器代理


推荐阅读
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 尽管某些细分市场如WAN优化表现不佳,但全球运营商路由器和交换机市场持续增长。根据最新研究,该市场预计在2023年达到202亿美元的规模。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文详细介绍了 com.facebook.drawee.view.SimpleDraweeView 中的 setScaleType 方法,提供了多个实际代码示例,并解释了其在不同场景下的应用。 ... [详细]
  • 本文探讨了MariaDB在当前数据库市场中的地位和挑战,分析其可能面临的困境,并提出了对未来发展的几点看法。 ... [详细]
  • 本文探讨了 Spring Boot 应用程序在不同配置下支持的最大并发连接数,重点分析了内置服务器(如 Tomcat、Jetty 和 Undertow)的默认设置及其对性能的影响。 ... [详细]
author-avatar
好的吧
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有