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.