我正在运行一个Node应用程序,它使用Passport.js在端口3000上进行身份验证.
Nginx用作代理服务器来侦听端口80,代理将请求传递给端口3000.
Passport.js用于身份验证.
认证协议如下:
用户请求example.com,如果他未登录,则会重定向到example.com/login.
成功登录后,用户将再次重定向到example.com.
>当我尝试使用Ipad上的Safari 6和Internet Explorer 9(怀疑客户遇到相同问题)登录时,会出现此问题.
当使用正确的凭据时,应用程序会重定向到example.com/login而不是example.com/.
>例如,Chrome 40中不会出现此问题.
>在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并且正在显示example.com/login.
我在互联网上找不到任何证据,但我怀疑Safari / IE9缓存重定向的页面并将其链接到原始URL.
所以这是接下来发生的事情的故事情节.
>用户浏览example.com/
>重定向到example.com/login(浏览器缓存example.com/但保存登录页面)
>用户登录并重定向到example.com/
>浏览器具有/在缓存中并加载登录页面.
>开发人员无法解释的错误和令人头疼的问题.
通过添加中间件来“解决”,在请求时不添加缓存头.
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();
}
原文地址:https://codeday.me/bug/20190824/1711783.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。