Tomcat Java Web Disable HTTP method
Configure the Tomcat,conf/web.xml or application Web.xml
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
This method is suitable for static resources and services of Servelt classes that implement Doget and Dopost methods. Most modern Web applications use the Spring MVC framework, Dispatchservelet's parent class Org.springframework.web.servlet.FrameworkServlet rewrite Javax.servlet.http.HttpServlet's doget, DoPost, DoPut, Dodelete, Dooptions, Dotrace, which corresponds to the standard method of HTTP.
When Dispatchservelet processes each request, it is handled by the Javax.servlet.http.HttpServlet service method, so the standard method of HTTP is processed. A simple configuration Web.xml cannot disable the HTTP method.
Spring MVC Disables the HTTP options methodModify the configuration of spring MVC in the applied web.xml:
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class> s2jh.biz.util.customerdispatcherservlet</servlet-class>
<init-param>
<param-name> Contextconfiglocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true </async-supported>
</servlet>
<servlet-mapping>
<servlet-name> springservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To override the Dispatcherservlet Dooptions method:
/** * Custom Spring MVC dispatcherservlet * Disabled HTTP OPTIONS Method */public class Customerdispatcherservlet Extend
s Dispatcherservlet {private static final Logger Logger = Loggerfactory.getlogger (Customerdispatcherservlet.class);
Private static final long serialversionuid = 8018418118826214565L;
private static final ResourceBundle lstrings = Resourcebundle.getbundle ("javax.servlet.http.LocalStrings");
private static final String method_options = "Options"; @Override protected void Dooptions (HttpServletRequest request, httpservletresponse response) throws Servletexception,
IOException {methodnotallowed (method_options, response);
Logger.warn ("HTTP OPTIONS DISABLED."); /** * DISABLED HTTP method * * @param methodname * @param response * @throws IOException/Private Voi D methodnotallowed (String methodname, HttpServletResponse response) throws IOException {string errmsg = Lstrings.gets Tring ("Http.method_post_not_supportEd ");
object[] Errargs = new Object[1];
Errargs[0] = methodname;
ErrMsg = Messageformat.format (errmsg, Errargs);
Response.senderror (httpservletresponse.sc_method_not_allowed, errmsg);
}
}
To test using a command:
Curl-v-X OPTIONS http:/localhost:8080/test.htm