首頁  >  文章  >  後端開發  >  curl 如何模擬登陸一個OAuth授權使用者中心從而實現登陸其下面的各個子站?

curl 如何模擬登陸一個OAuth授權使用者中心從而實現登陸其下面的各個子站?

WBOY
WBOY原創
2016-12-01 00:56:221392瀏覽

問題概述:
透過php curl模擬登陸一個網站如http://www.aaa.com,透過fiddler抓包分析如下:
1、表單以POST方式提交到http://www.aaa.com /dologin,這裡產生了一個token:xxx,
2、伺服器帶著這個token跳到了以下位址來登陸:
https://account.usercenter.com/login?token=xxx&target_url=http://www .aaa.com
(注意網域不同,而且是https,此外這個攜帶token的url拷貝到任何電腦都能正常登陸,登陸成功後就會失效)
3、登陸成功後地址重定向到target_url: http://www.aaa.com

問題分析:
我的理解:有一台授權伺服器,任何一台PC電腦存取攜帶有效token的url,PC和伺服器之間透過cookie來維持這個token;

提出問題:
使用php curl該如何實現這個登陸模擬?

以下是我的程式碼:

<code><?php
    $cookie_file = 'E:\work\cookie.txt';
    $login_url = 'http://www.aaa.com/dologin';
    $post_fields = 'userName=aa&password=bb&service_key=cc'
    $post_fields.= '&callback_url=http%3A%2F%2Fwww.aaa.com&hostUrl=http%3A%2F%2Fwww.aaa.com';
    $ch = curl_init($login_url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $contents=curl_exec($ch);
    curl_close($ch);
    preg_match('/(https:\/\/account\.usercenter\.com\/tokenLogin[^\s]*)\s*/',$contents,$match);
    //var_dump($match);die; 此处匹配出携带token的url
    $ch = curl_init($match[1]);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 OPR/41.0.2353.46");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    $url='http://www.aaa.com/1.html';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 OPR/41.0.2353.46");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $contents = curl_exec($ch);
    curl_close($ch);
    var_dump($contents);//这里输出的页面显示没有登陆成功(这里是问题所在)
?></code>

不知透過cookie能不能實現這種登陸?各位大俠請指點~~

回覆內容:

問題概述:
透過php curl模擬登陸一個網站如http://www.aaa.com,透過fiddler抓包分析如下:
1、表單以POST方式提交到http://www.aaa.com /dologin,這裡產生了一個token:xxx,
2、伺服器帶著這個token跳到了以下位址來登陸:
https://account.usercenter.com/login?token=xxx&target_url=http://www .aaa.com
(注意網域不同,而且是https,此外這個攜帶token的url拷貝到任何電腦都能正常登陸,登陸成功後就會失效)
3、登陸成功後地址重定向到target_url: http://www.aaa.com

問題分析:
我的理解:有一台授權伺服器,任何一台PC電腦存取攜帶有效token的url,PC和伺服器之間透過cookie來維持這個token;

提出問題:
使用php curl該如何實現這個登陸模擬?

以下是我的程式碼:

<code><?php
    $cookie_file = 'E:\work\cookie.txt';
    $login_url = 'http://www.aaa.com/dologin';
    $post_fields = 'userName=aa&password=bb&service_key=cc'
    $post_fields.= '&callback_url=http%3A%2F%2Fwww.aaa.com&hostUrl=http%3A%2F%2Fwww.aaa.com';
    $ch = curl_init($login_url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $contents=curl_exec($ch);
    curl_close($ch);
    preg_match('/(https:\/\/account\.usercenter\.com\/tokenLogin[^\s]*)\s*/',$contents,$match);
    //var_dump($match);die; 此处匹配出携带token的url
    $ch = curl_init($match[1]);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 OPR/41.0.2353.46");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    $url='http://www.aaa.com/1.html';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 OPR/41.0.2353.46");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $contents = curl_exec($ch);
    curl_close($ch);
    var_dump($contents);//这里输出的页面显示没有登陆成功(这里是问题所在)
?></code>

不知透過cookie能不能實現這種登陸?各位大俠請指點~~

之前的程式碼我是多次調試如何攜帶cookie,不過應該是始終沒有成功關聯上cookie文件,不知道程式碼方面如何寫攜帶cookie的程式碼。不過後來經過更多次調試後,發現http://www.aaa.com/dologin呼叫後,已經可以登陸本站,這個帶有token的url只是一個sso,為了登陸其它子站。

感謝各位的解答~~

當然可以實現啊,你的理解沒錯。 cookie中保存唯一的token,每次提交的時候都要攜帶token,登入成功後,token立即失效。

按流程有 肯定就是對的

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn