简体   繁体   English

带有Express的http-auth-不断弹出登录提示

[英]http-auth with express - login prompt popping up endlessly

So I'm trying to password protect my node.js app with http-auth. 因此,我尝试使用http-auth密码保护我的node.js应用程序。 I saw many people that needed help with that too and tried multiple things. 我看到许多人也需要帮助,并尝试了多种方法。 But now I'm stuck. 但是现在我被卡住了。 The password is working fine, but the login prompt pops up again as soon as you logged in into the previous one. 密码可以正常工作,但是一旦您登录到上一个,登录提示就会再次弹出。 I can't get full access to my app. 我无法完全访问我的应用。

My auth-part looks like this: 我的身份验证部分如下所示:

var preAuth = require('http-auth');
var basic = preAuth.basic({
    realm: "Private area",
    file: __dirname + "/htpasswd",
    //Type: "basic"
});


var server;
var app = express();
app.use(preAuth.connect(basic));

If I add a 如果我添加一个

app.get('/test', function(req, res) {
    res.send("Hello from express - " + req.user + "!");
});

I can access the /test site no problem and it shows me "Hello from express - insertUsernameHere!" 我可以毫无问题地访问/ test站点,并且显示“您好,快递-insertUsernameHere!”。

But it won't work with my app. 但这不适用于我的应用程序。 Is this because of my express.static? 这是因为我的express.static吗? I'm not that good with javascript/node.js and still learning. 我对javascript / node.js不太满意,仍然在学习。

(And sorry if I upset anybody with my english, I know it is not perfect) (对不起,如果我的英语让任何人不高兴,我知道这并不完美)

You probably defined app.use(auth.connect(basic)); 您可能定义了app.use(auth.connect(basic)); after specifying the static files. 指定静态文件之后

That's the reason your static files are not protected. 这就是您的静态文件不受保护的原因。

Make sure you set the authorization before setting the use of static files: 设置使用静态文件之前,请确保设置了授权:

app.use(preAuth.connect(basic));
app.use(express.static(path.join(__dirname, 'public')));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM