.过滤器链加载原理

简介: 通过前文十五个过滤器的解析,我们了解了Spring Security的工作流程。虽未显式配置,但这些过滤器由DelegatingFilterProxy通过名称springSecurityFilterChain自动加载,最终交由FilterChainProxy执行。其核心是DefaultSecurityFilterChain,封装了所有安全过滤器。底层原理明晰后,方可更好实现自定义认证页面。

通过前面十五个过滤器功能的介绍,对于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的认证操作呢?要完成此功能,首先要有一套自己的页面!

相关文章
|
3月前
|
安全 Java 数据安全/隐私保护
认识SpringSecurity
Spring Security 是基于过滤器链的成熟安全框架,提供认证、鉴权及防御 CSRF 等攻击的核心功能,支持多种认证方式与灵活的权限控制模型。
|
3月前
|
IDE Java 关系型数据库
SpringCloud工程部署启动
本文介绍Spring Boot微服务工程搭建的两种方案:完整工程导入或从零创建。包含父工程与子模块(order-service、user-service)构建步骤,详细配置pom依赖及Maven资源管理,确保项目结构清晰、依赖正确加载,助力快速启动开发环境。(238字)
|
3月前
|
Java 关系型数据库 MySQL
OAuth2.0实战案例
基于Spring Boot与Spring Cloud构建OAuth2安全认证系统,包含父工程、资源服务与授权服务模块,集成Spring Security、MyBatis及MySQL,实现统一权限管理与安全访问控制。
|
安全 前端开发 Java
Spring Security是如何工作的?
Spring Security 是一个强大的框架,用于保护 Spring 应用程序,提供全面的安全服务,包括身份验证、授权等功能。本文将介绍其核心概念及默认配置。Spring Security 通过与 Spring MVC、Spring Webflux 或 Spring Boot 集成,创建高度可定制的身份验证和访问控制框架。其核心组件包括 Servlet Filters、Authentication 和 Authorization。通过默认的过滤器链和一系列预定义过滤器,Spring Security 可以轻松实现各种安全功能。
1289 3
|
人工智能 安全 搜索推荐
【claude网页版】中文claude网页版在线使用入口
Claude AI 是由 Anthropic 这家专注于 AI 安全和研究的公司开发的 大型语言模型 (LLM) 🤖。它就像一个超级聪明的语言专家,能够理解和生成人类语言,无论是进行日常对话
|
JSON 前端开发 JavaScript
JavaWeb基础8——Filter,Listener,Ajax,Axios,JSON
Filter过滤器、Listener监听器、AJAX、 同步、异步优点和使用场景、Axios异步框架、JSON、js和JSON转换、案例,Axios + JSON 品牌列表查询和添加
JavaWeb基础8——Filter,Listener,Ajax,Axios,JSON
|
缓存 NoSQL Java
Spring Boot中的高并发处理
Spring Boot中的高并发处理
|
存储 安全 Java
|
Java 开发框架 XML
JDK、JRE、Java SE、Java EE和Java ME有什么区别?
JDK、JRE、Java SE、Java EE和Java ME有什么区别?

热门文章

最新文章