提供三种配置方式:

1.配置过滤器,实现 WebMvcConfigurer接口(springboot2.x的方式)

@Configuration
public class GlobalCorsConfig implements WebMvcConfigurer {
//添加到容器中管理
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.setAllowCredentials(true);
config.addAllowedMethod("*");
config.addAllowedHeader("*");
config.addExposedHeader("*"); UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config); return new CorsFilter(configSource);
}
}

2.配置拦截器,继承WebMvcConfigurerAdapter类(1.x的方式,在2.x已经过时)

@Configuration
public class MyConfiguration extends WebMvcConfigurerAdapter { @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("*"); }
}

以上两种是针对全局的配置方式 

3. 单个请求的跨域通过 @CrossOrigin 注解来实现

@RequestMapping("/hello")
@CrossOrigin("http://127.0.0.1:8080")
public String hello( ){
  return "Hello World springboot";
}

  

最新文章

  1. Tornado 结合memcached缓存页面
  2. 使用powershell链接到Azure
  3. iOS---自动布局
  4. 代码生成AnimatorController
  5. firefox怎么修改tls协议号
  6. webvnc利器-noVNC集成实战
  7. zookeeper作为soa服务器集群的协调调度服务器
  8. 将string转换成UTF8在进行请求
  9. Linux 网络相关命令
  10. PureMVC(JS版)源码解析(三):Observer类
  11. vue :class的动态绑定
  12. struct2利用相关的Aware接口
  13. TreeView 节点拖拽
  14. Zookeeper分布式集群原理与功能
  15. 机器学习算法 Python&R 速查表
  16. 将php-fpm添加至service服务
  17. 推荐算法 pd
  18. springboot @value和@configurationproperties注解的区别
  19. C++中关键字explicit的作用
  20. HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)

热门文章

  1. WordPress窗体化侧边栏
  2. js-xlsx
  3. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_4-1.单机和分布式应用的登录检验讲解
  4. mybatisplus
  5. HashMap 怎么 hash?又如何 map?
  6. linux下后台启动springboot项目(转载)
  7. Pearson Correlation Score
  8. 在java中有关于反射的皮毛----自己的简略认知
  9. Python multiprocess模块(上)
  10. 转:微服务框架之微软Service Fabric