当 Ajax 请求从不同域返回时设置 cookie

发布于 2024-12-27 02:44:00 字数 406 浏览 2 评论 0原文

我有一个需要基本身份验证的移动网络应用程序。我设法在不同域的服务器上使用 Ajax 调用基本身份验证。但是,我的回复有问题。

通常,Ajax 响应将通过设置的 cookie 标头在浏览器中设置会话 ID。我注意到认证成功后并没有发生这种情况。

我所做的是尝试通过在返回响应时读取标头来手动设置 cookie。我通过 jQuery 的 jqxhr 对象实现了这一点。令我惊讶的是,即使服务器指示返回了 sessionID,我也无法通过 jqxhr 在响应中看到任何设置的 cookie 标头。

这是跨域请求的预期行为吗?一些响应标头会被丢弃吗?这是浏览器的安全功能吗?如果是这样,您建议如何解决这个问题(例如,返回正文中的会话 ID,我不想这样做,因为我在后台使用 Shiro 安全过滤器来处理这个问题,我不这样做想要破解它)?

请帮忙

I have a mobile web app which requires basic authentication. I managed to invoke a basic authentication with Ajax on a server that is of different domain. However, I have a problem with the response.

Normally, a session id would be set in the browser by the Ajax response through the set cookie header. I noticed that this didn't happen upon successful authentication.

What I did was to try to set the cookie manually by reading the headers when the response is returned. I achieved this via jQuerys jqxhr object. To my surprise even though the server indicated that a sessionID was returned, I was unable to see any set cookie header in the response through jqxhr.

Is this the expected behavior of a cross domain request? That some response headers would get dropped? Is this a security feature of the browser? If so, what would you suggest to work around this (eg. Return the session id in the body, which I don't want to because I'm using Shiro security filter in the backed to take care of this, I don't want to need to hack it)?

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-01-03 02:44:00

不能使用ajax请求跨域uri。您必须实现自己的服务器端代理,它将处理远程身份验证服务器的请求/响应。它可以只复制/粘贴响应,但它必须与您的客户端位于同一域中。

cookie 也是如此,它们有一个可以读取/写入的域。

您将被授权仅请求此代理。它还可以处理cookie创建过程,然后将cookie id返回给客户端。

对于(一个非常简单的)示例,在 www.auth.com 上,您有这个 Web 服务:

url : www.auth.com/auth
@params : string uuid, string password
@returns : int shiroSessionId // yes it's more complicated, but KISS for the example

您应该在 www.mydomain.com/myAuth 上创建一个类,该类使用相同的参数获取 www.auth.com/auth 服务,并且创建本地会话 cookie

url : /myAuth
@params : string uuid, string password
@returns : int cookieId, -1 if login failed

最后在 www.mydomain.com/login.html 中,您将有以下 js 调用(此处为 jquery):

$.ajax(function() {
  url : '/myAuth',
  date : {uuid: inputUuid, password: inputPassword}, // provided by a form
  success(cookieId) {
    if(cookieId > 0) {
      alert('logged with cookie '+getCookie(cookieId));
    } else {
      alert('login failed');
    }
  }
});

希望有帮助。

问候

You can't use ajax to request cross-domain uri. You'll have to implement your own server-side proxy, which will handle request/response with the distant authentification server. It could just copy/paste the response, but it has to be in the same domain as your client.

Same thing for cookies, they have a domain in which they can be read/written.

You'll be authorized to only request this proxy. It can also handle the cookie creation process, and then return the cookie id to the client.

For (a very simple) example, on www.auth.com, you have this webservice :

url : www.auth.com/auth
@params : string uuid, string password
@returns : int shiroSessionId // yes it's more complicated, but KISS for the example

You should create, on www.mydomain.com/myAuth a class which wget the www.auth.com/auth service with the same parameters and create local session cookie

url : /myAuth
@params : string uuid, string password
@returns : int cookieId, -1 if login failed

And finally in www.mydomain.com/login.html, you will have the following js call (here in jquery) :

$.ajax(function() {
  url : '/myAuth',
  date : {uuid: inputUuid, password: inputPassword}, // provided by a form
  success(cookieId) {
    if(cookieId > 0) {
      alert('logged with cookie '+getCookie(cookieId));
    } else {
      alert('login failed');
    }
  }
});

Hope it helps.

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文