url
/wapi/v1/getLoginInfo/username/${name}
回参
"code": 200,
"data : {
"access_token": "xxxxx",
"authority": "xxxxx",
"username": "xxxxx",
"userId": "xxxxx"
},
"message": "success"
<!DOCTYPE html>
<html lang="zh">
<head>
</head>
<body>
<script>
// 获取cookie
function getCookie(name) {
let cookieArray = document.cookie.split(";"); // 将cookie字符串分割成数组
let cookieName = name + "="; // 要查找的cookie名,添加等号
for (let i = 0; i < cookieArray.length; i++) {
let c = cookieArray[i];
while (c.charAt(0) === " ") {
c = c.substring(1); // 去除可能的空格
}
if (c.indexOf(cookieName) === 0) { // 如果找到了cookie名
return c.substring(cookieName.length, c.length); // 返回cookie的值
}
}
return ""; // 如果没有找到cookie,返回空字符串
}
// 跳转至第三方sso认证
const redirectSso = () => {
const currentUrl = location.href;
const ssoUrl = 'xxxxx';
const redirectUrl = `${ssoUrl}?${encodeURIComponent(currentUrl)}`;
location.href = redirectUrl;
}
// 获取第三方用户信息
const fetchCurrentUser = async () => {
const currentUserApiUrl = 'xxxxx';
const response = await fetch(currentUserApiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'REQUEST_HEADER': 'binary-http-client-header'
},
body: {}
})
return new Promise((resolve, reject) => {
if (response.ok) {
response.json()
.then(responseData => {
if (responseData.success) {
resolve(responseData.data);
} else {
reject(null);
}
})
.catch(err => { reject(err) })
} else {
reject(response)
}
})
}
const init = async () => {
// 获取token 通过localStorage或cookie
const token = localStorage.getItem('tk');
// const token = getCookie("token");
if (token) {
const user = await fetchCurrentUser();
if (user) {
const response = await fetch(`/wapi/v1/getLoginInfo/username/${user.name}`)
.then((res) => {
return res.json();
})
.then((res) => {
const userToken = res.data.access_token;
const username = res.data.username;
const userId = res.data.userId;
const authority = res.data.authority;
location.href = `protocol://host/diagram.html?id=24000000000001&userToken=${userToken}&username=${username}&userId=${userId}&authority=${authority}`;
})
} else {
redirectSso();
}
} else {
redirectSso();
}
}
init();
</script>
</body>
</html>
第三方
location /nVisual {
proxy_pass sso.html
}
nVisual
location /access.html {
proxy_pass 第三方认证页面
}