项目中常用的请求方式有GET和POST,但除了这之外还有TRACE,HEAD,PUT,DELETE,OPTIONS。由于安全目的,通常会禁用除GET和POST之外的请求方式。
经过测试,在tomcat的web.xml配置文件最后加上请求方式限制,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<security-constraint>
<web-resource-collection>
<!-- web-app_2_5.xsd需要加上名称 2.4则不需要-->
<web-resource-name>随意的名称</web-resource-name>
<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>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
还需要修改tomcat的/conf/server.xml中Connector 的allowTrace,默认为false。配置如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" allowTrace="true"/>
测试中<auth-constraint/>写法的有时候没有效果,需要注意。
测试方式:
使用命令:curl -v -X 请求方式 目的地址
查看结果和是否有Allow:POST、GET、DELETE、OPTIONS、PUT、HEAD
如:curl -v -X TRACE http://localhost:8080/login
curl -v -X PUT http://localhost:8080/login