我正在修复一些旧的缺陷,作为一个缺陷的一部分,我需要确保一些请求只是POST到JSP页面而不是GET请求.应用程序有一个表单,它将数据提交到另一个JSP页面(我知道它的错误并且反对MVC但为
if (request.getMethod().equals("GET")) {
// reroute the user as it is not a valid req
}
还有其他办法吗?
两种解决方案>添加< security-constraint>空的< auth-constraint>在< url-pattern>上* .jsp和< http-method> GET将阻止对每个人的JSP文件的GET请求(由McDowell建议):
<security-constraint>
<display-name>Restrict GET requests on JSP files</display-name>
<web-resource-collection>
<web-resource-name>JSP files</web-resource-name>
<url-pattern>*.jsp</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
>创建一个侦听< url-pattern>的过滤器* .jsp并且在doFilter()方法中基本执行以下操作.
if (((HttpServletRequest) request).getMethod().equals("GET")) {
((HttpServletResponse) response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
} else {
chain.doFilter(request, response);
}
无需在所有只容易出现IllegalStateException的JSP页面上进行密码操作:响应已经提交错误.
