User-Profile-Image
hankin
  • 5
  • Java
  • Kotlin
  • Spring
  • Web
  • SQL
  • MegaData
  • More
  • Experience
  • Enamiĝu al vi
  • 分类
    • Zuul
    • Zookeeper
    • XML
    • WebSocket
    • Web Notes
    • Web
    • Vue
    • Thymeleaf
    • SQL Server
    • SQL Notes
    • SQL
    • SpringSecurity
    • SpringMVC
    • SpringJPA
    • SpringCloud
    • SpringBoot
    • Spring Notes
    • Spring
    • Servlet
    • Ribbon
    • Redis
    • RabbitMQ
    • Python
    • PostgreSQL
    • OAuth2
    • NOSQL
    • Netty
    • MySQL
    • MyBatis
    • More
    • MinIO
    • MegaData
    • Maven
    • LoadBalancer
    • Kotlin Notes
    • Kotlin
    • Kafka
    • jQuery
    • JavaScript
    • Java Notes
    • Java
    • Hystrix
    • Git
    • Gateway
    • Freemarker
    • Feign
    • Eureka
    • ElasticSearch
    • Docker
    • Consul
    • Ajax
    • ActiveMQ
  • 页面
    • 归档
    • 摘要
    • 杂图
    • 问题随笔
  • 友链
    • Spring Cloud Alibaba
    • Spring Cloud Alibaba - 指南
    • Spring Cloud
    • Nacos
    • Docker
    • ElasticSearch
    • Kotlin中文版
    • Kotlin易百
    • KotlinWeb3
    • KotlinNhooo
    • 前端开源搜索
    • Ktorm ORM
    • Ktorm-KSP
    • Ebean ORM
    • Maven
    • 江南一点雨
    • 江南国际站
    • 设计模式
    • 熊猫大佬
    • java学习
    • kotlin函数查询
    • Istio 服务网格
    • istio
    • Ktor 异步 Web 框架
    • PostGis
    • kuangstudy
    • 源码地图
    • it教程吧
    • Arthas-JVM调优
    • Electron
    • bugstack虫洞栈
    • github大佬宝典
    • Sa-Token
    • 前端技术胖
    • bennyhuo-Kt大佬
    • Rickiyang博客
    • 李大辉大佬博客
    • KOIN
    • SQLDelight
    • Exposed-Kt-ORM
    • Javalin—Web 框架
    • http4k—HTTP包
    • 爱威尔大佬
    • 小土豆
    • 小胖哥安全框架
    • 负雪明烛刷题
    • Kotlin-FP-Arrow
    • Lua参考手册
    • 美团文章
    • Java 全栈知识体系
    • 尼恩架构师学习
    • 现代 JavaScript 教程
    • GO相关文档
    • Go学习导航
    • GoCN社区
    • GO极客兔兔-案例
    • 讯飞星火GPT
    • Hollis博客
    • PostgreSQL德哥
    • 优质博客推荐
    • 半兽人大佬
    • 系列教程
    • PostgreSQL文章
    • 云原生资料库
    • 并发博客大佬
Help?

Please contact us on our email for need any support

Support
    首页   ›   Spring   ›   SpringSecurity   ›   正文
SpringSecurity

SpringSecurity—UsernamePasswordAuthenticationFilter 源码分析

2020-04-14 02:12:13
735  0 0
参考目录 隐藏
1) 继承关系
2) 流程分析

阅读完需:约 2 分钟

UsernamePasswordAuthenticationFilter 是 AbstractAuthenticationProcessingFilter 的子类,主要作用是对用户身份信息的验证。

关于 AbstractAuthenticationProcessingFilter 的分析见此:

SpringSecurity—AbstractAuthenticationProcessingFilter 源码解析

继承关系

UsernamePasswordAuthenticationFilter 继承自AbstractAuthenticationProcessingFilter。

public class UsernamePasswordAuthenticationFilter extends
        AbstractAuthenticationProcessingFilter {

AbstractAuthenticationProcessingFilter 是处理 form 登陆的过滤器,与 form 登陆有关的所有操作都是在该类中及其子类中进行的。

流程分析

关于 UsernamePasswordAuthenticationFilter 处理请求的大体流程和其父类一致,见此: AbstractAuthenticationProcessingFilter 源码分析。该处主要分析UsernamePasswordAuthenticationFilter 实现的父类方法 attemptAuthentication() 中的用户身份验证逻辑。

attemptAuthentication() 方法代码如下所示:

public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

-->1    String username = obtainUsername(request);
-->2    String password = obtainPassword(request);

        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }

        username = username.trim();

-->3    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                username, password);

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

-->4    return this.getAuthenticationManager().authenticate(authRequest);
    }
  • -->1 和 -->2 处的代码从 request 中提取用户名和密码。
  • -->3 处代码为构建类UsernamePasswordAuthenticationToken 成员变量,UsernamePasswordAuthenticationToken 是一个用户信息的载体类,用来存储及传递用户名和用户密码。
  • -->4 处代码调用getAuthenticationManager()方法获取AuthenticationManager实例,getAuthenticationManager()方法定义如下:
protected AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }

然后调用获取到的AuthenticationManager实例的authenticate()方法对封装在authRequest [UsernamePasswordAuthenticationToken]中的用户名及密码进行验证。

注意: AuthenticationManager这里可以理解为 spring security 配置文件中 <authentication-manager/> 的实现类。

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
Enamiĝu al vi
不要为明天忧虑.因为明天自有明天的忧虑.一天的难处一天当就够了。
543文章 68评论 294点赞 593825浏览

随机文章
实用网站(待)
5年前
Java8—新特性之CompletableFuture(构建异步应用)
5年前
Spring笔记5—Java 配置
5年前
SpringSecurity—核心组件UserDetailsService
5年前
SpringSecurity—防御 CSRF 攻击
4年前
博客统计
  • 日志总数:543 篇
  • 评论数目:68 条
  • 建站日期:2020-03-06
  • 运行天数:1927 天
  • 标签总数:23 个
  • 最后更新:2024-12-20
Copyright © 2025 网站备案号: 浙ICP备20017730号 身体没有灵魂是死的,信心没有行为也是死的。
主页
页面
  • 归档
  • 摘要
  • 杂图
  • 问题随笔
博主
Enamiĝu al vi
Enamiĝu al vi 管理员
To be, or not to be
543 文章 68 评论 593825 浏览
测试
测试
看板娘
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付