版权声明:JAVA架构之路群:590583588,每月都有共读书籍! https://blog.csdn.net/zhou9898/article/details/81166242
其实标题叫SpringBoot处理跨域是个伪标题哈。
其实跨域的存在是浏览器问题。当你去访问其他人的资源时候,很可能会出现跨域问题。
下面是网上搜集来的关于什么情况下回出现跨域的问题:
链接 | 结果 | 原因 |
---|---|---|
http:// www.example.com /dir/page2.html |
是 |
同协议同域名同端口 |
http:// www.example.com /dir2/other.html |
是 |
同协议同域名同端口 |
http://user:pwd@ www.example.com /dir2/other.html |
是 |
同协议同域名同端口 |
http://www.example.com: 81 /dir/other.html |
否 | 端口不同 |
https ://www.example.com/dir/other.html |
否 | 协议不同端口不同 |
http:// en.example.com /dir/other.html |
否 | 域名不同 |
http:// example.com /dir/other.html |
否 | 域名不同(要求精确匹配) |
http:// v2.www.example.com /dir/other.html |
否 | 域名不同(要求精确匹配) |
http://www.example.com: 80 /dir/other.html |
不确定 |
取决于浏览器的实现方式 |
其实就是我上面说的,当你访问别人的资源的时候,你的浏览器会出现提示。这时候需要处理跨域。
一般有几种方式:1、通过代理,让你们两个域名一致,这种形式不合理。2、设置response形式。
我这里推荐使用response形式的。
在被访问的资源里面设置response。
比如java里面:
response.setHeader("Access-Control-Allow-Origin", "*");
如果被访问的资源在nginx里面:在location里面设置:
add_header 'Access-Control-Allow-Origin' *;
一行即可。