浅谈SpringSecurity请求解析过程
字数 2627 2025-08-06 12:20:45
Spring Security 请求解析过程深度解析
0x00 Spring Security 基础架构
Spring Security 通过过滤器链实现认证/鉴权流程,主要过滤器包括:
- AuthenticationProcessingFilter:处理用户登录操作
- ExceptionTranslationFilter:处理异常并返回对应页面或状态码
- SessionFixationProtectionFilter:防止CSRF攻击
- FilterSecurityInterceptor:主要权限控制过滤器
典型认证流程:
UsernamePasswordAuthenticationFilter拦截表单提交的用户名密码AuthenticationProcessingFilter处理登录认证FilterSecurityInterceptor判断当前URL是否需要认证,已认证则放行,否则重定向到认证页面
0x01 Spring Security 请求解析过程
请求处理流程
-
DelegatingFilterProxy 接收请求
- 委派给
FilterChainProxy处理 - 调用
doFilterInternal方法
- 委派给
-
HttpFirewall 处理
- 在进入过滤器链前对请求对象和响应对象进行处理
- 拦截恶意字符
-
VirtualFilterChain 执行
- 通过
currentPosition依次调用过滤器链中的过滤器
- 通过
-
FilterSecurityInterceptor 权限验证
- 调用
beforeInvocation方法进行权限认证 - 验证
Authentication和目标URL所需权限是否匹配 - 使用
RequestMatcher匹配请求规则
- 调用
0x02 HttpFirewall 接口
Spring Security 提供 HttpFirewall 接口处理非法请求,有两种实现:
2.1 StrictHttpFirewall(严格模式,默认)
主要安全检查方法:
-
rejectForbiddenHttpMethod
- 检查请求方法是否合法(GET/POST/HEAD/OPTIONS/PATCH/PUT/DELETE)
- 不在白名单中的方法会被拦截
-
rejectedBlacklistedUrls
- 检查URL是否规范
- 拦截包含以下字符的请求:
- 分号(;、%3b、%3B)
- 斜杠(%2f、%2F)
- 反斜杠(\、%5c、%5C)
- 百分号(%25)
- 英文句号(.、%2e、%2E)
- 换行符(\n、%0a、%0A)
- 回车符(\r、%0d、%0D)
-
isNormalized
- 检查URL是否规范化
- 拦截包含以下情况的请求:
- 双斜杠(//)
- 相对路径(./、/../)
-
containsOnlyPrintableAsciiCharacters
- 检查请求地址是否只包含ASCII字符
-
rejectedUntrustedHosts(5.3.11.RELEASE+)
- 校验HOST是否在白名单中
2.2 DefaultHttpFirewall
相比严格模式更简单,主要检查:
- URL规范化(同StrictHttpFirewall)
- 请求URI是否包含编码后的斜杠(%2f或%2F)
0x03 RequestMatcher 接口
用于匹配请求是否符合定义的规则,主要实现类:
3.1 AntPathRequestMatcher
基于Ant风格模式匹配,如 /admin/**。
匹配过程:
- 检查请求方法是否一致
- 获取当前请求URL(使用
UrlPathHelper.getPathWithinApplication) - 根据pattern选择匹配器:
SubpathMatcher:以**结尾的patternSpringAntMatcher:其他情况
潜在风险:
-
PathPatternParser解析差异
- Spring Boot 2.6+ 使用
PathPatternParser替代AntPathMatcher - 可能因解析差异导致鉴权绕过(如使用
\r、\n)
- Spring Boot 2.6+ 使用
-
TrailingSlashMatch属性绕过
- 当启用时(默认true),
/admin/index和/admin/index/视为相同 - 可通过添加尾部
/绕过
- 当启用时(默认true),
-
SuffixPatternMatch属性绕过
- 启用时(Spring WebMVC 5.3前默认true),
/admin/index和/admin/index.do视为相同 - 可通过添加后缀绕过
- 启用时(Spring WebMVC 5.3前默认true),
3.2 RegexRequestMatcher
基于正则表达式匹配,如 /admin/.*。
潜在风险:
-
?认证绕过
- 匹配时会拼接?和参数作为URL
- 可通过在路径后添加?绕过
-
CVE-2022-22978
- 漏洞版本(Spring Security 5.5.x < 5.5.7 & 5.6.x < 5.6.4)
- 默认Pattern模式不匹配
\r、\n字符 - 可通过添加
\r或\n绕过 - 修复版本启用DOTALL模式,使
.匹配任何字符
3.3 MvcRequestMatcher
使用Spring MVC的 HandlerMappingIntrospector 匹配路径,比 AntPathMatcher 更严谨:
- 匹配
/index也会匹配/index/、/index.html、/index.do - 避免了
AntPathMatcher的绕过问题
3.4 AnyRequestMatcher
匹配所有请求。
3.5 新版推荐用法(Spring Security 5.8+)
antMatchers、mvcMatchers 和 regexMatchers 已弃用,推荐使用:
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((authz) -> authz
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.requestMatchers("/api/user/**").hasRole("USER")
.anyRequest().authenticated()
);
return http.build();
}
}
在Spring MVC应用中会自动使用 MvcRequestMatcher,更安全。
最佳实践建议
- 使用最新版Spring Security
- 优先使用
requestMatchers替代旧的匹配方法 - 保持
StrictHttpFirewall的默认配置 - 对于关键路径,考虑使用
MvcRequestMatcher - 定期检查安全公告,及时修复已知漏洞