文章正文

Spring Boot 允许跨域请求、自定义请求头

【文章】2020-04-23

简介Spring Boot 允许跨域请求、自定义请求头

1:禁止跨域请求 
Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘xxx’ is therefore not allowed access. The response had HTTP status code 403

2:禁止自定义请求头 
Request header field xxx is not allowed by Access-Control-Allow-Headers in preflight response

前后端分离,前端使用Ajax请求一般都会遇着这两配置问题,在Spring Boot中添加如下配置

@Configurationpublic class CorsConf {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", corsConfiguration);        return new CorsFilter(source);
    }
}

打赏支持

感谢您的支持,加油!

打开微信扫码打赏,你说多少就多少

找书费时,联系客服快速获取!

扫码支持

在线客服8:30-22:30,若离线请留言!

获取教程,请联系在线客服!

扫码支持

在线客服8:30-22:30,若离线请留言!

热门阅读

找PDF电子书,太费时间?

  • 微信扫描二维码,让客服快速查找。
  • 在线客服8:30-22:00,若离线请留言!