|
|
@@ -1300,7 +1300,7 @@ app.get('/api/auto-login/:siteId', async (req, res) => {
|
|
|
return res.send(intermediateHtml);
|
|
|
}
|
|
|
|
|
|
- // 对于 GET 查询登录,如果有 redirectUrl,使用 HTML 页面设置 Cookie 后跳转
|
|
|
+ // 对于 GET 查询登录,使用 iframe 加载登录 URL 让服务器设置 Cookie
|
|
|
if (config.loginMethod === 'get-query-login' && loginResult.redirectUrl) {
|
|
|
console.log(`[${requestId}] GET 查询登录成功,重定向到: ${loginResult.redirectUrl}`);
|
|
|
|
|
|
@@ -1311,28 +1311,123 @@ app.get('/api/auto-login/:siteId', async (req, res) => {
|
|
|
console.log(`[${requestId}] Cookie ${index + 1}: ${cookie.name} = ${cookie.value.substring(0, 20)}...`);
|
|
|
});
|
|
|
|
|
|
- // 生成跳转 HTML,确保 Cookie 正确设置
|
|
|
- const html = generateRedirectHTML(
|
|
|
- cookieData,
|
|
|
- config.targetHost,
|
|
|
- config.targetDomain,
|
|
|
- requestId,
|
|
|
- loginResult.redirectUrl,
|
|
|
- null
|
|
|
- );
|
|
|
+ // 重新构建登录 URL(带参数)
|
|
|
+ const { targetBaseUrl, loginUrl, loginMethodConfig } = config;
|
|
|
+ const { usernameParam, passwordParam, entCode, saveCookie, isOnly } = loginMethodConfig;
|
|
|
+ const params = new URLSearchParams();
|
|
|
+ params.append(usernameParam, credentials.username);
|
|
|
+ params.append(passwordParam, credentials.password);
|
|
|
+ params.append('ent_code', entCode);
|
|
|
+ params.append('code', 'undefined');
|
|
|
+ params.append('mySel', 'undefined');
|
|
|
+ params.append('saveCookie', saveCookie);
|
|
|
+ params.append('isOnly', isOnly);
|
|
|
+ params.append('_', Date.now().toString());
|
|
|
+ const loginUrlWithParams = `${targetBaseUrl}${loginUrl}?${params.toString()}`;
|
|
|
|
|
|
- // 在响应头中设置 Cookie
|
|
|
- console.log(`[${requestId}] 设置响应头 Cookie...`);
|
|
|
- loginResult.cookies.forEach((cookie, index) => {
|
|
|
- // 修改 Cookie 的 Domain,移除端口号
|
|
|
- let modifiedCookie = cookie.replace(/Domain=[^;]+/i, `Domain=${config.targetDomain}`);
|
|
|
- res.setHeader('Set-Cookie', modifiedCookie);
|
|
|
- console.log(`[${requestId}] 设置 Cookie ${index + 1}: ${modifiedCookie.substring(0, 80)}...`);
|
|
|
- });
|
|
|
+ // 生成特殊 HTML:使用 iframe 加载登录 URL,让服务器设置 Cookie
|
|
|
+ const html = `
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="zh-CN">
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>自动登录中...</title>
|
|
|
+ <style>
|
|
|
+ body {
|
|
|
+ display: flex;
|
|
|
+ justify-content: center;
|
|
|
+ align-items: center;
|
|
|
+ height: 100vh;
|
|
|
+ margin: 0;
|
|
|
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
|
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
|
|
+ }
|
|
|
+ .loading {
|
|
|
+ text-align: center;
|
|
|
+ }
|
|
|
+ .spinner {
|
|
|
+ border: 4px solid #f3f3f3;
|
|
|
+ border-top: 4px solid #3498db;
|
|
|
+ border-radius: 50%;
|
|
|
+ width: 50px;
|
|
|
+ height: 50px;
|
|
|
+ animation: spin 1s linear infinite;
|
|
|
+ margin: 0 auto 20px;
|
|
|
+ }
|
|
|
+ @keyframes spin {
|
|
|
+ 0% { transform: rotate(0deg); }
|
|
|
+ 100% { transform: rotate(360deg); }
|
|
|
+ }
|
|
|
+ .message {
|
|
|
+ color: #333;
|
|
|
+ font-size: 18px;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <div class="loading">
|
|
|
+ <div class="spinner"></div>
|
|
|
+ <div class="message">正在自动登录,请稍候...</div>
|
|
|
+ </div>
|
|
|
+ <iframe id="loginFrame" style="display:none; width:1px; height:1px;"></iframe>
|
|
|
+ <script>
|
|
|
+ (function() {
|
|
|
+ const loginUrl = '${loginUrlWithParams}';
|
|
|
+ const targetUrl = '${loginResult.redirectUrl}';
|
|
|
+
|
|
|
+ console.log('[浏览器端] GET 查询登录 - 使用 iframe 方案');
|
|
|
+ console.log('[浏览器端] 登录 URL:', loginUrl);
|
|
|
+ console.log('[浏览器端] 目标 URL:', targetUrl);
|
|
|
+
|
|
|
+ const iframe = document.getElementById('loginFrame');
|
|
|
+ let loginComplete = false;
|
|
|
+
|
|
|
+ // iframe 加载完成后的处理
|
|
|
+ iframe.onload = function() {
|
|
|
+ if (!loginComplete) {
|
|
|
+ loginComplete = true;
|
|
|
+ console.log('[浏览器端] iframe 登录请求完成,等待 Cookie 设置...');
|
|
|
+
|
|
|
+ // 延迟跳转,确保 Cookie 已设置
|
|
|
+ setTimeout(function() {
|
|
|
+ console.log('[浏览器端] 跳转到目标页面:', targetUrl);
|
|
|
+ window.location.href = targetUrl;
|
|
|
+ }, 1000);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ iframe.onerror = function(error) {
|
|
|
+ console.error('[浏览器端] iframe 加载失败:', error);
|
|
|
+ // 即使失败也尝试跳转
|
|
|
+ setTimeout(function() {
|
|
|
+ console.log('[浏览器端] 跳转到目标页面(即使 iframe 失败):', targetUrl);
|
|
|
+ window.location.href = targetUrl;
|
|
|
+ }, 2000);
|
|
|
+ };
|
|
|
+
|
|
|
+ // 加载登录 URL,让服务器设置 Cookie
|
|
|
+ console.log('[浏览器端] 开始加载登录 URL...');
|
|
|
+ iframe.src = loginUrl;
|
|
|
+
|
|
|
+ // 保险:10秒后强制跳转
|
|
|
+ setTimeout(function() {
|
|
|
+ if (!loginComplete) {
|
|
|
+ console.warn('[浏览器端] 超时,强制跳转');
|
|
|
+ window.location.href = targetUrl;
|
|
|
+ }
|
|
|
+ }, 10000);
|
|
|
+ })();
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+ `;
|
|
|
|
|
|
const duration = Date.now() - startTime;
|
|
|
console.log(`[${requestId}] 总耗时: ${duration}ms`);
|
|
|
- console.log(`[${requestId}] 返回跳转页面`);
|
|
|
+ console.log(`[${requestId}] 返回 iframe 登录页面`);
|
|
|
+ console.log(`[${requestId}] 登录 URL: ${loginUrlWithParams}`);
|
|
|
+ console.log(`[${requestId}] 目标 URL: ${loginResult.redirectUrl}`);
|
|
|
console.log('='.repeat(80) + '\n');
|
|
|
|
|
|
return res.send(html);
|