2.过滤器链加载原理

简介: 通过分析DelegatingFilterProxy、FilterChainProxy与SecurityFilterChain的源码,揭示了Spring Security中15个过滤器的加载机制:web.xml中配置的DelegatingFilterProxy通过名称获取FilterChainProxy,进而加载封装在SecurityFilterChain中的过滤器链,最终实现安全控制。底层原理明晰后,方可更好自定义认证页面。

通过前面十五个过滤器功能的介绍,对于SpringSecurity简单入门中的疑惑是不是在心中已经有了答案了呀? 但新的问题来了!我们并没有配置这些过滤器啊?它们都是怎么被加载出来的?
1-DelegatingFilterProxy
我们在web.xml中配置了一个名称为springSecurityFilterChain的过滤器DelegatingFilterProxy,接下我直接对 DelegatingFilterProxy源码里重要代码进行说明,其中删减掉了一些不重要的代码,大家注意我写的注释就行了!
第二步debug结果如下:

由此可知,DelegatingFilterProxy通过springSecurityFilterChain这个名称,得到了一个FilterChainProxy过滤器, 最终在第三步执行了这个过滤器。
2-FilterChainProxy
注意代码注释!
第二步debug结果如下图所示,惊不惊喜?十五个过滤器都在这里了!
再看第三步,怀疑这么久!原来这些过滤器还真是都被封装进SecurityFilterChain中了。
3-SecurityFilterChain
最后看SecurityFilterChain,这是个接口,实现类也只有一个,这才是web.xml中配置的过滤器链对象!
Java
运行代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//接口
public interface SecurityFilterChain {
boolean matches(HttpServletRequest var1);
List 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的认证操作呢?要完成此功能,首先要有一套自己的页面!

相关文章
|
安全 Java 数据库连接
Security自定义全局AuthenticationManager
Security自定义全局AuthenticationManager
1054 1
|
消息中间件 数据可视化 Kafka
Mac Kafka可视化工具(kafkatool)
Mac Kafka可视化工具(kafkatool)
2546 0
|
Java 数据库
SpringBoot中如何在过滤器中取get的参数值
SpringBoot中如何在过滤器中取get的参数值
608 0
sentinel-dashboard-1.8.0.jar开机自启动脚本
sentinel-dashboard-1.8.0.jar开机自启动脚本
648 0
|
存储 负载均衡 架构师
Nginx极简入门(七)Nginx的日志管理及配置
前面讲了如何配置Nginx虚拟主机和Nginx负载均衡。我们知道日志对于Nginx非常重要。今天要说的是如何配置Nginx的日志。
Nginx极简入门(七)Nginx的日志管理及配置
|
消息中间件 数据可视化 Kafka
【kafka可视化工具】kafka-eagle在windows环境的下载、安装、启动与访问
【kafka可视化工具】kafka-eagle在windows环境的下载、安装、启动与访问
3161 0
|
12月前
|
存储 SQL JSON
打通可观测性的“任督二脉”:实体与关系的终极融合
阿里云推出图查询能力,基于 graph-match、graph-call、Cypher 三重引擎,实现服务依赖、故障影响、权限链路的秒级可视化与自动化分析,让可观测从‘看板时代’迈向‘图谱时代’。
790 113
|
12月前
|
SpringCloudAlibaba Java Nacos
SpringCloud概述
Spring Cloud是微服务领域的统一解决方案,具备约定大于配置、组件丰富、开箱即用等特点。其版本以地铁站命名,避免与子项目冲突。Spring Cloud Alibaba融合Nacos、Sentinel、Seata等阿里开源组件,提供更完善的微服务生态,成为当前主流技术选型。
 SpringCloud概述
|
数据可视化 Java Nacos
Sleuth+Zipkin 实现 SpringCloud 链路追踪
【8月更文挑战第9天】Sleuth+Zipkin 实现 SpringCloud 链路追踪
1069 1
Sleuth+Zipkin 实现 SpringCloud 链路追踪