【问题标题】:NodeJS, passport-jwt: Authenticate all user except in listNodeJS,passport-jwt:验证除列表中的所有用户
【发布时间】:2017-05-16 15:39:47
【问题描述】:

我正在使用passportjs 和passport-jwt 建立一个nodejs 项目。我知道您可以在哪里为要保护的每条路线指定 passport.authenticate 。但是,除了登录和注册之外,我没有看到锁定所有路由器的方法。我看到 express-jwt 允许使用 express-unless 的地方,这似乎完成了这个功能。 passport-jwt 是否有类似的机制,如果有,如何实现?

【问题讨论】:

    标签: node.js passport.js


    【解决方案1】:

    实际上,您甚至不需要express-unless,您可以使用 express 允许注册始终执行的中间件来进行过滤

    const express = require('express');
    const app = express(); 
    
    function authenticateSomeRoutesMiddleware(req, res, next) {
        if (/(login|register)/.test(req.originalUrl)) {
            // No authentication needed
            return next();
        } else {
            // Option 1 => use default passport logic 
            // which respond with a 401 unauthorized status if authentication fails
            passport.authenticate('jwt', { session: false}), function(req, res, next) {
                // Do something now you know that the user has been authenticated
                return next(); // this will call the next middleware on the stack 
            })(req, res, next);
    
            // Option 2: use a custom callback to allow your application 
            // to handle success or failure
            // As per passport spec: 
            // - If authentication failed, user will be set to false. 
            // - If an exception occurred, err will be set. 
            // - An optional info argument will be passed, containing additional details 
            // provided by the strategy's verify callback.
    
            passport.authenticate('local', function(err, user, info) {
                if (err) {
                    // Error in authentication process; handle it or call...
                    return next(err);
                }
                if (!user) {
                    // Authentication failed (based on your strategy's implementation)
                    // You can for example try again
                    return res.redirect('/login');
                }
    
                // If you are using session to store the user call req.logIn() else call `return next()` directly
                req.logIn(user, function(err) {
                    if (err) { return next(err); }
                    return next();
                });
            })(req, res, next);
        }
    }
    
    
    // add this BEFORE your route definitions
    app.use(authenticateSomeRoutesMiddleware);
    
    // add all your routes here
    app.use('/login', function(req, res, next) {
        // do something
    });
    app.use('/register', function(req, res, next) {
        // do something else
    });
    app.use('/some/protected/route', function(req, res, next) {
        // this will get called once the authentication process has been cleared
    });
    //...
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 2020-08-26
      • 2016-06-02
      • 1970-01-01
      • 2017-10-25
      • 2017-05-13
      • 2016-01-02
      • 2018-05-19
      • 1970-01-01
      相关资源
      最近更新 更多