SpringMVC filter is used to solve the problem of cross-origin vue requests. springmvcvue

Source: Internet
Author: User

SpringMVC filter is used to solve the problem of cross-origin vue requests. springmvcvue

Previously, I wrote a method to resolve cross-origin requests through annotations. Each time, I need to use annotations in the controll class. This time, I solve this problem through the springmvc Interceptor:

The HandlerInterceptor class that inherits SpringMVC overrides the preHandle method. This method will be called before it reaches controll, as shown below:

Public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {response. setHeader ("Access-Control-Allow-Origin", "*"); response. setHeader ("Access-Control-Allow-Methods", "*"); response. setHeader ("Access-Control-Max-Age", "3600"); response. setHeader ("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); response. setHeader ("Access-Control-Allow-Credentials", "true"); // whether to Allow the browser to carry user identity information (cookie) return true ;}

Springmvc configuration is as follows:

<Mvc: interceptors> <! -- Filter all requests and handle cross-origin requests --> <mvc: interceptor> <mvc: mapping path = "/**"/> <bean class = "com. jzy. interceptor. commonInterceptor "> </bean> </mvc: interceptor> </mvc: interceptors>

This not only solves the cross-origin request restrictions for SSM + VUE frontend and backend separation.

The above issue of using SpringMVC filter to solve vue cross-origin requests is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's home.

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.