FilterChainProxy

本文涉及的产品
RDS AI 助手,专业版
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
简介: `FilterChainProxy` 是 Spring Security 的核心过滤器链代理,通过 `SecurityFilterChain` 管理多个安全过滤器。请求到来时,先经防火墙封装,再匹配适用的过滤器链,最终由虚拟链依次执行。十五个安全过滤器由此协同工作,保障应用安全。

注意代码注释!
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 filterChains;
private FilterChainProxy.FilterChainValidator filterChainValidator;
private HttpFirewall firewall;
//咿!?可以通过一个叫SecurityFilterChain的对象实例化出一个FilterChainProxy对象
//这FilterChainProxy又是何方神圣?会不会是真正的过滤器链对象呢?先留着这个疑问!
public FilterChainProxy(SecurityFilterChain chain) {
this(Arrays.asList(chain));
}
//又是SecurityFilterChain这家伙!嫌疑更大了!
public FilterChainProxy(List 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 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中了。

相关文章
|
安全 Java 数据库连接
Security自定义全局AuthenticationManager
Security自定义全局AuthenticationManager
852 1
|
消息中间件 数据可视化 Kafka
Mac Kafka可视化工具(kafkatool)
Mac Kafka可视化工具(kafkatool)
2207 0
|
JSON JavaScript 前端开发
116.【SpringBoot和Vue结合-图书馆管理系统】(一)
116.【SpringBoot和Vue结合-图书馆管理系统】
390 0
|
消息中间件 数据可视化 Kafka
【kafka可视化工具】kafka-eagle在windows环境的下载、安装、启动与访问
【kafka可视化工具】kafka-eagle在windows环境的下载、安装、启动与访问
2643 0
|
5月前
|
消息中间件 Java 程序员
SpringCloud(2026)
本课程基于传智教育·黑马程序员教学资源,系统讲解Spring Cloud微服务架构实战,涵盖服务注册、远程调用、网关、配置中心等核心应用,并深入RabbitMQ消息队列、ElasticSearch搜索技术及高频面试题解析,结合AI辅助开发与实操训练,助力高效掌握企业级微服务开发与面试要点。
|
12月前
|
SQL Java 数据库连接
菜鸟之路Day34一一Mybatis-基础操作
本文介绍了MyBatis的基础操作,包括删除、插入、修改和查询功能的实现。通过`@Delete`、`@Insert`、`@Update`和`@Select`注解完成对应操作,支持主键自动生成与返回。同时探讨了`#{}`和`${}`的区别,前者用于预编译SQL提升安全性,后者直接拼接但存在SQL注入风险。文章还提供了根据ID查询及条件查询的示例,并介绍了实体类属性与数据库字段不一致时的解决方案,如使用驼峰命名规则或配置映射关系,确保数据封装准确。
334 32
|
安全
WEB安全~X-Frame-Options
`X-Frame-Options` HTTP响应头用于控制网页是否能在框架中被嵌套,防范点击劫持攻击,保护用户安全。常见取值有`DENY`(禁止嵌套)和`SAMEORIGIN`(同源嵌套)。通过设置此头部,网站能提升安全性,防止被恶意嵌入其他站点。注意合理配置并与其他安全头部结合使用。例如,配置为`ALLOW_FROM baidu.com`允许来自百度的嵌套,`SAMEORIGIN`则仅允许同域名嵌套,而`DENY`则拒绝所有。不配置则无保护。
926 2
|
安全 Java 数据安全/隐私保护
SpringSecurity 认证流程
通过了解SpringSecurity核心组件后,就可以进一步了解其认证的实现流程了。
455 0
|
SQL JavaScript 数据库连接
Seata的工作原理
【10月更文挑战第30天】
748 3
|
JavaScript Java 测试技术
基于SpringBoot+Vue的编程语言在线学习平台的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的编程语言在线学习平台的详细设计和实现(源码+lw+部署文档+讲解等)
244 1