修复 Headless 模式下使用 SSO 登录失败的问题

判断为 NJUPT 的 SSO 统一身份认证拥有更严格的反爬设计,因此在 kimi 的帮助下设置了更拟真的浏览器特征。
This commit is contained in:
2026-04-21 22:05:45 +08:00
parent 9d956e5952
commit c96cbf086e
2 changed files with 16 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ from playwright.async_api import (
BrowserContext,
Page,
Playwright,
ViewportSize,
async_playwright,
)
@@ -29,6 +30,7 @@ class PlayContextManager:
self.playwright = await async_playwright().start() # 不是 __enter__
self.browser = await self.playwright.chromium.launch(
headless=config.get("schedule", "playwright_headless", True),
channel="chromium",
args=[
"--disable-blink-features=AutomationControlled",
"--no-sandbox",
@@ -38,7 +40,18 @@ class PlayContextManager:
"--no-proxy-server",
],
)
self.context = await self.browser.new_context()
self.context = await self.browser.new_context(
user_agent=(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
),
viewport=ViewportSize(width=1920, height=1080),
locale="zh-CN",
timezone_id="Asia/Shanghai",
extra_http_headers={
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
},
)
self.page = await self.context.new_page()
async def __aenter__(self) -> "PlayContextManager":

View File

@@ -27,9 +27,10 @@ class SSO(PlayContextManager):
await self.page.click('button[type="button"]')
await self.page.wait_for_load_state("networkidle")
await self.page.wait_for_timeout(2000)
if "user-login" in self.page.url:
logger.error(f"{username} | 登录失败,请检查学号和密码是否正确。")
raise LoginError("unknown")
raise LoginError("SSO 登录未成功,可能是学号密码错误,请重试。")
logger.info(f"{username} | 登录南邮统一身份认证成功。")
self.isLogin = True