DelegatingFilterProxy

简介: 在web.xml中配置的DelegatingFilterProxy,实际是Spring Security的入口过滤器。其核心是通过`springSecurityFilterChain`名称从Spring容器获取FilterChainProxy实例,并在doFilter中初始化并调用它,最终实现安全过滤链的执行。

我们在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过滤器, 最终在第三步执行了这个过滤器。

相关文章
|
缓存 运维 监控
10分钟带你了解 Linux 系统中的 Top 命令
`top`命令是Linux系统中用于实时监控系统资源利用率的工具,展示CPU、内存使用情况及进程状态。启动`top`只需在终端输入`top`。默认按CPU使用率排序,可通过`P`、`M`、`T`键改变排序。使用`k`键可结束进程,`d`键调整刷新率,`q`键退出。输出信息包括系统负载、进程状态、内存使用等。通过进程列表,可以观察到每个进程的CPU和内存占用、用户、运行时间等。了解`top`能帮助测试工程师排查性能问题。
|
4月前
|
人工智能 IDE 开发工具
OpenCode怎么用?OpenCode从下载到上手图文教程(保姆级,巨详细)
OpenCode 是一款免费开源的终端优先 AI 编程助手,支持命令行、桌面端和 IDE 三端运行。模型中立,兼容 75+ 大模型(含 DeepSeek、Ollama 本地模型),代码与数据默认本地存储,隐私安全。
|
7月前
|
Linux 程序员 网络安全
初级程序员必备的十大技能之基础 Linux 命令(一)
教程来源 https://qcycj.cn/ 本文系统讲解程序员必备的Linux核心命令,涵盖文件操作、文本处理、权限管理、进程与网络工具等,结合原理、参数详解及实战案例,助你高效部署、排查与运维——无论用Windows还是macOS,Linux都是程序员不可或缺的“第二操作系统”。
|
测试技术 iOS开发 MacOS
JUnit单元测试报错NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
因为 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit Platform是提供了运行(测试框架)环境的平台 JUnit Jupiter 是新的Junit5(子项目提供了一个基于平台测试运行Jupiter的测试引擎) JUnit Vintage提供了Junit3/4的测试引擎(向前兼容) 所以这里报错的原因应该是:缺少对应依赖()。
1570 0
JUnit单元测试报错NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
|
NoSQL Java Redis
Redis Pipeline介绍 ---- 提高操作Redis数据库的执行效率。
Redis Pipeline是提高Redis执行效率的重要技术,通过批量发送命令,显著减少了网络往返次数,提高了系统的吞吐量和性能。在实际应用中,合理使用Pipeline可以有效优化Redis的性能,特别是在需要批量操作的场景下。本文通过Python和Java的示例代码展示了如何实现和使用Redis Pipeline,为开发者提供了具体的操作指南。
857 16
|
消息中间件 数据库
如何保证消息的可靠性?可以百分百保证MQ的消息可靠性吗?
如何保证消息的可靠性?可以百分百保证MQ的消息可靠性吗?
|
缓存 Java
java把InputStream流写入到文件中
java把InputStream流写入到文件中
784 0
|
Java API 数据安全/隐私保护
在Spring Boot中,过滤器(Filter)是一种非常有用的组件
在Spring Boot中,过滤器(Filter)是一种非常有用的组件
931 6
|
消息中间件 存储 负载均衡
微服务与分布式系统设计看这篇就够了!
【10月更文挑战第12天】 在现代软件架构中,微服务和分布式系统设计已经成为构建可扩展、灵活和可靠应用程序的主流方法。本文将深入探讨微服务架构的核心概念、设计原则和挑战,并提供一些关于如何在分布式系统中实现微服务的实用指导。
793 2