cas之Python 请求和 CAS

emanlee 阅读:34 2024-10-01 17:34:08 评论:0

我还没有找到任何人使用 Python 来通过 CAS 的例子。希望 Kenneth Reitz 可以向我展示“请求”如何让这一切变得简单......

基本上,我无法通过 CAS 登录...从不验证我的 Python 尝试。 (注意,我定义了两个 url……url1 是主网页,url2 是到 CAS 站点的重定向链接……我已经知道重定向链接,所以很容易)。

我的理解是我所要做的就是捕获 CAS 作为 cookie 发送给我的 JsessionId,然后获取该 cookie 并将 jsessionid 附加回 url 并将其作为 POST 发送回 CAS用户名密码)。然而,这个脚本每次都会失败。

一些 CAS 专家可以帮助我吗?我根本无法弄清楚为什么它不会对我进行身份验证。

import sys 
import requests 
 
my_config = {'verbose': sys.stderr } 
 
url1 = 'http://agnes:8080' 
url2 = 'https://agnes:8543/irisCAS/login?service=http%3A%2F%2Fagnes%3A8080%2FirisRootWeb%2Fj_spring_cas_security_check' 
 
response = requests.get(url1, headers=headers, verify=False) 
print response.cookies 
 
cookies = response.cookies 
response = requests.post(url2, headers=headers, verify=False, config=my_config, params=cookies, auth=('username', 'password')) 
 
print response.status_code 
print response.content 

输出 ....注意 jsessionId 是如何附加到 url2 的,所以这很好......我认为。

{'JSESSIONID': 'EEE38382A1D5AAACA58E12433BDA0BFF'} 
 
2012-05-18T15:04:17.668601   POST   https://agnes:8543/irisCAS/login?service=http%3A%2F%2Fagnes%3A8080%2FirisRootWeb%2Fj_spring_cas_security_check&JSESSIONID=EEE38382A1D5AAACA58E12433BDA0BFF 
 
200 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> 
<head> 
... 
... 
        </script> 
 
        <form id="fm1" class="fm-v clearfix" action="/irisCAS/login;jsessionid=30ABCAC79FEA5B48399053939530A608?service=http%3A%2F%2Fagnes%3A8080%2FirisRootWeb%2Fj_spring_cas_security_check&amp;JSESSIONID=B6235434D64C5E2E6C063BA3E1C1AC43" method="post"> 
 
            <div class="box fl-panel" id="login"> 
            <!-- Congratulations on bringing CAS online!  The default authentication handler authenticates where usernames equal passwords: go ahead, try it out.  --> 
                <h2>Enter your UserId and Password</h2> 
 
(this is just the xml of the CAS login page that I can't get past) 
 
... 
... 

请您参考如下方法:

好的,我想通了,所以我会为以后可能会发现这个问题的人回答这个问题。问题是我不明白“表单数据”的基本概念。换句话说,网页需要将用户名和密码输入到“表单”中,并且需要通过 POST 单击虚拟“提交”按钮,因为它是一个“事件”(即下面的 _eventId)。所以我不得不使用“数据”参数并将所有这些构建为字典。这就是我所做的:

payload = {'_eventId': 'submit', 'lt': 'e1s1', 'submit': 'LOGIN', 'username': 'admin', 'password': 'admin'} 
sessionResp = sessionReq.post(url2, data=payload, params=cookies, verify=False, config=my_config, headers=headers) 


标签:Python
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号