美文网首页
spring boot—Cors 跨域

spring boot—Cors 跨域

作者: 平凡的小蚂蚁 | 来源:发表于2019-02-27 09:56 被阅读0次

cors跨域问题

注: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 跨域

      本文链接:https://www.haomeiwen.com/subject/qbhxuqtx.html