使用护照和 Nginx 作为代理服务器的身份验证问题 Node.js
Authentication issue Node.js with passport and Nginx as proxy server
我是 运行 一个节点应用程序,它使用 Passport.js 在端口 3000 上进行身份验证。
Nginx作为代理服务器监听80端口,代理将请求传递到3000端口。
Passport.js 用于用户身份验证。
认证协议如下:
用户请求 example.com 并被重定向到 example.com/login 如果他没有登录。
成功登录后,用户将再次重定向到 example.com.
当我尝试在Ipad和Internet Explorer 9 中使用Safari 6 登录时出现此问题(怀疑客户有同样的问题)。
发生的情况是应用程序重定向到 example.com/login 而不是 example.com/ 当使用正确的凭据时。
例如Chrome40就不会出现这个问题
- 在Safari中使用example.com:3000避开nginx时不会出现该问题。
- 更糟的是:有时它会无缘无故地工作。
我怀疑它与Nginx 和请求文件的顺序有关。
Nginx 配置:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
部分应用代码:
app.post('/api/login', function (req, res, next) {
passport.authenticate('local-login', function (err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.status(401).send(info);
}
req.logIn(user, function (err) {
if (err) {
return next(err);
}
return res.send({
username: user.username
});
});
})(req, res, next);
});
app.get('/', isLoggedIn, function (req, res) {
res.sendFile(__dirname + '/client/views/index.html');
});
function isLoggedIn(req, res, next) {
if (!req.isAuthenticated()) {
res.redirect('/login');
} else {
next();
}
}
我想知道是否有人可以帮助我解决这个问题。
我很乐意在需要时提供额外的代码或解释。
原来与Nginx无关。
通过使用 example.com:3000 直接访问,问题偶尔也会发生。
我注意到浏览器有一次说我正在访问 example.com 并且正在显示示例。com/login。
我在互联网上找不到任何证据,但我怀疑 Safari/IE9 缓存了重定向的页面并将其 link 到原始 URL。
所以这是接下来发生的事情的故事情节。
- 用户浏览到 example.com/
- 重定向到示例。com/login(浏览器缓存 example.com/ 但保存登录页面)
- 用户登录并被重定向到 example.com/
- 浏览器在缓存中有 / 并加载登录页面。
- 开发人员无法解释的错误和头痛。
'Solved' 通过添加在请求 / 时不添加缓存 headers 的中间件。
app.get('/', isLoggedIn, noCache, function (req, res) {
res.sendFile(__dirname + '/client/views/index.html');
});
function noCache(req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}
我是 运行 一个节点应用程序,它使用 Passport.js 在端口 3000 上进行身份验证。 Nginx作为代理服务器监听80端口,代理将请求传递到3000端口。 Passport.js 用于用户身份验证。
认证协议如下: 用户请求 example.com 并被重定向到 example.com/login 如果他没有登录。 成功登录后,用户将再次重定向到 example.com.
当我尝试在Ipad和Internet Explorer 9 中使用Safari 6 登录时出现此问题(怀疑客户有同样的问题)。 发生的情况是应用程序重定向到 example.com/login 而不是 example.com/ 当使用正确的凭据时。
例如Chrome40就不会出现这个问题
- 在Safari中使用example.com:3000避开nginx时不会出现该问题。
- 更糟的是:有时它会无缘无故地工作。
我怀疑它与Nginx 和请求文件的顺序有关。
Nginx 配置:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
部分应用代码:
app.post('/api/login', function (req, res, next) {
passport.authenticate('local-login', function (err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.status(401).send(info);
}
req.logIn(user, function (err) {
if (err) {
return next(err);
}
return res.send({
username: user.username
});
});
})(req, res, next);
});
app.get('/', isLoggedIn, function (req, res) {
res.sendFile(__dirname + '/client/views/index.html');
});
function isLoggedIn(req, res, next) {
if (!req.isAuthenticated()) {
res.redirect('/login');
} else {
next();
}
}
我想知道是否有人可以帮助我解决这个问题。 我很乐意在需要时提供额外的代码或解释。
原来与Nginx无关。 通过使用 example.com:3000 直接访问,问题偶尔也会发生。
我注意到浏览器有一次说我正在访问 example.com 并且正在显示示例。com/login。 我在互联网上找不到任何证据,但我怀疑 Safari/IE9 缓存了重定向的页面并将其 link 到原始 URL。 所以这是接下来发生的事情的故事情节。
- 用户浏览到 example.com/
- 重定向到示例。com/login(浏览器缓存 example.com/ 但保存登录页面)
- 用户登录并被重定向到 example.com/
- 浏览器在缓存中有 / 并加载登录页面。
- 开发人员无法解释的错误和头痛。
'Solved' 通过添加在请求 / 时不添加缓存 headers 的中间件。
app.get('/', isLoggedIn, noCache, function (req, res) {
res.sendFile(__dirname + '/client/views/index.html');
});
function noCache(req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}