文章
js实现dolphinscheduler自动登录
需求:第三方跳转ds页面需自动登录后跳转到指定ds页面
在dolphinscheudler安装目录ui文件夹下新建文件: redirect.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DS Auto Login</title>
</head>
<body>
<p>正在检查登录状态...</p>
<script>
(async function () {
try {
const urlParams = new URLSearchParams(window.location.search);
let nextUrl = urlParams.get('nextUrl');
if (!nextUrl) {
nextUrl = '/dolphinscheduler/ui/';
}
// ✅ 第一步:检查是否已登录(从 localStorage 读取)
const userStr = localStorage.getItem('user');
if (userStr) {
try {
const userObj = JSON.parse(userStr);
const sessionId = userObj?.sessionId;
if (sessionId) {
console.log('已登录,直接跳转');
window.location.href = nextUrl;
return; // ⬅️ 提前退出,不再登录
}
} catch (e) {
console.warn('localStorage.user 格式异常,将重新登录', e);
}
}
// ❌ 未登录:执行自动登录
console.log('未检测到有效登录,开始自动登录...');
const USERNAME = "datacenter";
const PASSWORD = "your_actual_password_here"; // ← 替换为真实密码
const formData = new URLSearchParams();
formData.append('userName', USERNAME);
formData.append('userPassword', PASSWORD);
const res = await fetch('/dolphinscheduler/login', {
method: 'POST',
body: formData
});
const data = await res.json();
if (data.code !== 0) {
alert('自动登录失败: ' + (data.msg || '未知错误'));
return;
}
// 保存登录信息
localStorage.setItem('user', JSON.stringify({
sessionId: data.data.sessionId,
securityConfigType: "PASSWORD",
userInfo: data.data.userInfo || {}
}));
// 跳转
window.location.href = nextUrl;
} catch (err) {
console.error('Auto login error:', err);
alert('自动登录异常,请手动登录');
}
})();
</script>
</body>
</html>https://your-ds-host:12345/redirect.html?nextUrl=/dolphinscheduler/ui