我需要使用Node JS在我的应用程序中实现单一登录。我是Node JS和SSO的新手。我浏览了一些有关SSO的文章和博客,并在我的应用程序中得到了实现。
我将以下代码放置在服务器中,并使用URL-https://qa.sp.*****.net
访问了应用程序。这将重定向到一个新的登录页面(所有应用程序的通用登录页面),如果我们提供正确的凭据,它将导航到所需页面,并按预期显示用户名。
app.js
const express = require('express');
const app = express();
const port = 3000;
const https = require('https');
const axios = require('axios');
app.use(express.static(__dirname));
var code = '';
app.get('/', function (req, res) {
code = req.query.code;
if(code) {
axios({
// make a POST request
method: 'post',
url: `https://federationdev.XXXXXX.com/as/token.oauth2?code=${req.query.code}&client_id=******&grant_type=authorization_code&scope=profile_email+profile_name+profile_sales+profile_location+profile_org+profile_network+profile_contact&redirect_uri=https://qa.sp.*****.net`,
// Set the content type header, so that we get the response in JSON
headers: {
accept: 'application/json'
}
}).then((response) => {
// Once we get the response, extract the access token from the response body
const accessToken = response.data.access_token;
// res.send("Access Token " +accessToken);
return res.redirect(`/welcome.html?access_token=${accessToken}`)
//return res.redirect(`/sample.pdf`); // moving to pdf page if it is uncommented
});
}
else
{
return res.redirect(
`https://federationdev.XXXXXX.com/as/token.oauth2?code=${req.query.code}&client_id=******&grant_type=authorization_code&scope=profile_email+profile_name+profile_sales+profile_location+profile_org+profile_network+profile_contact&redirect_uri=https://qa.sp.*****.net`
);
}
});
app.listen(0, () => console.log(`Example app listening on port ${port}!`));
Welcome.html
// We can get the token from the "access_token" query param, available in the browsers "location" global
const query = window.location.search.substring(1)
const token = query.split('access_token=')[1]
// Call the user info API using the fetch browser library
fetch('https://idp-d.XXXXX.com/upi/profile/', {
headers: {
// Include the token in the Authorization header
Authorization: 'Bearer ' + token
}
})
// Parse the response as JSON
.then(res => res.json())
.then(res => {
const nameNode = document.createTextNode(`Welcome, ${res.displayName}`)
document.body.appendChild(nameNode)
})
现在,我的查询是,如果我想使用ppd/secure/sample1.pdf
在https://qa.sp.*****.net/ppd/secure/sample1.pdf
路径中打开PDF,它应该如上所述重定向到登录页面。但这在我的情况下没有发生,但是PDF直接打开。请帮助我实现此方案,因为服务器内部的子文件夹中将包含许多PDF,因此无论域中的子文件夹如何,我都希望重定向到登录页面。网址示例https://qa.sp.*****.net/secure/sam.pdf
,
https://qa.sp.*****.net/ppd/secure/control/abc.pdf
我在服务器中的文件夹结构。