springBoot2.x 支持跨域请求配置
2024-09-06 00:34:41
提供三种配置方式:
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";
}
最新文章
- Tornado 结合memcached缓存页面
- 使用powershell链接到Azure
- iOS---自动布局
- 代码生成AnimatorController
- firefox怎么修改tls协议号
- webvnc利器-noVNC集成实战
- zookeeper作为soa服务器集群的协调调度服务器
- 将string转换成UTF8在进行请求
- Linux 网络相关命令
- PureMVC(JS版)源码解析(三):Observer类
- vue :class的动态绑定
- struct2利用相关的Aware接口
- TreeView 节点拖拽
- Zookeeper分布式集群原理与功能
- 机器学习算法 Python&;R 速查表
- 将php-fpm添加至service服务
- 推荐算法 pd
- springboot @value和@configurationproperties注解的区别
- C++中关键字explicit的作用
- HihoCoder - 1781: Another Bubble Sort (冒泡排序&;逆序对)