Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication 使用jQuery AJAX进行CAS身份验证和重定向_Authentication_Redirect_Jquery - Fatal编程技术网

Authentication 使用jQuery AJAX进行CAS身份验证和重定向

Authentication 使用jQuery AJAX进行CAS身份验证和重定向,authentication,redirect,jquery,Authentication,Redirect,Jquery,我有一个HTML页面,需要使用jQueryAjax函数向CAS protected()web服务发出请求。我有以下代码: $.ajax({ type: "GET", url: request, dataType: "json", complete: function(xmlHttp) { console.log(xmlHttp); alert(xmlHttp.status); }, success: handleRe

我有一个HTML页面,需要使用jQueryAjax函数向CAS protected()web服务发出请求。我有以下代码:

$.ajax({
    type: "GET",
    url: request,
    dataType: "json",
    complete: function(xmlHttp) {
        console.log(xmlHttp);
        alert(xmlHttp.status);
    },
    success: handleRedirects
});
request
变量可以是CAS服务器(
https://cas.mydomain.com/login?service=myServiceURL
)或直接转到服务(然后应重定向回CAS以获取服务票证)。Firebug显示请求正在发出,并以302重定向的形式返回。但是,
$.ajax()
函数没有处理重定向

我编写此函数是为了解决这个问题:

var handleRedirects = function(data, textStatus) {
    console.log(data, textStatus);
    if (data.redirect) {
       console.log("Calling a redirect: " + data.redirect);
       $.get(data.redirect, handleRedirects);
    } else {
        //function that handles the actual data processing
        gotResponse(data);
    }
};
但是,即使这样,
handleRedirects
函数也不会被调用,
xmlHttp.status
始终返回
0
。看起来cookies也不是通过cas.mydomain.com调用发送的。(有关类似问题,请参见。)


这是AJAX调用不处理重定向的问题,还是这里发生的事情比看上去的要多?

确实发生的事情比看上去的要多

经过一些调查,如果不是向同一子域发出jqueryajax请求,那么以这种方式发出的jqueryajax请求似乎会失败。在本例中,请求是从不同的服务器发送到
cas.mydomain.com
。即使它也在
mydomain.com
上,请求也会失败,因为子域不匹配

jqueryajax确实正确地处理重定向。我在同一子域上使用脚本进行了一些测试,以验证这一点。此外,cookies也会像您预期的那样被传递。请参阅本研究


还要记住,协议必须相同。也就是说,由于
cas.mydomain.com
正在使用HTTPS,因此您调用它的页面必须也在HTTPS上,否则请求将失败。

浏览器不允许跨域调用。最简单的方法是在移动应用程序端使用JSONP,并使用CAS网关返回票证。

您可以使用PHP代理进行此类跨域AJAX调用。在下面的示例中,代理能够调用返回JSON字符串的REST web服务

wsproxy.php


你有没有找到解决这个问题的方法?我读了你的博客文章建议JSONP,这是我在其他地方读到的。然而,看起来CORS是解决这一问题的方法,但我仍然无法使302重定向正常工作。您可能会遇到这样一个问题,即代理不会使用用户的会话信息。在我的例子中,我试图通过向CAS服务器发送服务器端请求来将用户登录到CAS服务—我得到的服务票证确实在服务上对他们进行了身份验证,但他们没有登录到服务器本身(失去了从SSO设置开始的一些好处)。
$.ajax({
    type : "POST",
    url : "http://" + document.domain +
        "/wsproxy.php?url=http://wsToCall.com/ws/resource?param1=false&param2=true",
    dataType : "json",
    success : handleRedirects,
    data: { username: "foo", password: "bar" }
});