HTTP --- 基础 禁用PUT、DELETE、TRACE等方法

博客围绕HTTP服务展开,重点介绍了禁用PUT、DELETE、TRACE等危险方法。具体说明了在tomcat中可通过在应用程序的web.xml添加代码实现,还提及了jetty禁用相关方法的两种方式,包括基于xml配置和springboot 2.X项目中使用jetty容器的情况。

HTTP — 基础 禁用PUT、DELETE、TRACE等方法

HTTP 服务

在httpd.conf中增加,只允许GET、POST、OPTIONS方法

<Location "/"> AllowMethods GET POST OPTIONS </Location>

重启apache

systemctl restart httpd.service

tomcat 禁用PUT、DELETE、TRACE等危险方法

在应用程序的web.xml中添加如下的代码即可:

<security-constraint>    
	<web-resource-collection>    
		<url-pattern>/*</url-pattern>    
		<http-method>PUT</http-method>    
		<http-method>DELETE</http-method>    
		<http-method>OPTIONS</http-method>    
		<http-method>TRACE</http-method>    
	</web-resource-collection>    
	<auth-constraint>    
	</auth-constraint>    
</security-constraint>    
<login-config>    
	<auth-method>BASIC</auth-method>    
</login-config>

jetty禁用http put和delete等方法的方式

1. 基于xml的配置方式

<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
</auth-constraint>
</security-constraint>

2. springboot项目,容器是jetty,版本 springboot 2.X


```java
@Bean
public JettyServletWebServerFactory createJettyServletWebServerFactory() {
return new JettyServletWebServerFactory(){
@Override
protected void postProcessWebAppContext(WebAppContext webAppContext) {

HttpConstraintElement disable = new HttpConstraintElement(ServletSecurity.EmptyRoleSemantic.DENY);
HttpMethodConstraintElement put = new HttpMethodConstraintElement("PUT", disable);
HttpMethodConstraintElement delete = new HttpMethodConstraintElement("DELETE", disable);
HttpMethodConstraintElement head = new HttpMethodConstraintElement("HEAD", disable);


ServletSecurityElement sse = new ServletSecurityElement(Arrays.asList(put, delete, head));
List<ConstraintMapping> mappings = ConstraintSecurityHandler.createConstraintsWithMappingsForPath("disable", "/*", sse);

ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
csh.setConstraintMappings(mappings);
webAppContext.setSecurityHandler(csh);
}
};
}

*备注:不允许的情况下访问就会报告 http 403 forbidden 错误。*





### 禁用HTTP TRACE和TRACK方法的实现方式 为了提高Web服务器的安全性,禁用HTTP TRACE和TRACK方法是一种常见的安全实践。以下将详细介绍如何在同类型的Web服务器上实现这一目标。 #### 在IIS上禁用TRACE和TRACK方法 对于IIS服务器,可以通过修改网站的`Web.config`文件来禁用TRACE和TRACK方法。具体配置如下: ```xml <configuration> <system.webServer> <security> <requestFiltering> <verbs> <add verb="OPTIONS" allowed="false" /> <add verb="Trace" allowed="false" /> </verbs> </requestFiltering> </security> </system.webServer> </configuration> ``` 通过上述配置,可以确保IIS会处理TRACE和TRACK请求[^1]。 #### 在Spring Boot内置Undertow服务器上禁用TRACE和TRACK方法 如果使用的是Spring Boot内置的Undertow服务器,可以通过自定义配置类来禁用TRACE和TRACK方法。例如,创建一个配置类并覆盖默认的Undertow初始化逻辑: ```java import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class UndertowConfig { @Bean public UndertowServletWebServerFactory undertowServletWebServerFactory() { UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory(); factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo.addInitialHandlerChainWrapper(httpHandler -> (exchange, chain) -> { if ("TRACE".equalsIgnoreCase(exchange.getRequestMethod()) || "TRACK".equalsIgnoreCase(exchange.getRequestMethod())) { exchange.setResponseCode(405); // Method Not Allowed exchange.endExchange(); return; } chain.proceed(); })); return factory; } } ``` 通过上述代码,可以拦截所有TRACE和TRACK请求,并返回405状态码以表示这些方法被允许[^2]。 #### 在Tomcat上禁用TRACE和TRACK方法 对于Tomcat服务器,可以通过修改`web.xml`文件来禁用TRACE和TRACK方法。在`web.xml`中添加以下配置: ```xml <security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint /> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> ``` 此外,还需要确保在`server.xml`中设置`allowTrace="false"`,以防止TRACE请求被允许: ```xml <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" allowTrace="false" redirectPort="8443" /> ``` 通过上述配置,可以有效禁用TRACE和TRACK方法[^3]。 #### 在Apache上禁用TRACE和TRACK方法 对于Apache服务器,可以在`.htaccess`文件或主配置文件中添加以下指令来禁用TRACE和TRACK方法: ```apache TraceEnable off ``` 该指令会直接禁用TRACE方法。对于TRACK方法,通常需要额外配置,因为大多数现代版本的Apache已默认禁用TRACK方法[^3]。 #### 在Spring Security中禁用TRACE和TRACK方法 如果使用Spring Security框架,可以通过配置`HttpSecurity`对象来禁用特定的HTTP方法。例如,在Spring Security配置类中添加以下代码: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.httpMethodRestrict() .and() .authorizeRequests() .antMatchers("TRACE", "TRACK").denyAll(); } } ``` 通过上述配置,可以确保Spring Security会处理任何TRACE或TRACK请求[^5]。 ### 注意事项 - 禁用TRACE和TRACK方法的主要目的是防止潜在的跨站脚本攻击(XSS)[^4]。 - 配置完成后,建议使用工具(如`curl`)测试服务器是否仍然响应TRACE或TRACK请求。 ```bash curl -v -X TRACE http://<ip>:<port> ``` 如果服务器返回405状态码,则说明配置成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值