Spring Security Web : 概念模型接口 HttpFirewall

本文介绍了Spring Security中HTTP防火墙接口,它用于拒绝潜在风险请求或包装请求、响应以控制行为。该接口定义两个方法,在过滤器链调用前执行,有风险则抛异常。框架提供两个实现类,缺省使用DefaultHttpFirewall,但建议用StrictHttpFirewall。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

功能介绍

HttpFirewallSpring Web提供的一个接口,抽象建模HTTP防火墙这一概念。相应对象用于拒绝存在潜在风险的请求或者包装它们以控制它们的行为。该接口的实现类对象会被注入到FilterChainProxy,在安全过滤器链被调用之前该防火墙逻辑会被调用。如果响应对象的行为需要被限制,该防火墙也可以对响应进行包装再返回给请求方。

HttpFirewall只定义了两个接口方法,分别对请求/响应对象进行包装。这两个方法都在FilterChainProxy对请求调用过滤器链之前被调用。如果HttpFirewall认为请求有风险,会抛出异常RequestRejectedException从而拒绝该请求。而HttpFirewall封装后的响应对象对设置响应头部方法,cookie设置方法(对应头部Set-Cookie),重定向方法(对应头部Location设置)做了安全加强,确保相应值中不包含\r或者\n

HttpFirewall包装后的请求/相应对象分别是:FirewalledRequestFirewalledResponse

继承关系

HttpFirewall

使用介绍

Spring Web框架自身对HttpFirewall提供了两个实现类:DefaultHttpFirewallStrictHttpFirewallFilterChainProxy缺省使用的是StrictHttpFirewallDefaultHttpFirewall是个缺省实现,建议使用StrictHttpFirewall而不是DefaultHttpFirewall

源代码

package org.springframework.security.web.firewall;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public interface HttpFirewall {

	/**
	 * Provides the request object which will be passed through the filter chain.
	 *
	 * @throws RequestRejectedException if the request should be rejected immediately
	 */
	FirewalledRequest getFirewalledRequest(HttpServletRequest request)
			throws RequestRejectedException;

	/**
	 * Provides the response which will be passed through the filter chain.
	 *
	 * @param response the original response
	 * @return either the original response or a replacement/wrapper.
	 */
	HttpServletResponse getFirewalledResponse(HttpServletResponse response);
}

参考文章

Spring Security Web : Web安全过滤器链代理对象 FilterChainProxy
Spring Security Web : StrictHttpFirewall HTTP防火墙(严格模式)
Spring Security Web : DefaultHttpFirewall HTTP防火墙(缺省模式)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值