首页 > 其他分享 >【第三篇】SpringSecurity请求流程分析

【第三篇】SpringSecurity请求流程分析

时间:2024-06-12 18:33:33浏览次数:10  
标签:web 第三篇 流程 null targetBeanName DelegatingFilterProxy SpringSecurity 加载

简介

本篇文章主要分析一下SpringSecurity在系统启动的时候做了那些事情、第一次请求执行的流程是什么、以及SpringSecurity的认证流程是怎么样的,主要的过滤器有哪些?

SpringSecurity初始化流程

1.加载配置文件web.xml

当Web服务启动的时候,会加载我们配置的web.xml文件

web.xml中配置的信息:

  • Spring的初始化(会加载解析SpringSecurity的配置文件)
  • SpringMVC的前端控制器初始化
  • 加载DelegatingFilterProxy过滤器
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <!-- 初始化spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- post乱码过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 前端控制器 -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!-- 拦截所有请求jsp除外 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 配置过滤器链 springSecurityFilterChain 名称固定 -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

2.加载SpringSecurity配置文件

Spring的初始化操作和SpringSecurity有关系的操作是,会加载SpringSecurity的配置文件,将相关的数据添加到Spring容器中

image.png

3.执行DelegatingFilterProxy过滤器的init方法

DelegatingFilterProxy过滤器:拦截所有的请求。这个过滤器本身和SpringSecurity没有关系,但也是会使用到,其实就是完成从Ioc容器中获取DelegatingFilterProxy这个过滤器配置的FilterName的对象。

系统启动的时候会执行DelegatingFilterProxy的init方法

protected void initFilterBean() throws ServletException {
    synchronized(this.delegateMonitor) {
        // 如果委托对象为null 进入
        if (this.delegate == null) {
            // 如果targetBeanName==null
            if (this.targetBeanName == null) {
                // targetBeanName = 'springSecurityFilterChain'
                this.targetBeanName = this.getFilterName();
            }
// 获取Spring的容器对象
            WebApplicationContext wac = this.findWebApplicationContext();
            if (wac != null) {
                // 初始化代理对象
                this.delegate = this.initDelegate(wac);
            }
        }

    }
}
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
    // springSecurityFilterChain
    String targetBeanName = this.getTargetBeanName();
    Assert.state(targetBeanName != null, "No target bean name set");
    // 从IoC容器中获取 springSecurityFilterChain的类型为Filter的对象
    Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
  

标签:web,第三篇,流程,null,targetBeanName,DelegatingFilterProxy,SpringSecurity,加载
From: https://blog.csdn.net/weixin_43552143/article/details/139600908

相关文章

  • 微信小游戏开发流程及上架步骤:微信小游戏定制开发源码搭建
    微信小游戏开发方案主要包括以下几个方面:    1.游戏设计:根据需求和目标用户群体,设计游戏玩法、关卡、角色、场景等元素,确保游戏具有吸引力和可玩性。    2.技术实现:根据游戏设计,选择合适的技术栈和开发工具,进行游戏代码编写、测试、优化等工作。    ......
  • 【CMake系列】03-cmake 注释、常用指令 message、set、file、for_each、流程控制if
    本文给出了cmake中的一些常用的指令,可以快速了解,为后面的内容深入打点基础。本专栏的详细实践代码全部放在github上,欢迎star!!!如有问题,欢迎留言、或加群【392784757】交流注释#行注释#[[多行注释]]message(""#[[这里也可以注释]]"")message在学习时......
  • JavaScript常用的流程控制语句
    在JavaScript中,有以下几种常用的流程控制语句:if...else:当if条件为假时,可以使用else语句执行另一段代码。if(condition){//条件为真时执行的代码}else{//条件为假时执行的代码}if...elseif...else:可以链式使用多个if和elseif来根据多个条件执行不......
  • 程序猿大战Python——流程控制——其他控制语句
    for循环==目标:==掌握for循环的使用。与while循环功能类似,for语句也能完成反复多次的执行。for语法:for临时变量in序列: 满足条件时,执行的代码1 满足条件时,执行的代码2 ……[else:当for循环正常执行结束后,执行代码]说明:序列指的是能被循环处理......
  • 基于jeecgboot-vue3的Flowable流程--抄送我的功能
    因为这个项目license问题无法开源,更多技术支持与服务请加入我的知识星球。1、抄送我的界面代码如下:<template><divclass="p-2"><!--查询区域--><divclass="jeecg-basic-table-form-container"><a-formref="formRef"@keyup.enter.nati......
  • CH04_程序流程结构
    CH04_程序流程结构程序流程结构C/C++支持最基本的三种程序运行结构:顺序结构:程序按顺序执行,不发生挑战选择结构:依据条件是否满足,有选择的执行相应的功能循环结构:依据条件是否满足,循环多次执行某段代码选择结构if语句作用:执行满足条件的语句单行格式if语句:if(条件){语句......
  • 基本数据类型 String,null 和 undefined,运算符,流程控制,JavaScript之数组,数组常用
    Ⅰ基本数据类型String【一】String类型String类型就是字符串类型【二】定义变量【1】常规变量var变量名="变量值";//一般用这种var变量名='变量值';不支持三引号【2】可以先声明不赋值先用varb;再对变量b赋值varb='6';【三】字符串的格式化输出语法......
  • 储能变流器(PCS)的效率测试流程
    目录引言:    一般来说企业生产了一个电气产品,通常都需要进行效率测试,我想作用有以下几点:第一、方便用户了解产品性能;二、符合国内、美国或者欧盟的标准,才可以进行生产销售;三、是发布会的一个宣传点(可以与其它产品对标)。一、何为电气产品的效率?    一般来说,我......
  • 国际物流的实际操作流程-全球利物流
    国际物流的流程复杂且环节众多,但可以简单总结为以下几个步骤:货物准备:确保货物包装符合国际运输标准,准备相关的出口文件。订舱与运输:根据货物特点选择合适的运输方式(海运、空运、陆运),向承运人订舱。报关:准备报关资料,进行出口清关。跨境运输:货物在运输过程中,可能需要多次转运和......
  • 服务发现全流程解析-APOLLO7.0
    cyber服务发现完全依赖于fastDDS,下面从底层一步一步看下服务发现的整个过程。topology_manager.cc首先从这个类看起,这个类是和dds接壤的,dds发现后,完全由这个类接管,然后整体开始通信。TopologyManager::TopologyManager():init_(false),node_manager_(nullptr),......