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