spring boot—Cors 跨域

cors跨域问题

spring boot—Cors 跨域_第1张图片

注:1、若工程内没有拦截器,只需要 addCorsMappings 方法即可解决跨域

        2、若有拦截器,需添加 corsFilter 方法


@Override

public void addCorsMappings(CorsRegistry registry){

registry.addMapping("/**")

.allowedOrigins("*")

.allowedHeaders("*")

.allowedMethods("POST", "GET", "DELETE", "PUT");

}

private CorsConfiguration addCorsConfig() {

CorsConfiguration corsConfiguration =new CorsConfiguration();

    List list =new ArrayList<>();

    list.add("*");

    corsConfiguration.setAllowedOrigins(list);

    corsConfiguration.addAllowedOrigin("*");

    corsConfiguration.addAllowedHeader("*");

    corsConfiguration.addAllowedMethod("*");

    return corsConfiguration;

}

@Bean

public CorsFilter corsFilter() {

UrlBasedCorsConfigurationSource source =new UrlBasedCorsConfigurationSource();

    source.registerCorsConfiguration("/**", addCorsConfig());

    return new CorsFilter(source);

}

你可能感兴趣的:(spring boot—Cors 跨域)