过滤器链加载原理

简介: 本文深入解析Spring Security核心过滤器链工作原理,重点分析DelegatingFilterProxy如何代理springSecurityFilterChain,通过FilterChainProxy封装多个安全过滤器,并最终由SecurityFilterChain管理实际的过滤器列表,实现请求的安全控制。

1-DelegatingFilterProxy

我们在web.xml中配置了一个名称为springSecurityFilterChain的过滤器DelegatingFilterProxy,接下我直接对 DelegatingFilterProxy源码里重要代码进行说明,其中删减掉了一些不重要的代码,大家注意我写的注释就行了!

public class DelegatingFilterProxy extends GenericFilterBean {
    @Nullable 
    private String contextAttribute; 
    @Nullable 
    private WebApplicationContext webApplicationContext; 
    @Nullable 
    private String targetBeanName; 
    private boolean targetFilterLifecycle; 
    @Nullable 
    private volatile Filter delegate;//注:这个过滤器才是真正加载的过滤器 
    private final Object delegateMonitor; 
    //注:doFilter才是过滤器的入口,直接从这看! 
    public void doFilter(ServletRequest request, 
                         ServletResponse response, 
                         FilterChain 
                         filterChain) throws ServletException, IOException { 
        Filter delegateToUse = this.delegate; 
        if (delegateToUse == null) { 
            synchronized(this.delegateMonitor) { 
                delegateToUse = this.delegate; 
                if (delegateToUse == null) { 
                    WebApplicationContext wac = this.findWebApplicationContext(); 
                    if (wac == null) { 
                        throw new IllegalStateException("No WebApplicationContext found: no 
                        ContextLoaderListener or DispatcherServlet registered?"); 
                    } 
                    //第一步:doFilter中最重要的一步,初始化上面私有过滤器属性delegate 
                    delegateToUse = this.initDelegate(wac); 
                } 
                this.delegate = delegateToUse; 
            } 
        } 
        //第三步:执行FilterChainProxy过滤器 
        this.invokeDelegate(delegateToUse, request, response, filterChain); 
    } 
    //第二步:直接看最终加载的过滤器到底是谁 
    protected Filter initDelegate(WebApplicationContext wac) throws ServletException { 
        //debug得知targetBeanName为:springSecurityFilterChain 
        String targetBeanName = this.getTargetBeanName(); 
        Assert.state(targetBeanName != null, "No target bean name set"); 
        //debug得知delegate对象为:FilterChainProxy 
        Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class); 
        if (this.isTargetFilterLifecycle()) { 
            delegate.init(this.getFilterConfig()); 
      } 
    return delegate; 
    }
}

第二步debug结果如下:

由此可知,DelegatingFilterProxy通过springSecurityFilterChain这个名称,得到了一个FilterChainProxy过滤器, 最终在第三步执行了这个过滤器。

2-FilterChainProxy

注意代码注释!

public class FilterChainProxy extends GenericFilterBean {
    private static final Log logger = LogFactory.getLog(FilterChainProxy.class);
    private static final String FILTER_APPLIED =
    FilterChainProxy.class.getName().concat(".APPLIED");
    private List<SecurityFilterChain> filterChains;
    private FilterChainProxy.FilterChainValidator filterChainValidator;
    private HttpFirewall firewall;
    //咿!?可以通过一个叫SecurityFilterChain的对象实例化出一个FilterChainProxy对象
    //这FilterChainProxy又是何方神圣?会不会是真正的过滤器链对象呢?先留着这个疑问!
    public FilterChainProxy(SecurityFilterChain chain) {
        this(Arrays.asList(chain));
    }
    //又是SecurityFilterChain这家伙!嫌疑更大了!
    public FilterChainProxy(List<SecurityFilterChain> filterChains) {
        this.filterChainValidator = new FilterChainProxy.NullFilterChainValidator();
        this.firewall = new StrictHttpFirewall();
        this.filterChains = filterChains;
    }
    //注:直接从doFilter看
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
        boolean clearContext = request.getAttribute(FILTER_APPLIED) == null;
        if (clearContext) {
            try {
                request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
                this.doFilterInternal(request, response, chain);
            } finally {
                SecurityContextHolder.clearContext();
                request.removeAttribute(FILTER_APPLIED);
            }
        } else {
            //第一步:具体操作调用下面的doFilterInternal方法了
            this.doFilterInternal(request, response, chain);
        }
    }
    private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain
                                  chain) throws IOException, ServletException {
        FirewalledRequest fwRequest =
        this.firewall.getFirewalledRequest((HttpServletRequest)request);
        HttpServletResponse fwResponse =
        this.firewall.getFirewalledResponse((HttpServletResponse)response);
        //第二步:封装要执行的过滤器链,那么多过滤器就在这里被封装进去了!
        List<Filter> filters = this.getFilters((HttpServletRequest)fwRequest);
        if (filters != null && filters.size() != 0) {
            FilterChainProxy.VirtualFilterChain vfc = new
            FilterChainProxy.VirtualFilterChain(fwRequest, chain, filters);
            //第四步:加载过滤器链
            vfc.doFilter(fwRequest, fwResponse);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(UrlUtils.buildRequestUrl(fwRequest) 
                             + (filters == null ? " has no matching filters" : " has an empty filter list"));
            }
            fwRequest.reset();
            chain.doFilter(fwRequest, fwResponse);
        }
    }
    private List<Filter> getFilters(HttpServletRequest request) {
        Iterator var2 = this.filterChains.iterator();
        //第三步:封装过滤器链到SecurityFilterChain中!
        SecurityFilterChain chain;
        do {
            if (!var2.hasNext()) {
                return null;
            }
                chain = (SecurityFilterChain)var2.next();
    } while(!chain.matches(request));
    return chain.getFilters();
  }
}

