springboot:全局配置
@Configuration
public class Myconfig implements WebMvcConfigurer {
static final String ORIGINS[] = new String[]{"GET", "POST", "PUT", "DELETE"};
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
// 所有的当前站点的请求地址,都支持跨域访问。
.allowedOrigins("*")
// 所有的外部域都可跨域访问。
.allowCredentials(true)
// 是否支持跨域用户凭证
.allowedMethods(ORIGINS)
// 当前站点支持的跨域请求类型是什么
.maxAge(3600);
// 超时时长设置为1小时。 时间单位是秒。
}
}
2:直接在controller类加上@CrossOrigin(origins = “*”, maxAge = 3600)