跨域请求原理

<p><span style="font-size:18px"><strong>&bull;&nbsp; 简单请求原理</strong></span></p> <p>平安云API网关默认不支持跨域访问CORS功能,如要需要支持跨域访问CORS,请先在API网关管理控制台开启CORS并重新发布API。</p> <p>调用方在调用开启CORS功能的API时,会自动在HTTP请求头中添加Origin字段,表明请求的来源。后端服务器收到请求后,根据请求头中的Origin字段,并结合自身的跨域规则,在应答消息中通过Access-Control-Allow-Origin字段,返回是否同意本次请求的验证结果。</p> <p>&bull;&nbsp;&nbsp; 后端服务器接受任意域的跨域访问,返回结果中Access-Control-Allow-Origin字段为:</p> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20193110100755-113897f19312.png" style="height:360px; width:830px" /></p> <p>&bull;&nbsp;&nbsp; 如果后端服务器仅允许某一指定的域进行跨域访问,例如:https://pinganyun.com&nbsp;,返回结果中的Access-Control-Allow-Origin字段为:</p> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20193110100824-12bd9e799410.png" style="height:359px; width:830px" /></p> <p><span style="font-size:18px"><strong>&bull;&nbsp; 预检请求原理</strong></span></p> <p>平安云API网关默认不支持跨域访问CORS功能,如要需要支持跨域访问CORS,请先在<a href="https://pinganyun.com/console/apiGateway" target="_blank">API网关管理控制台</a>开启CORS并重新发布API。同时,您需要创建一个满足以下要求的预检API:</p> <p>&bull;&nbsp;&nbsp; <strong>API分组</strong>:选择开启CORS功能的API所在分组。</p> <p><!--[if gte mso 9]><xml> <o:OLEObject Type="Embed" ProgID="PBrush" ShapeID="_x0000_i1025" DrawAspect="Content" ObjectID="_1634022221"> </o:OLEObject> </xml><![endif]-->&nbsp; &nbsp;&nbsp;<img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20193110100947-1d74bc749273.png" style="height:26px; margin:0px; width:65px" />:</p> <p>&nbsp; &nbsp; &diams;&nbsp;&nbsp; 调用方在调用预检API时,需要通过QueryString字段传递<code>PA-AG-</code>GroupID(GroupID可在API网关管理控制台查看,可参考查看分组详情),告诉API网关要调用的预检API的分组,例如:</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa"> <p><span style="font-family:Consolas">http://gateway-serverhost:port/path/to/api?PA-AG-GroupId=<em>GroupID</em></span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; &diams;&nbsp;&nbsp; 当API网关接收到合法的预检请求后,不会进行认证和鉴权等操作。</p> <p>&bull;&nbsp;&nbsp; <strong>认证方式</strong>:选择无认证。</p> <p>&bull;&nbsp;&nbsp; <strong>HTTP Method</strong>:接口调用类型选择<code>OPTIONS</code>。</p> <p>&bull;&nbsp;&nbsp; <strong>请求Path</strong>:选择与已开启CORS功能的API相同的PATH,或与已开启CORS功能的API相匹配的请求Path。</p> <p>调用方在调用满足上述条件的预检API时,无需在API请求中添加任何API网关需要的请求头等信息,即可在浏览器等环境中直接通过URI访问其他域的后端服务。</p> <p>预检请求处理过程:</p> <p>1.&nbsp; &nbsp;浏览器增加一次预先验证请求(Preflighted requests),用于询问后端服务器当前页面所在的域是否在可以发送跨域请求,以及可以使用哪些HTTP请求方法和头信息字段。</p> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20193110101232-15c1c407972d.png" style="height:349px; width:830px" /></p> <p>2.&nbsp; &nbsp;预先检查通过后,向后端服务器发送简单请求。&nbsp;</p> <p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20193110101242-17c8d73a922f.png" style="height:351px; width:830px" /></p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题