我正在使用一个web.xml来尝试禁用我们不使用的HTTP方法,并返回一个不包含任何tomcat信息的主体。
所以我把应用程序的web.xml改成:
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<http-method>PUT</http-method>
<http-method>OPTIONS</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
所以被阻塞的方法返回403和一个空体,对于forbidden。但是TRACE返回一个带有Tomcat HTML页面的405。
我尝试通过ErrorServlet重定向所有错误:
<error-page>
<location>/ErrorServlet</location>
</error-page>
确保内容体为0。但这似乎并不能拦截这些
那么为什么TRACE被不同地对待呢?
谢谢
这对我来说是完全有意义的,因为在所有情况下,除了TRACE,你提交的请求是由URL标识的web资源,代码403意味着对资源的访问被拒绝。尝试使用允许的方法访问相同的资源。也许对他们来说也是禁止的?
另一方面,TRACE不需要访问任何资源,它只是回显客户机的输入,因此405 ("METHOD NOT ALLOWED")看起来适合这种情况。
拥有自定义错误页面是个好主意。每个错误代码的具体示例可以在这里找到:https://serverfault.com/questions/254102/custom-error-pages-on-apache-tomcat