Tomcat Java Web Disable HTTP method __ Development Issues Summary

Source: Internet
Author: User
Tags auth java web
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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.