简体   繁体   English

浏览器不遵循来自 AJAX 响应的重定向(PHP 生成的响应使用 CAS 身份验证)

[英]Browser doesn't follow redirect from an AJAX response (PHP-generated response is using CAS authentication)

Ok, It looks like I made a mistake with my initial question.好的,看来我在最初的问题上犯了一个错误。 So, here are some corrections.所以,这里有一些更正。 The answer still applies, because the second redirect is stopped when there is a change in protocol to HTTPS (SSL).答案仍然适用,因为当协议更改为 HTTPS (SSL) 时,第二次重定向将停止。

In my case, I have a redirect occurring multiple times, and the browser doesn't follow the second redirect.就我而言,我的重定向发生了多次,并且浏览器不遵循第二次重定向。 The first redirect is followed but returns an error.遵循第一个重定向但返回错误。

I keep reading that JavaScript AJAX responses containing redirects are followed automatically, but it look like not in my case.我一直在阅读包含重定向的 JavaScript AJAX 响应会自动遵循,但在我看来并非如此。 The first redirect is automatically followed by the browser, and the first redirect is returned without following the second redirect in the header. My problem is that I want all the redirects to be automatically followed by the browser.浏览器自动跟随第一个重定向,返回第一个重定向而不跟随header中的第二个重定向。我的问题是我希望浏览器自动跟随所有重定向。

The redirects are part of the phpCAS library.重定向是 phpCAS 库的一部分。 I have an API written in PHP which checks the user authentication, each time, before returning the results.我有一个 API 写在 PHP 中,每次在返回结果之前检查用户身份验证。

Here is the sequence.这是顺序。 The main thing to note is that the browser returns the second response, after following 1 redirect.需要注意的主要事情是,浏览器在执行 1 次重定向后返回第二个响应。 I would prefer it went all the way and returned the last response when I make an AJAX call to localhost/example/api .当我对localhost/example/api进行 AJAX 调用时,我更希望它一直运行并返回最后一个响应。

localhost/example本地主机/示例

  • Calls localhost/example/api using jQuery.ajax()使用 jQuery.ajax() 调用localhost/example/api

Response 1: localhost/example/api响应 1:localhost/example/api

  • Redirects to https://localhost/accounts/cas/login?service=api.example.com&gateway=true (using SSL).重定向到https://localhost/accounts/cas/login?service=api.example.com&gateway=true (使用 SSL)。

Response 2: (SSL) localhost/accounts/cas/login?service=api.example.com&gateway=true响应 2:(SSL)localhost/accounts/cas/login?service=api.example.com&gateway=true

  • When the query key 'gateway' is present, the login simply redirects back to the URL provided by the 'service' key with or without a ticket (to signal to service that the user is either logged in or not).当存在查询键“网关”时,登录会简单地重定向回由“服务”键提供的 URL,有或没有票(向服务发出用户登录或未登录的信号)。

Response 3: localhost/api?ticket=TICKET响应 3:localhost/api?ticket=TICKET

  • Verifies the ticket and redirects back to itself without the ticket.验证票证并在没有票证的情况下重定向回自身。

Response 4: localhost/api响应 4:localhost/api

  • This time the CAS client looks at the $_SESSION to remember what the ticket was, and then processes the API request returning JSONP.这次 CAS 客户端查看 $_SESSION 以记住票证是什么,然后处理返回 JSONP 的 API 请求。

There's no particular reason I'm using CAS over OpenID or OpenAuth(orization).我在 OpenID 或 OpenAuth(orization) 上使用 CAS 没有特别的原因。 CAS was just the first authentication module I was able to get working in WordPress. I am open to suggestions in terms of using a different authentication library, CMS, framework, etc. Although, my hope is to just get this project finished. CAS 只是我能够在 WordPress 工作的第一个身份验证模块。我愿意接受有关使用不同的身份验证库、CMS、框架等方面的建议。尽管如此,我希望只完成这个项目。 So the less re-tooling the better.因此,重新加工越少越好。

As you later found yourself as you added in your comments, ajax requests are subject to same origin policy .正如您后来在评论中添加的那样发现自己,ajax requests are subject to same origin policy

Yes, you could use JSONP - however, if you are fortunate enough to have to support only IE8 and upwards, CORS might be a better solution.是的,您可以使用 JSONP - 但是,如果您有幸只支持 IE8 及更高版本, CORS可能是更好的解决方案。

Basically, adding headers such as基本上,添加标题,例如

access-control-allow-origin: http://api.example.com
access-control-allow-credentials: true

to your server answer, you could work around cross origin policy.对于您的服务器答案,您可以解决跨源策略。

Also see this jQuery ticket to make it kinda work with jQuery.另请参阅此jQuery 票证,使其与 jQuery 兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM