如何禁用对JSP页面的GET请求?

Sha*_*mik 7 jsp

我正在修复一些旧的缺陷,作为一个缺陷的一部分,我需要确保一些请求只是POST到JSP页面而不是GET请求.应用程序有一个表单,它将数据提交到另一个JSP页面(我知道它的错误并且反对MVC但为时已晚,无法修复它),因为它是一个JSP页面,所以我们可以POST请求或者我们可以获取请求.如果是恶意用户,可以阅读表单并将请求作为GET从浏览器等发送http://host:80/somejsp.jsp?param=value&param=value.在这种情况下,它将成为违规行为.我需要确保不处理这样的GET请求.一种方法是在jsp页面中执行以下步骤 -

if (request.getMethod().equals("GET")) {
   // reroute the user as it is not a valid req
}
Run Code Online (Sandbox Code Playgroud)

还有其他办法吗?

Bal*_*usC 8

两种解决方案

  1. 添加<security-constraint>一个空<auth-constraint><url-pattern>*.jsp<http-method>GET,这将阻止GET在JSP文件给大家(由麦克道尔建议)要求:

    <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> 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建一个Filter侦听的<url-pattern>方法*.jsp,并在doFilter()方法中基本完成以下操作.

    if (((HttpServletRequest) request).getMethod().equals("GET")) {
        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } else {
        chain.doFilter(request, response);
    }
    
    Run Code Online (Sandbox Code Playgroud)

无需在所有只容易出错的JSP页面上进行密码操作IllegalStateException: response already committed.