About SPRINGMVC cross-domain

Source: Internet
Author: User
With regard to cross-domain problems, the main use of the more is Cros cross-domain. For more information please see Https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS However, when Cross-domain requests are supported under SPRINGMVC+ANGULARJS, A situation in which a complex cross-domain scenario (post + JSON) failed. The starting Cross-domain configuration is as follows:
public class Crossinterceptor extends Handlerinterceptoradapter {

    @Override public
    boolean Prehandle ( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {Response.AddHeader
        (" Access-control-allow-origin "," * ");
        Response.AddHeader ("Access-control-allow-methods", "*");
        Response.AddHeader ("Access-control-max-age", "the");
        Response.AddHeader ("Access-control-allow-headers", "Content-type");
        Response.AddHeader ("Access-control-allow-credentials", "false");
        return Super.prehandle (Request, response, handler);
    }

The Spring-dispatcher-servlet.xml is configured as follows:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path= "/**/*"/>
        <bean class= " Cn.***.filter. Crossinterceptor "/>
    </mvc:interceptor>
</mvc:interceptors>

No problem for simple cross-domain. However, the Post+json request failed, prompting for a cross-domain failure. Tracking the Dooption method in Springmvc source to Frameworkservlet, found that the option Prefetch was accepted, but spring actively returned to allow and did not support Cross-domain configuration. Therefore, add the new configuration as follows:

public class Crossfilter extends Onceperrequestfilter {

    @Override
    protected void dofilterinternal ( HttpServletRequest request, HttpServletResponse response, Filterchain Filterchain) throws Servletexception, IOException {
        if (Request.getheader ("Access-control-request-method")!= null && "Options". Equals ( Request.getmethod ()) {
            //CORS "pre-flight" Request
            Response.AddHeader ("Access-control-allow-origin", "*") ;
            Response.AddHeader ("Access-control-allow-methods", "Get, POST, put, DELETE");
            Response.AddHeader ("Access-control-allow-headers", "Content-type");
            Response.AddHeader ("Access-control-max-age", "1800");//30 min
        }
        filterchain.dofilter (request, response);
}

The Web.xml configuration is as follows:

<filter>
    <filter-name>cors</filter-name>
    <filter-class>cn.***.filter. crossfilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cors </filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

At this point, option requests that the Crossfilter filter be accessed and given a cross-domain response header, as well as a dooption method in the Frameworkservlet. Looking at the browser console, the option request returns support for Cross-domain information and subsequent post requests enter Controller.

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.