跨域网站请求解决方案

什么是跨域:跨域其实是浏览器安全机制,请求访问的域名与ajax请求地址不一致,浏览器会无法返回请求结果。通俗点就是:B服务的页面中通过ajax加载了A服务中的信息

 跨域原因产生:在当前域名请求网站中,默认不允许通过ajax请求发送其他域名。

http://b.com/b.jsp

b.jsp
<script type="text/javascript">
$(function() {
$.ajax({
 type:"get",
url : "http://a.com/MyServlet?userId=12344",
cache : false,
dataType : "json",
success : function(data) {
console.log(data);
}
});
})  

</script>

怎么解决?

1.jsonp 只支持get请求,不支持post请求
2.使用接口网关--nginx、springcloud zull  -- 互联网公司实际案例
3.httpclient内部转发
4.添加header请求允许跨域访问

5.使用dubbo分布式服务框架

1.jsonp 只支持get请求,不支持post请求

b.jsp
<script type="text/javascript">
$(function() {
$.ajax({
 type:"get",
async:false,
url : "http://a.com/MyServlet?userId=12344",
dataType:"jsonp",
jsonp:"jsonpCallback", //服务器用于接收callback调用的function名的参数
cache : false,
dataType : "json",
success : function(data) {
console.log(data);
}
});
})  
</script>

A服务后台:
String jsonpCallback = request.getParameter("jsonpCallback");
再将该参数返回:
result=jsonpCallback+"("+result+")";
return result;

jsonp原理剖析:使用script发送get请求,传参数回调带回来进行解析。
<script type="text/javascript" src="http://www.yy.com/static/common/jquery.js?t=2017-07-27"></script>

jsonp永远只是get请求


2.使用接口网关--nginx、springcloud zull  -- 互联网公司实际案例

使用nginx搭建企业api接口网关 :

接口网关:拦截所有的请求,进行分发。作用:权限控制。

使用nginx搭建企业api接口网关实现原理:通过域名相同,项目不同,进行拦截跳转到真实服务器中。

http://www.edu.com/A  --> http://a.com:8080/A
http://www.edu.com/B  --> http://a.com:8080/B

B项目访问A项目:http://www.edu.com/A 使用nginx转发

nginx.conf 配置:
server {
        listen       80;
        server_name  www.edu.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location /A {
            proxy_pass http://a.com:8080/A
            index  index.html index.htm;
        }

location /B {
            proxy_pass http://b.com:8080/B
            index  index.html index.htm;
        }
    }

listen:监听的端口号
server_name:页面显示的服务名
proxy_pass:被转发到真实的服务器的地址
location /A :拦截A项目 转发至A服务中 A:A项目名

B项目b.jsp
<script type="text/javascript">
$(function() {
$.ajax({
 type:"get",
url : "http://www.edu.com/A/MyServlet?userId=12344",
cache : false,
dataType : "json",
success : function(data) {
console.log(data);
}
});
})  
</script>


3.httpclient内部转发

B项目b.jsp 
<script type="text/javascript">
$(function() {
$.ajax({
 type:"get",
url : "http://b.com/BdemoServlet?userId=12344",
cache : false,
dataType : "json",
success : function(data) {
console.log(data);
}
});
})  
</script>

b项目后台:
//创建默认链接
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求
HttpPost httpPost = new HttpPost("http://a.com/MyServlet?userId=12344");
CloseableHttpResponse response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
if(code == 200){
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
}
response.close();
httpClient.close();

缺点:浪费资源(不存在跨域问题) 底层做了两次请求。第一次: b.jsp先访问B项目中请求,第二次: B项目后台再次发送请求访问A项目获取数据
优点:安全,抓包分析不到。

4.添加header请求允许跨域访问

B项目b.jsp 
<script type="text/javascript">
$(function() {
$.ajax({
 type:"get",
url : "http://a.com/MyServlet?userId=12344",
cache : false,
dataType : "json",
success : function(data) {
console.log(data);
}
});
})  
</script>

在A项目的MyServlet代码中加入:

// 允许浏览器跨域访问 允许所有的
response.setHeader("Access-Control-Allow-Origin","*");

5.使用dubbo分布式服务框架.




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值