Springcloud+zuul the options request processing in cross-domain requests

Source: Internet
Author: User

At present the project structure is vue to do front-end, back-end adoption of micro-service architecture, in the development of the front-end need to cross-domain request data, through the Corsconfig configuration to solve the simple cross-domain request. However, when the token information needs to be added to the header of the request, there is a case where the request fails, and the browser and background are prompted for the options type request.

After searching for the data, after the header is set up, the browser sends an options request before sending a formal request, and (according to the data) sends the options request to verify the validity of the formal request, check whether the service side supports the formal request type (POST, GET, etc.), It is unclear how the server-side underlying framework should respond to the request by default. The options request does not contain any user parameters, which causes the user checksum in the filter in Zuul to fail, returning the user prompt, and cannot proceed with the formal request.

There is a response that requires the server to return a status of 200, but the response to the test returning status 200 is not functioning, and the browser still shows that the request failed and does not perform a formal request.

Eventually:

By modifying the filter filter rules, if it is necessary to filter the method body, increase if the request type is options, then no user check, skip the filter directly, the framework itself to respond to the request. The code is as follows:

@Component Public classAuthFilterextendsZuulfilter {.../*** PRE: Route before * Routing: Route Time * Post: After route * ERROR: Send error call * *@return     */@Override PublicString FilterType () {return"Pre"; } .... @Override Public BooleanShouldfilter () {RequestContext CTX=Requestcontext.getcurrentcontext (); HttpServletRequest Request=ctx.getrequest (); if(Request.getmethod (). Equals ("OPTIONS")) {            return false; }        return true; }    ...}

Springcloud+zuul the options request processing in cross-domain requests

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.