服务器ip有跨域问题吗
㈠ 跨域以及解决跨域的几种方式
跨域是指浏览器允许向服务器发送跨域请求,从而克服Ajax只能 同源 使用的限制。
同源策略 是一种约定,由Netscape公司1995年引入浏览器,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,浏览器很容易受到XSS、CSFR等攻击。所谓同源是指"协议 + 域名 + 端口"三者相同,即便两个不同的域名指向同一个ip地址,也非同源。
常见的跨域场景:
对于简单请求,浏览器会直接发出CORS请求,具体的就是在头信息中,增加一个 Origin 字段。
非简单请求是那种对服务器有特殊要求的请求,譬如 put delete 方法,或者 Content-Type 字段类型是 application/json 的,非简单请求在正式通信前,会增加一次请求,称为预检请求,也就是 options 方法。
浏览器先询问服务器,当前网页所在的域名是否在服务器的许可名单之中,以及可以使用哪些HTTP动词和头信息字段。只有得到肯定答复,浏览器才会发出正式的 XMLHttpRequest 请求,否则就报错。
与简单请求不同的是, option 请求多了2个字段:
Access-Control-Request-Method :必须字段,用来列出浏览器的CORS请求会用到哪些HTTP方法。
Access-Control-Request-Headers :该字段是一个逗号分隔的字符串,指定浏览器CORS请求会额外发送的头信息字段。
服务器收到"预检"请求以后,检查了 Origin 、 Access-Control-Request-Method 和 Access-Control-Request-Headers 字段以后,确认允许跨源请求,就可以做出回应。
表明服务器支持的所有跨域请求的方法。
表明服务器支持的所有头信息字段,不限于浏览器在"预检"中请求的字段。
表示是否允许发送认证信息(Cookie)。
指定本次预检请求的有效期,单位为秒,允许缓存。在缓存期间,不用发出另一条预检请求。
㈡ 怎么解决服务器间的跨域问题
服务端的解决方案的基本原理就是,由客户端将请求发给本域服务器,再由本域服务器的代理来请求数据并将响应返回给客户端。
最常用的服务器解决方案就是利用web服务器本身提供的proxy功能,如apache和lighttpd的mod_proxy模块。在网络内
部,transmit的分流功能也可以解决部分跨域问题。但这些方法都有一定的局限性,鉴于安全性等问题的考虑,space这边最后开发了一个专门用于处
理跨域请求代理服务的spproxy模块,用于彻底解决js跨域问题。
下面我们将以空间的开放平台为例,简单介绍下如何通过apache的mod_proxy、transmit的分流以及space的spproxy模块来解
决该跨域问题,并简单介绍下spproxy的一些特性、缺点及下一步的改进计划。
空间在展现每个UWA开放模块之前都必须请求该模块的xml源代码以进行解析,每个模块的源代码文件都是存放在act域下的/ow/uwa目录下,那么在
用户空间首页(hi域)中请求该xml文件时就会存在js跨域问题。要解决该问题,只能让js向hi域的web服务器请求xml文件,而hi域web服务
器则通过一定的代理机制(如mod_proxy、transmit分流、spproxy)向act域的web服务器请求文件
㈢ 请求接口时跨域问题,前端解决方法
在前后端接口请求中,由于浏览器的限制,会出现跨域的情况。常用的跨域方案有:
1、JSONP跨域
2、Nginx反向代理
3、服务器端修改header
4、document.domain
5、window.name
6、postMessage
7、后台配置运行跨域
当一个请求url的 协议、域名、端口 三者之间任意一个与当前页面url不同即为跨域
特别注意两点:
1、如果是协议和端口造成的跨域问题“前台”是无能为力的,
2、在跨域问题上,域仅仅是通过“URL的首部”来识别而不会去尝试判断相同的ip地址对应着两个域或两个域是否在同一个ip上。
㈣ 怎么解决服务器间的跨域问题
通过设置Http Header方式允许跨域名请求
<?php
header("Access-Control-Allow-Origin: http://www.requesting-page.com");
?>
more details
browser(client) side code examples:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
server side code examples:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Server-Side_Access_Control
怎样配置Apache 服务器允许跨域名请求
How do we fix cross domain scripting issue ?
The simple solution is to allow the server to which request is being
made to server request to any domain or to a list of domains. The
important thing to remember is that the changes are to be made in the server which is serving the web service.
There are multiple ways to do it
1. You change settings in your apache’s httpd-vhosts.conf file ( I am using Apache 2.2 )
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot “C:/apache-tomcat-6.0.29/webapps/myApplication”
ServerName skill-guru.com
ErrorLog “logs/skg1-error.log”
CustomLog “logs/skg1-access.log” common
Header set Access-Control-Allow-Origin “*”
<Directory “C:/apache-tomcat-6.0.29/webapps/myApplication”>
Options -Indexes FollowSymLinks
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
JkUnmount /*.jsp ajp13
</VirtualHost>
Now after you set the value in apache server and look at the header and would see
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 00:23:53 GMT
Server: Apache/2.0.61
Access-Control-Allow-Origin: *
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/xml
怎样配置Tomcat 服务器允许跨域名请求
If you do not plan to use Apache and for some reasons using tomcat or
any other similar web container which supports filter, here is a ready
made solution, Cors
Filter
This gives you a servlet filter which is compatible with any Java Servlet 2.5+ web container.
Installation is very simple. Add the jar to your libraries
In you web.xml
add this line
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>