【问题标题】:How to exclude a router/api url from jsonwebtoken如何从 jsonwebtoken 中排除路由器/api url
【发布时间】: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 来了。所以对于用户注册它会给出响应没有提供令牌

注意:我已转发此链接How-to-ignore-some-request-type-in-Jsonwebtoken

【问题讨论】:

    标签: node.js express json-web-token


    【解决方案1】:

    为要排除的 API 创建单独的路由文件。

    //Routes
    
      var users = require('./routes/users');
      var api = require('./routes/publicApi');
    

    App.js:

    // 路由中间件以验证令牌

    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 {};
    });
    
    
    app.use('/users', router);//will use Token Authentican
    app.use('/publicApi', router);//Dont do this. 
    

    【讨论】:

    • 我创建了单独的 api。在那里只有登录和注册。我已将该路由器放在中间件之上。 router.use('/authentication', mountAllRoutes(authenticationModule));它只有登录和注册api。
    • 仍然令牌要求注册
    • router.use('/authentication', mountAllRoutes(authenticationModule)) 这是mountAllRoutes
    • 只有登录和注册 api 不需要令牌保护。登录它工作完美。它不会要求保护。它会生成正确的令牌密钥,但注册时会询问未提供令牌
    • 不应该。可能缺少某些东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2016-10-29
    相关资源
    最近更新 更多