@Configuration
public class CorsConfigure extends WebMvcConfigurationSupport {
/*
* 这里主要为了解决跨域问题,所以重写addCorsMappings方法
*/
@Override
protected void addCorsMappings(CorsRegistry registry) {
log.info("跨域配置....");
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.exposedHeaders("access-control-allow-headers",
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-max-age",
"X-Frame-Options")
.allowCredentials(false).maxAge(3600);
super.addCorsMappings(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("----配置静态资源访问-----");
// 告知系统static 当成 静态资源访问
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
其中,addCorsMappings是用于配置跨域请求。