spring-security使用-安全防护HttpFirewall(七)
1.Spring-Security系列导航2.spring-security使用-登录(一)3.spring-security使用-自定义数据源(二)4.spring-security使用-更友好的方式扩展登录AuthenticationProvider(三)5.spring-security使用-获得当前用户信息(四)6.spring-security使用-同一个账号只允许登录一次(五)7.spring-security使用-session共享(六)
8.spring-security使用-安全防护HttpFirewall(七)
9.spring-security使用-权限控制(八)10.spring-security源码-初始化(九)11.spring-security源码-如何初始化SecurityFilterChain到Servlet12.spring-security源码-FilterChainProxy13.spring-security源码-Filter之WebAsyncManagerIntegrationFilter(十)14.Spring-security源码-Filter之SecurityContextPersistenceFilter(十一)15.Spring-security源码-Filter之HeaderWriterFilter(十二)16.Spring-security源码-Filter之LogoutFilter(十三)17.Spring-security源码-Filter之UsernamePasswordAuthenticationFilter(十四)18.Spring-security源码-Filter之ConcurrentSessionFilter(十五)19.Spring-security源码-Filter之SessionManagementFilter(十六)20.Spring-security源码-Filter之RememberMeAuthenticationFilter(十七)21.Spring-security源码-Filter之ExceptionTranslationFilter(十八)22.Spring-security源码-Filter之FilterSecurityInterceptor(十九)23.Spring-security源码-注解权限原理(二十)24.Spring-security源码-注解权限原理之MethodSecurityInterceptor(二十一)25.Spring-Security基于源码扩展-一套系统多套登录逻辑(二十二)26.Spring-Security基于源码扩展-自定义登录(二十三)27.Spring-Security基于源码扩展-自定义认证失败返回(二十四)28.Spring-Security基于源码扩展-自定义授权注解(二十五)类图
默认提供2种实现 defaultfHttpFirewall 看源码可以看出来比较宽松,我们一般使用StrictHttpFirewall
限制请求方法
如果需要修改可以自定义StrictHttpFirewall
public class StrictHttpFirewall implements HttpFirewall { //空的集合 private static final Set<String> ALLOW_ANY_HTTP_METHOD = Collections.unmodifiableSet(Collections.emptySet()); private Set<String> allowedHttpMethods = createDefaultAllowedHttpMethods(); //设置允许的请求方式 private static Set<String> createDefaultAllowedHttpMethods() { Set<String> result = new HashSet(); result.add(HttpMethod.DELETE.name()); result.add(HttpMethod.GET.name()); result.add(HttpMethod.HEAD.name()); result.add(HttpMethod.OPTIONS.name()); result.add(HttpMethod.PATCH.name()); result.add(HttpMethod.POST.name()); result.add(HttpMethod.PUT.name()); return result; } public void setUnsafeAllowAnyHttpMethod(boolean unsafeAllowAnyHttpMethod) { //如果是false则设置空的集合 this.allowedHttpMethods = unsafeAllowAnyHttpMethod ? ALLOW_ANY_HTTP_METHOD : createDefaultAllowedHttpMethods(); } private void rejectForbiddenHttpMethod(HttpServletRequest request) { if (this.allowedHttpMethods != ALLOW_ANY_HTTP_METHOD) { //如果不存在运行的方法里面 则抛出异常 if (!this.allowedHttpMethods.contains(request.getMethod())) { throw new RequestRejectedException("The request was rejected because the HTTP method \"" + request.getMethod() + "\" was not included within the whitelist " + this.allowedHttpMethods); } } } }
使用方式
@Bean HttpFirewall httpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall(); firewall.setUnsafeAllowAnyHttpMethod(true); return firewall; }
请求地址不能有分号
如地址:http://localhost:8080/index;id=ddd
public class StrictHttpFirewall implements HttpFirewall { private Set<String> encodedUrlBlacklist = new HashSet(); private Set<String> decodedUrlBlacklist = new HashSet(); //;的urlecod和decode的几种 private static final List<String> FORBIDDEN_SEMICOLON = Collections.unmodifiableList(Arrays.asList(";", "%3b", "%3B")); public void setAllowSemicolon(boolean allowSemicolon) { //如果是false则删除调 if (allowSemicolon) { this.urlBlacklistsRemoveAll(FORBIDDEN_SEMICOLON); } else { this.urlBlacklistsAddAll(FORBIDDEN_SEMICOLON); } } //加入到校验list private void urlBlacklistsAddAll(Collection<String> values) { this.encodedUrlBlacklist.addAll(values); this.decodedUrlBlacklist.addAll(values); } //清除到校验list private void urlBlacklistsRemoveAll(Collection<String> values) { this.encodedUrlBlacklist.removeAll(values); this.decodedUrlBlacklist.removeAll(values); } private void rejectedBlacklistedUrls(HttpServletRequest request) { Iterator var2 = this.encodedUrlBlacklist.iterator(); String forbidden; do { if (!var2.hasNext()) { var2 = this.decodedUrlBlacklist.iterator(); do { if (!var2.hasNext()) { return; } forbidden = (String)var2.next(); } while(!decodedUrlContains(request, forbidden)); throw new RequestRejectedException("The request was rejected because the URL contained a potentially malicious String \"" + forbidden + "\""); } forbidden = (String)var2.next(); } while(!encodedUrlContains(request, forbidden)); throw new RequestRejectedException("The request was rejected because the URL contained a potentially malicious String \"" + forbidden + "\""); } }
必须是可打印的 ASCII 字符
private static boolean containsOnlyPrintableAsciiCharacters(String uri) { int length = uri.length(); for (int i = 0; i < length; i++) { char c = uri.charAt(i); if (c < '\u0020' || c > '\u007e') { return false; } } return true; }
不能使用双斜杠
@Bean HttpFirewall httpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall(); firewall.setAllowUrlEncodedDoubleSlash(true); return firewall; }
% 不被允许
如果需要去掉
@Bean HttpFirewall httpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall();
//允许%的配置 firewall.setAllowUrlEncodedPercent(true); return firewall; }
反斜杠不被允许
如果需要去掉
@Bean HttpFirewall httpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall(); firewall.setAllowBackSlash(true); firewall.setAllowUrlEncodedSlash(true); return firewall; }
.
不被允许
如果需要去掉
@Bean HttpFirewall httpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall(); firewall.setAllowUrlEncodedPeriod(true); return firewall; }
入口处
public FirewalledRequest getFirewalledRequest(HttpServletRequest request) throws RequestRejectedException { this.rejectForbiddenHttpMethod(request); this.rejectedBlacklistedUrls(request); this.rejectedUntrustedHosts(request); if (!isNormalized(request)) { throw new RequestRejectedException("The request was rejected because the URL was not normalized."); } else { String requestUri = request.getRequestURI(); if (!containsOnlyPrintableAsciiCharacters(requestUri)) { throw new RequestRejectedException("The requestURI was rejected because it can only contain printable ASCII characters."); } else { return new FirewalledRequest(request) { public void reset() { } }; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!