SoFunction
Updated on 2025-04-05

Using SpringMVC filter to solve the problem of vue cross-domain requests

I have written a method to solve cross-domain requests through annotation methods. I need to use annotations in the controller class every time. This time I use the interceptor of springmvc:

The class HandlerInterceptor inherits SpringMVC overrides the preHandle method. This method will be called before reaching the controller, as follows

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

SpringMVC configuration is as follows:

<mvc:interceptors > 
 <!--Filter all requests,Handle cross-domain request issues--> 
  <mvc:interceptor> 
   <mvc:mapping path="/**"/> 
   <bean class=""></bean> 
  </mvc:interceptor> 
</mvc:interceptors > 

This can solve the limitations of cross-domain requests when the SSM+VUE front and back ends are separated.

The above article using SpringMVC filter to solve the problem of cross-domain requests is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.