背景
項目中請求方式只有GET,POST請求,處於安全考慮準備禁用TRACE,HEAD,PUT,DELETE,OPTIONS請求方式。
實現
在tomcat的web.xml配置文件最後加上請求方式限制,配置如下,本次使用的tomcat8
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
<http-method>HEAD</http-method>
<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>
這裏主要目的是限制服務器只接受GET,POST請求,不做其他權限限制,本配置即可滿足。
問題
經過測試,除TRACE請求其他請求方式都順利攔截。TRACE請求返回的是405 method not allowed,也就是說TRACE沒有被攔截。通過查資料發現Connector 中有個allowTrace屬性,這裏默認禁用了trace請求,將allowTrace設置爲true就可以限制TRACE請求了,至於爲什麼,我的理解是Connector的優先級高於 security-constraint的配置。
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" allowTrace="true"/>
以上都是個人理解,歡迎拍磚!