SpringSecurity认证流程分析

简介: 我们前面实现了使用自定义认证界面的功能,但是后台认证校验还是使用的’/login’来处理的,对比的账号密码还是我们写在内存的数据,那我们如果想要实现和数据库中的数据比较,那么我们就必须要实现自定义认证逻辑的实现,本文我们就先来分析下系统自带的认证是怎么走的。

我们前面实现了使用自定义认证界面的功能,但是后台认证校验还是使用的’/login’来处理的,对比的账号密码还是我们写在内存的数据,那我们如果想要实现和数据库中的数据比较,那么我们就必须要实现自定义认证逻辑的实现,本文我们就先来分析下系统自带的认证是怎么走的。


一、UsernamePasswordAuthenticationFilter


 系统认证是通过UsernamePasswordAuthenticationFilter 过滤器实现的,所以我们需要来分析下这个过滤器的源码


1.表单提交参数


20191205120128699.png


2.doFilter方法


 因为UsernamePasswordAuthenticationFilter就是一个过滤器,所以我们要分析他的原理,肯定需要通过doFilter方法来开始,注意该方法在父类中。

20191205120754835.png


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

  20191205120922798.png20191205120944605.png



通过上面我们发现,要查看认证的流程我们需要进入attemptAuthentication方法中查看


3.认证的过程

20191205121124316.png


进入this.getAuthenticationManager().authenticate(authRequest);中

20191205121200970.png20191205121206787.png20191205121210864.png20191205121222416.png20191205121229313.png20191205121236491.png20191205121245446.png20191205121252715.png20191205121256732.png2019120512130657.png20191205121308313.png

 我们发现最终去做认证的是 UserDetailsService接口的实现去完成的,那么我们要自定义认证过程,那么也只需要实现该接口接口,下篇我们具体看看如何实现。


相关文章
|
API Android开发 数据安全/隐私保护
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
4005 0
|
8月前
|
SQL 人工智能 API
一个人干五个人的活?Copilot CLI 的 `/fleet` 让你体验“影分身术“
GitHub Copilot CLI 新增 `/fleet` 命令,化身“AI包工头”,智能拆解需求、调度多子代理并行处理跨文件任务(如重构+测+文档),支持依赖编排与实时监工。提升开发效率,助你从执行者进阶为指挥官!
504 0
|
安全 Java 数据安全/隐私保护
Springboot应用开发-SpringBootSecurity
Spring Boot Security 是 Spring 提供的安全框架,集成了身份认证和授权功能,帮助开发者快速构建安全的应用程序。本篇博客将从功能介绍到配置和实践,带您全面了解 Spring Boot Security。
1412 1
Springboot应用开发-SpringBootSecurity
|
存储
如何在Flutter中动态申请权限?
如何在Flutter中动态申请权限?
1264 66
|
搜索推荐 算法
桶排序算法
桶排序是一种高效的排序算法,基于分治思想,理想时间复杂度为O(n)。它通过将数据分到多个桶中,每个桶再单独排序,最后按序合并各桶元素,从而实现整体有序。
828 0
|
安全 Java 数据安全/隐私保护
SpringSecurity 认证流程
通过了解SpringSecurity核心组件后,就可以进一步了解其认证的实现流程了。
632 0
|
XML JavaScript Java
SpringBoot集成Shiro权限+Jwt认证
本文主要描述如何快速基于SpringBoot 2.5.X版本集成Shiro+JWT框架,让大家快速实现无状态登陆和接口权限认证主体框架,具体业务细节未实现,大家按照实际项目补充。
1161 11
|
JavaScript 前端开发 Java
SpringBoot配置文件 —— 超详细全方位教程
本文是一篇关于SpringBoot配置文件的超详细全方位教程,涵盖了配置文件的作用、SpringBoot中的配置文件格式、优先级、properties和yml配置文件的详解及语法、读取配置文件的方法、转义字符和单双引号的使用、配置对象、集合和Map,以及yml的优缺点。
2245 0
SpringBoot配置文件 —— 超详细全方位教程
|
安全 前端开发 Java
Spring Security 6.x 过滤器链SecurityFilterChain是如何工作的
上一篇主要介绍了Spring Secuirty中的过滤器链SecurityFilterChain是如何配置的,那么在配置完成之后,SecurityFilterChain是如何在应用程序中调用各个Filter,从而起到安全防护的作用,本文主要围绕SecurityFilterChain的工作原理做详细的介绍。
1975 0
Spring Security 6.x 过滤器链SecurityFilterChain是如何工作的
|
Java 数据库连接 数据库
【微服务】mybatis typehandler使用详解
自定义 `TypeHandler` 的能力使得 MyBatis 在处理特定的数据类型转换时更加灵活和强大,为在微服务架构中构建与数据库交互逻辑提供了极大的便利。它允许我们灵活处理多样化的数据格式,满足业务不断变化的需求。
1057 2