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


    简介

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

    SpringSecurity初始化流程

    1.加载配置文件web.xml

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

    web.xml中配置的信息:

    • Spring的初始化(会加载解析SpringSecurity的配置文件)
    • SpringMVC的前端控制器初始化
    • 加载DelegatingFilterProxy过滤器
    1. web-app PUBLIC
    2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
    4. <web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"
    5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    6. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    7. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    8. <display-name>Archetype Created Web Applicationdisplay-name>
    9. <context-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:applicationContext.xmlparam-value>
    12. context-param>
    13. <listener>
    14. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    15. listener>
    16. <filter>
    17. <filter-name>CharacterEncodingFilterfilter-name>
    18. <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    19. <init-param>
    20. <param-name>encodingparam-name>
    21. <param-value>utf-8param-value>
    22. init-param>
    23. filter>
    24. <filter-mapping>
    25. <filter-name>CharacterEncodingFilterfilter-name>
    26. <url-pattern>/*url-pattern>
    27. filter-mapping>
    28. <servlet>
    29. <servlet-name>dispatcherServletservlet-name>
    30. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    31. <init-param>
    32. <param-name>contextConfigLocationparam-name>
    33. <param-value>classpath:spring-mvc.xmlparam-value>
    34. init-param>
    35. <load-on-startup>1load-on-startup>
    36. servlet>
    37. <servlet-mapping>
    38. <servlet-name>dispatcherServletservlet-name>
    39. <url-pattern>/url-pattern>
    40. servlet-mapping>
    41. <filter>
    42. <filter-name>springSecurityFilterChainfilter-name>
    43. <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
    44. filter>
    45. <filter-mapping>
    46. <filter-name>springSecurityFilterChainfilter-name>
    47. <url-pattern>/*url-pattern>
    48. filter-mapping>
    49. web-app>

    2.加载SpringSecurity配置文件

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

    image.png

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

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

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

    1. protected void initFilterBean() throws ServletException {
    2.    synchronized(this.delegateMonitor) {
    3.        // 如果委托对象为null 进入
    4.        if (this.delegate == null) {
    5.            // 如果targetBeanName==null
    6.            if (this.targetBeanName == null) {
    7.                // targetBeanName = 'springSecurityFilterChain'
    8.                this.targetBeanName = this.getFilterName();
    9.           }
    10. // 获取Spring的容器对象
    11.            WebApplicationContext wac = this.findWebApplicationContext();
    12.            if (wac != null) {
    13.                // 初始化代理对象
    14.                this.delegate = this.initDelegate(wac);
    15.           }
    16.       }
    17.   }
    18. }
    1. protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
    2.    // springSecurityFilterChain
    3.    String targetBeanName = this.getTargetBeanName();
    4.    Assert.state(targetBeanName != null, "No target bean name set");
    5.    // 从IoC容器中获取 springSecurityFilterChain的类型为Filter的对象
    6.    Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
    7.  
  • 相关阅读:
    如何写专利
    Flowable工作流之查询历史流程信息
    23种设计模式之代理模式(动态代理)
    有关java连接数据库报错的解决方案
    查找组成一个偶数最接近的两个素数
    SpringBoot 静态资源规则
    vscode文件夹折叠问题
    PyQt5 QLineEdit基础
    【Spring】AOP实现原理
    C语言上网计费系统模拟系统
  • 原文地址:https://blog.csdn.net/weixin_43552143/article/details/139600908