Spring Boot Nginx axios 跨域设置
来源:互联网 发布:腾讯 绝地求生 知乎 编辑:程序博客网 时间:2024/05/18 15:51
1、Spring Boot跨域配置
在后端使用Spring Boot。Spring Boot跨域非常简单,只需书写以下代码即可。
@Configurationpublic class CustomCORSConfiguration { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); corsConfiguration.setAllowCredentials(true); return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); return new CorsFilter(source); }}
2.Nginx跨域配置
Spring Boot应用用Nginx反向代理。而前端跨域请求的需求不减。
Nginx跨域也比较简单,只需添加以下配置即可。
location / {proxy_pass http://localhost:8080;if ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}if ($request_method = 'POST') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';}if ($request_method = 'GET') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';}}其中:
add_header 'Access-Control-Expose-Headers'
务必加上你请求时所带的header。例如本例中的“Token”,其实是前端传给后端过来的。如果记不得也没有关系,浏览器的调试器会有详细说明。
三、浏览器设置跨域
Chrome、Firefox本身是可以通过配置支持跨域请求的。
Chrome跨域:参考文档:http://www.cnblogs.com/laden666666/p/5544572.html
Firefox跨域:参考文档:https://segmentfault.com/q/1010000002532581/a-1020000002533699
四、axios跨域;
axios.defaults.withCredentials = true;psot 请求加入axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; //证明是ajax 请求
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',}
阅读全文
1 0
- Spring Boot Nginx axios 跨域设置
- Spring boot跨域设置
- 跨域访问支持(Spring Boot、Nginx、浏览器)
- nginx 配合 spring boot
- spring boot 服务器端设置允许跨域访问
- spring boot tomcat 设置
- Spring boot favicon 设置
- Spring boot 设置AOP
- axios的跨域问题
- vue axios跨域问题解决
- axios请求 跨域问题
- vue axios 跨域问题
- nginx跨域设置
- Nginx跨域设置
- nginx 跨域设置
- nginx跨域设置
- spring boot 跨域配置
- spring boot跨域解决办法
- java基础复习----set集合使用
- springMVC工作原理
- qemu.hw.mainkeys 控制是否存在navigationBar
- 操作系统之进程与线程6——死锁处理
- postman+jenkins+newman做接口测试的持续集成
- Spring Boot Nginx axios 跨域设置
- Porfessional JS(21.2-XMLHttpRequest Level 2/Progress Event/CORS)& Sharp jQuery(selectors[part])
- C语言程序Linux下运行
- Android学习笔记——数据存储
- 笨方法学Python 习题 28: 布尔表达式练习
- ubuntu虚拟机中添加新磁盘并挂载(12.04)
- hdu6069 Counting Divisors
- solr5.5(7)——solr常用词了解
- 【割边 && 桥】UVA