只需写一个配置类就可解决:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author fan
* @projectName
* @description: TODO 支持全局跨域
* @date 2021/9/17 20:15
*/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.maxAge(3600)
.allowCredentials(true);
super.addCorsMappings(registry);
}
}