【发布时间】:2018-02-19 13:06:10
【问题描述】:
我正在使用 node.js express 应用程序。 jsonwebtoken 用于身份验证。我想从 jsonwebtoken 验证中排除一些 api url。以下是我尝试过的和我的代码
router.use('/authentication', mountAllRoutes(authenticationModule));
// route middleware to verify a token
router.use((req, res, next) => {
const r = req;
const token = req.body.token || req.query.token || req.headers.authorization;
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, (req.app.get('superSecret')), (err, decoded) => {
if (err) {
// res.json({ success: false, message: 'Failed to authenticate token.' });
res.status(401).send({
success: false,
message: 'Failed to authenticate token.'
});
} else {
// if everything is good, save to request for use in other routes
r.decoded = decoded;
next();
// console.log(decoded);
}
return {};
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
return {};
});
router.use('/test', mountAllRoutes(testModule));
router.use('/other', mountAllRoutes(otherModule));
router.use('/data', mountAllRoutes(dataModule));
在这里,我将路由放置在我不想保护的中间件之上。我已经放置在我想要保护的中间件之后。但它是受保护的,我放在中间件之上。在 authenticationModule 中,登录和用户注册 api 来了。所以对于用户注册它会给出响应没有提供令牌
【问题讨论】:
标签: node.js express json-web-token