【发布时间】:2012-05-11 02:52:14
【问题描述】:
根据我的阅读,跨域发送 cookie 是不可能的(据我了解,浏览器出于隐私原因阻止了它们)。不过,我希望有人能告诉我解决方法。
我已经在我们的 .Net winForms 客户端中实现了这一点,但是我无法让它在我们的网络软件上运行。
场景:我有我的网站,这需要调用第 3 方系统,该系统使用带有 XML 的 rest 实现,存在于客户防火墙内并且无法从其办公室外部访问(VPN 也不是一个选项) .
我设置了我的 cookie:
$.cookie('name', 'value', {path : '/', domain : '192.168.254.164'});
并提出发帖请求
$.post(call, function(d) {}, 'text')
.success(function(d) {... /*do something*/
但是,cookie 没有发送(请参阅下面的请求和响应标头)
Request Headers Accept text/plain, */*; q=0.01 Accept-Encoding gzip, deflate Accept-Language en-gb,en;q=0.5 Connection keep-alive Host 192.168.254.164:8080 Origin http://localhost:27249 Referer http://localhost:27249/index.html User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0 Response Headers Access-Control-Allow-Orig... http://localhost:27249 Cache-Control no-cache Content-Type application/xml;charset=UTF-8 Date Tue, 01 May 2012 15:14:58 GMT Server Sun Java System Application Server 9.1_01 Set-Cookie JSESSIONID=8f7fbcd40348c0b5d912830bca8a; Path=/App Transfer-Encoding chunked X-Powered-By Servlet/2.5
我收到的回复告诉我我未经授权(这是因为未设置 cookie)。
我不能修改第三方服务器上的任何东西,所以在代理服务器上,我不能使用 JSONP,因为一切都在 XML 中。
为了调试,我尝试在发送之前读取 cookie,但结果为“null”:
alert($.cookie(_svcMnuCookieSessionIdKey));
我是网络开发新手,所以这可能是一个奇怪的问题 - 但正如您从响应标题中看到的那样,我相信我收到了一个 cookie(我在登录时收到了相同的 cookie) - 作为浏览器处理cookies,它不应该保存它并自动将其应用于我的请求吗?而不是我必须像上面那样手动添加它?尽管如此,JSESSIONID 看起来在两个请求中都有不同的值,并且规范说我必须使用我登录时获得的原始 JSESSIONID 作为我的 cookie 的值。
在我的 .Net 应用程序中,我这样做类似于:
Dim httpWebRequest As Net.HttpWebRequest = CType(Net.WebRequest.Create("http://192.168.254.164:8080/App/RestfulService"), Net.HttpWebRequest)
httpWebRequest.UserAgent = My.Application.Info.AssemblyName
httpWebRequest.KeepAlive = True 'set the connection keep-alive (this is the default)
httpWebRequest.Headers.Set(Net.HttpRequestHeader.Pragma, "no-cache") 'we don't want caching to take place so we need to set the pragma header to say we don't want caching
httpWebRequest.Method = "POST"
If Not String.IsNullOrEmpty(_sessionId) Then 'it isn't used on some request, so don't add it if it is nothing
Dim cookie As New System.Net.Cookie("JSESSIONID", _sessionId)
cookie.Domain = New Uri("http://192.168.254.164:8080/App/RestfulService").Host
httpWebRequest.CookieContainer = New Net.CookieContainer
httpWebRequest.CookieContainer.Add(cookie)
End If
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
Dim postData As Byte() = New System.Text.UTF8Encoding(False).GetBytes(RichTextBox1.Text)
httpWebRequest.ContentLength = postData.Length
Using tmpStream As IO.Stream = httpWebRequest.GetRequestStream
tmpStream.Write(postData, 0, postData.Length)
End Using
Dim httpWebResponse As Net.HttpWebResponse = CType(httpWebRequest.GetResponse, Net.HttpWebResponse)
If WebValidation.WebResponseHasContent(httpWebResponse) Then
Dim str As String = String.Empty
'Read the raw HTML from the request
Using sr As New IO.StreamReader(httpWebResponse.GetResponseStream, System.Text.Encoding.UTF8)
str = sr.ReadToEnd
End Using
End If
Return str
那么,这是一个跨域 cookie 请求吗?以及如何在我的 Web 客户端中重新创建此功能?
【问题讨论】:
-
如果任何类型的跨域 cookie 都是可能的,那不是一个巨大的安全漏洞吗?
-
您的网络服务器是否与它尝试调用的第 3 方系统位于同一内部网络?
-
@edr - 不,我们的网络服务器在互联网上,第 3 方服务器位于客户防火墙后面,无法访问。在我的开发场景中,他们都在同一个办公室。
-
当你将它部署到生产环境中并且你的网络服务器在外部世界并且第 3 方应用程序在防火墙内时,它是如何工作的?如果您尝试通过客户端访问第 3 方,那么这些请求将来自不同的 IP,并且很难在防火墙中打开规则以允许访问(而不是将其设为公共访问)。希望你有一个计划。
-
答案在这里:stackoverflow.com/a/10353875。中间的 web 客户端站点并使用 jQuery 将 httprequests 发布到其他服务器,
标签: jquery post cookies cross-domain