第二步debug结果如下图所示,惊不惊喜?十五个过滤器都在这里了!

再看第三步,怀疑这么久!原来这些过滤器还真是都被封装进SecurityFilterChain中了。

3-SecurityFilterChain

最后看SecurityFilterChain,这是个接口,实现类也只有一个,这才是web.xml中配置的过滤器链对象!

//接口
public interface SecurityFilterChain {
    boolean matches(HttpServletRequest var1);
    List<Filter> getFilters();
}
//实现类
public final class DefaultSecurityFilterChain implements SecurityFilterChain {
    
    private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class);
    private final RequestMatcher requestMatcher;
    private final List<Filter> filters;
    
    public DefaultSecurityFilterChain(RequestMatcher requestMatcher,
                                      Filter... filters) {
        this(requestMatcher, Arrays.asList(filters));
    }
    
    public DefaultSecurityFilterChain(RequestMatcher requestMatcher, 
                                      List<Filter> filters) {
        logger.info("Creating filter chain: " + requestMatcher + ", " + filters);
        this.requestMatcher = requestMatcher;
        this.filters = new ArrayList(filters);
    }
    
    public RequestMatcher getRequestMatcher() {
        return this.requestMatcher;
    }
    
    public List<Filter> getFilters() {
        return this.filters;
    }
    
    public boolean matches(HttpServletRequest request) {
        return this.requestMatcher.matches(request);
    }
    
    public String toString() {
        return "[ " + this.requestMatcher + ", " + this.filters + "]";
    }
}

总结:通过此章节,我们对SpringSecurity工作原理有了一定的认识。但理论千万条,功能第一条,探寻底层,是为了更好的使用框架。 那么,言归正传!到底如何使用自己的页面来实现SpringSecurity的认证操作呢?要完成此功能,首先要有一套自己的页面!

相关文章
|
2月前
|
存储 缓存 安全
常用过滤器介绍
本文介绍了Spring Security中的核心过滤器链,涵盖SecurityContextPersistenceFilter、CsrfFilter、LogoutFilter等15个关键过滤器的作用与执行逻辑,揭示其基于AOP思想的安全机制,并说明过滤器的加载具有配置依赖性,非固定不变。
|
设计模式 开发框架 监控
精准解读桥接模式-用桥接模式构建可扩展的软件系统
桥接模式是一种设计模式,旨在将抽象和实现部分分离,使它们可以独立地变化。这种模式的目的是提高系统的灵活性和可扩展性。桥接模式的主要思想是将抽象和实现通过一个桥接类连接起来,从而实现它们的解耦。在这种模式中,抽象部分可以根据需要进行扩展,而实现部分可以自由地变化,而不影响彼此。桥接模式在处理多个独立变化的维度、解耦继承关系、处理平台差异和扩展现有系统等方面具有广泛的应用领域。通过使用桥接模式,可以提高系统的可维护性和可扩展性,使系统更加灵活和适应变化。通过桥接模式,将系统中的抽象部分与实现部分解耦,从而...
902 0
精准解读桥接模式-用桥接模式构建可扩展的软件系统
|
2月前
|
安全 Java 索引
说说 java 中常见的集合类
本文介绍了Java集合框架的核心接口(Collection、List、Set、Map)及其常见实现类。图示展示了类间关系,重点解析ArrayList、LinkedList、HashSet、HashMap等实现原理与性能特点,并指出线程安全及进阶特性,助你掌握集合体系关键知识点。
|
2月前
|
算法 数据安全/隐私保护
比较一下 RSA、ECDSA 和 SM2
RSA、SM2均为非对称加密算法。SM2基于椭圆曲线,密钥长度256位,安全性高且运算更快;RSA依赖大数分解,密钥通常1024-4096位。SM2为国密算法,受国内政策支持。ECDSA与SM2原理相似。
HTTP协议中常见的状态码 ?
HTTP状态码分为1xx、2xx、3xx、4xx、5xx五类。常见状态码包括:101(切换协议,如WebSocket)、200(请求成功)、302(重定向)、401(未认证)、404(资源未找到)、500(服务器内部错误),广泛应用于Web开发中。
|
2月前
|
安全 Java 编译器
我们来说一下 synchronized 与 ReentrantLock 的区别
我是小假 期待与你的下一次相遇 ~
148 4
|
2月前
|
人工智能 自然语言处理 安全
2025-2026智能客服选型推荐:主流方案实测与甄选
2025 年,AI Agent 技术的规模化落地与大模型的行业化适配,推动智能客服行业迈入“全链路价值协同 + 场景化主动服务”的成熟周期。中国电子技术标准化研究院数据显示,零售、金融、政务三大核心领域的智能客服渗透率已突破 65%,部分头部企业的 AI 服务占比更是超过 80%。对于企业而言,智能客服已不再是单纯的“成本优化工具”,更升级为串联售前引流、售中转化、售后复购的业务增长引擎,成为数字化转型的核心基础设施。
|
安全 网络协议 Linux
firewalld服务 具体介绍
firewalld服务 具体介绍
|
Windows
window如何批量创建文件夹
【5月更文挑战第20天】window如何批量创建文件夹
1688 1
|
运维 监控 安全
如何写复盘报告
该内容是关于IT公司中复盘报告的撰写指南,主要包括五个步骤:1) 还原故障基本信息,如定级参考;2) 描述处理过程,按时间顺序列出关键点;3) 评估影响范围,可能涉及业务基线;4) 确定故障原因,从直接原因到根本原因层层分析;5) 分析责任归属和事件级别。复盘还包括故障回顾,提出优化措施以减少重演。内容还提到了一些参考资料,用于深入学习稳定性保障。
1312 0

热门文章

最新文章