登录接口有统一认证,访问该登录接口时会跳转到认证界面输入用户名密码
url = "https://auth.xxxxx.com/auth/realms/xxxoa-with-os/protocol/openid-connect/auth?response_type=code&client_id=tests&scope=openid&redirect_uri=https://xxxnew.xxxxx.net"
response = requests.get(url)
if response.status_code == 200:
# 从接口的响应中获取重定向的登录接口url
new_url = re.findall(r'action="(.*?)" method=', response.text)[0]
该接口返回内容如下
<div id="kc-form-wrapper" >
<form id="kc-form-login" onsubmit="login.disabled = true; return true;" action="https://auth.xxxx.com/auth/realms/xxx-with-os/login-actions/authenticate?session_code=9k3MqNnprJeoM41ugw4B_JvuspNABjCQvzSK-0jXwvg&execution=dfd7b3b2-6595-47d4-b96d-655ff493677e&client_id=test&tab_id=vxRO5NTwtLE" method="post">
<div class="form-group">
<label for="username" class="control-label">账号</label>
<input tabindex="1" id="username" placeholder="请输入域账号" class="form-control user-input" name="username" value="" type="text" autofocus autocomplete="off" />
</div>
<div class="form-group">
<label for="password" class="control-label">密码</label>
<input tabindex="2" id="password" class="form-control user-input" placeholder="请输入登录密码" name="password" type="password" autocomplete="off" />
</div>
<div class="form-group login-pf-settings">
<div id="kc-form-options">
</div>
<div class="">
</div>
</div>
获取到的new_url如下:
https://auth.igwfmc.com/auth/realms/igwoa-with-os/login-actions/authenticate?session_code=ed1wdMv8YR_XHCRRIM3Ua8JNz-pDUAuz6-qIRWuFIfM&execution=dfd7b3b2-6595-47d4-b96d-655ff493677e&client_id=devops&tab_id=jet144uKtaI
返回的url中存在 &
是 HTML 中用于表示字符 &
的转义实体。在处理包含这种 HTML 实体的字符串时,要使用 Python 的 html
模块中的 unescape
函数将其转换回普通字符:
new_url = html.unescape(re.findall(r'action="(.*?)" method=', response.text)[0])
对该接口进行请求,获取后续接口需要的内容
完整代码如下:
# -*- coding: utf-8 -*-
import json
import requests
import re
import html
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def getkeyCloakToken():
url = "https://auth.xxxx.com/auth/xxxoa-with-os/protocol/openid-connect/auth?response_type=code&client_id=devops&scope=openid&redirect_uri=https://aaaanew.xxxx.net/subSysList"
response = requests.get(url)
if response.status_code == 200:
# 请求成功,打印响应内容
new_url = re.findall(r'action="(.*?)" method=', response.text)[0]
print("new_url:",new_url)
new_url = html.unescape(re.findall(r'action="(.*?)" method=', response.text)[0])
print("new_url:", new_url)
AUTH_SESSION_ID = re.findall(r'AUTH_SESSION_ID=(.*?);', str(response.headers))[0]
AUTH_SESSION_ID_LEGACY = re.findall(r'AUTH_SESSION_ID_LEGACY=(.*?);', str(response.headers))[0]
KC_RESTART = re.findall(r'KC_RESTART=(.*?);', str(response.headers))[0]
headers ={
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'AUTH_SESSION_ID='+AUTH_SESSION_ID+'; AUTH_SESSION_ID_LEGACY='+AUTH_SESSION_ID_LEGACY+'; KC_RESTART='+KC_RESTART+'; Hm_lvt_ed25ac3c0e72b77fbab3c2b066a445e7=1715676484,1716192914; Hm_lpvt_ed25ac3c0e72b77fbab3c2b066a445e7=1716192915'
}
print(headers)
data ={
'username':'username',
'password':'password',
'credentialId':''
}
response = requests.post(new_url, headers=headers, data=data,verify=False)
print(response.status_code,response.url)
session_state = re.findall(r'session_state=(.*?)\&code', str(response.url))[0]
code = re.findall(r'code=(.*?)$', str(response.url))[0]
url_3='https://auth.xxxxx.com/auth/xxxoa-with-os/protocol/openid-connect/token'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
'grant_type' : 'authorization_code',
'client_id' : 'tests' ,
"code" :code,
'redirect_uri': 'https://xxxxnew.xxxx.net/aaaa?session_state='+session_state,
'code':code
}
response = requests.post(url_3,headers=headers, data=data).json()
access_token = response['access_token']
token_type =response['token_type']
print(access_token)
print(token_type)
else:
# 请求失败,打印错误信息
print(f"Failed to retrieve data. Status code: {response.status_code}")
getkeyCloakToken()