我有基于nodejs和angularjs的应用程序,我使用基于jwt令牌的身份验证进行身份验证,并且api调用正常工作
即使用户现在没有登录,应用程序也会为所有静态资源提供服务,如果用户没有登录并将用户重定向到登录页面,如何避免加载应用程序
最后我能够在app.js floder中找到它,添加代码sinpet app.use('/ app / view / *',function(req,res,next){
if (!req.headers['authorization'] ) {
res.sendfile('app/views/Error.html');
} else {
next();
}
});
这意味着请求/ app / view /检查请求的标头是否包含使用jwt生成的令牌
答案 0 :(得分:1)
如果你的JWT存储在一个cookie中,你可以使用这样的道路:
router.all('/*', function(req, res, next){
if (!req.cookies.session) {
return res.json("ERROR");
}
else {
ValidateCookieFunction(req.cookies.session, function(auth_state) {
if (!auth_state)
return res.json("ERROR");
else
next();
});
}
});
否则您可以在HTTP-Header中提供JWT
router.all('/*', function(req, res, next){
if (!req.headers['x-access-token']) {
return res.json("ERROR");
}
else {
ValidateJWTFunction(req.headers['x-access-token'], function(auth_state) {
if (!auth_state)
return res.json("ERROR");
else
next();
});
}
});