Express (NodeJS) - 代理应用程序的身份验证
Express (NodeJS) - authentication for proxy app
我有两台服务器:
- 服务器 A - Express/FeathersJS - 可公开访问,通过 jwt
进行身份验证
- 服务器 X - Django 应用程序 - 不可公开访问 - 无需身份验证
服务器 A 由几个 API 组成,一个通过 JWT 的身份验证 API,以及一个 front-end javascript 允许用户访问 [=35] 的应用程序=] 和 sign-in.
服务器 X 由一个前端应用程序和一些 APIs 组成,但不可公开访问,也没有访问应用程序的身份验证。
我的目标是让用户登录服务器 A,然后通过代理访问服务器 X。
所以 - 我已经实现了 express 中间件 http-proxy-middleware
。它的工作,除了当我尝试使用某种身份验证保护路由时 - 验证 JWT 的 built-in 方法期望 Authorization bearer
jwt header,这是不可能用 [=12] 做的=] 像我这样的请求访问此应用程序。
有人有什么建议吗?
解法:
如此处记录:How to add a users auth in feathers middleware?
- 登录成功后document.cookie设置cookie。
app.authenticate({
strategy: 'local',
username: $('#inputUsername').val(),
password: $('#inputPassword').val(),
}).then( result => {
document.cookie = "feathers-jwt=" + result.accessToken;
window.location.href = "/";
}).catch(error => {});
- 在代理路由上解析 cookie:
// authenticateCookie.js
module.exports = function authenticateCookie(req, res, next){
const cookies = req.cookies;
const token = cookies['feathers-jwt'];
if(token){
logger('Found cookie in feathers-jwt');
req.authentication = {
strategy: 'jwt',
accessToken: token,
};
}
next();
};
// proxyMiddleware.js
app.use(urlRegex,
// parse and handle jwt cookies
cookieParser(),
authenticateCookie,
// logging function
(req, res, next) => {
logger(req.url);
next();
},
// validate jwt cookie
authenticate('jwt'),
// proxy requests upstream
createProxyMiddleware({
target: url,
changeOrigin: true,
auth: auth,
}),
)
我有两台服务器:
- 服务器 A - Express/FeathersJS - 可公开访问,通过 jwt 进行身份验证
- 服务器 X - Django 应用程序 - 不可公开访问 - 无需身份验证
服务器 A 由几个 API 组成,一个通过 JWT 的身份验证 API,以及一个 front-end javascript 允许用户访问 [=35] 的应用程序=] 和 sign-in.
服务器 X 由一个前端应用程序和一些 APIs 组成,但不可公开访问,也没有访问应用程序的身份验证。
我的目标是让用户登录服务器 A,然后通过代理访问服务器 X。
所以 - 我已经实现了 express 中间件 http-proxy-middleware
。它的工作,除了当我尝试使用某种身份验证保护路由时 - 验证 JWT 的 built-in 方法期望 Authorization bearer
jwt header,这是不可能用 [=12] 做的=] 像我这样的请求访问此应用程序。
有人有什么建议吗?
解法:
如此处记录:How to add a users auth in feathers middleware?
- 登录成功后document.cookie设置cookie。
app.authenticate({
strategy: 'local',
username: $('#inputUsername').val(),
password: $('#inputPassword').val(),
}).then( result => {
document.cookie = "feathers-jwt=" + result.accessToken;
window.location.href = "/";
}).catch(error => {});
- 在代理路由上解析 cookie:
// authenticateCookie.js
module.exports = function authenticateCookie(req, res, next){
const cookies = req.cookies;
const token = cookies['feathers-jwt'];
if(token){
logger('Found cookie in feathers-jwt');
req.authentication = {
strategy: 'jwt',
accessToken: token,
};
}
next();
};
// proxyMiddleware.js
app.use(urlRegex,
// parse and handle jwt cookies
cookieParser(),
authenticateCookie,
// logging function
(req, res, next) => {
logger(req.url);
next();
},
// validate jwt cookie
authenticate('jwt'),
// proxy requests upstream
createProxyMiddleware({
target: url,
changeOrigin: true,
auth: auth,
}),
)