FilterChainProxy

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
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
736 1
|
消息中间件 数据可视化 Kafka
Mac Kafka可视化工具(kafkatool)
Mac Kafka可视化工具(kafkatool)
1907 0
|
消息中间件 数据可视化 Kafka
【kafka可视化工具】kafka-eagle在windows环境的下载、安装、启动与访问
【kafka可视化工具】kafka-eagle在windows环境的下载、安装、启动与访问
2344 0
|
2月前
|
存储 NoSQL 物联网
MongoDB应用场景
MongoDB适用于社交、游戏、物流、物联网及直播等场景,因其支持海量数据存储、高频读写操作。用户信息、动态、日志等低事务性、高并发数据可高效存取,尤其适合用嵌套结构与地理位置索引优化查询,是大规模非结构化数据存储的理想选择。(238字)
|
2月前
|
安全 小程序 JavaScript
OAuth2.0四种授权模式
OAuth2四种授权模式简介:授权码模式最安全,适用于第三方登录;简化模式无授权码,token直接返回,适合无后端场景;密码模式需用户共享账号信息,仅限高度信任服务;客户端模式为服务间调用,无需用户参与。
何为跨域?
URL由协议、子域名、主域名、端口和资源路径组成,任一部分不同即属不同域。跨域指不同域间相互访问资源,因安全策略常受限制。
|
2月前
|
Java
常见加载顺序
本示例展示了Java中各类代码块的执行顺序:静态代码块随类加载仅执行一次,优先于main方法;局部代码块在方法内按顺序执行;构造代码块每次创建对象前执行,再调用构造器。输出顺序体现其优先级与生命周期。
|
2月前
|
Java
什么是泛型擦除
Java泛型是伪泛型,编译后泛型信息被擦除,如List&lt;Object&gt;和List&lt;String&gt;在JVM中均视为List,类型参数不保留,仅用于编译期检查,运行时无法获取泛型实际类型。
|
2月前
|
JSON JavaScript 前端开发
预检请求
非简单CORS请求会先发送OPTIONS预检请求,检查服务器是否允许跨域。预检包含Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段,确认后才发送正式请求,否则报错。(238字)
|
2月前
|
Java 应用服务中间件 网络安全
Eclipse运行SSM/SSH项目教程
本教程介绍如何在Eclipse中运行Java Web项目。内容包括JDK、Eclipse和Tomcat的安装配置,项目导入(支持Maven与非Maven项目),以及Eclipse中绑定Tomcat服务器并部署项目。完成配置后,可启动项目并通过浏览器访问。