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

作者 : admin 本文共1844个字,预计阅读时间需要5分钟 发布时间: 2024-06-16 共1人阅读

简介

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

SpringSecurity初始化流程

1.加载配置文件web.xml

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

web.xml中配置的信息:

  • Spring的初始化(会加载解析SpringSecurity的配置文件)
  • SpringMVC的前端控制器初始化
  • 加载DelegatingFilterProxy过滤器



    Archetype Created Web Application

    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        CharacterEncodingFilter
        /*
    

    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
    
    
        dispatcherServlet
        
        /
    

    
    
        springSecurityFilterChain
        org.springframework.web.filter.DelegatingFilterProxy
    
    
        springSecurityFilterChain
        /*
    

2.加载SpringSecurity配置文件

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

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

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);
  
本站无任何商业行为
个人在线分享-虚灵IT资料分享 » 【第三篇】SpringSecurity请求流程分析
E-->