简体   繁体   English

使用express-jwt VS passport-jwt的JSONWebTokens

[英]JSONWebTokens with express-jwt VS passport-jwt

The express-jwt package allows tremendous flexibility in creating multiple authentication options on login( ie local storage or social media OAuth or OpenID providers, etc. ) and then protecting the application with JWT. express-jwt包允许在登录时创建多个身份验证选项(即本地存储或社交媒体OAuth或OpenID提供程序等),然后使用JWT保护应用程序,具有极大的灵活性。

The express-jwt configuration in particular ie 特别是express-jwt配置即

app.use(expressJwt({ secret: jwtSecret}).unless({path:['/login']}));

shows the way. 显示方式。

The question is: many of the sites I want to use for login alternatives are most easily accessed through passport.js. 问题是:我想用于登录备选方案的许多站点最容易通过passport.js访问。 Passport-jwt seems to use the jsonwebtokens.js module under the hood so is there a way of configuring passport-jwt with the same flexibility that can be obtained with jsonwebtokens.js and express-jwt.js individually? Passport-jwt似乎使用了引擎盖下的jsonwebtokens.js模块,那么有没有一种配置passport-jwt的方法具有相同的灵活性,可以通过jsonwebtokens.js和express-jwt.js单独获得?

Yes there is. 就在这里。 Passport has many configurations, what it terms strategies. Passport有许多配置,它的术语策略。 One of those is passport-jwt: https://github.com/themikenicholson/passport-jwt 其中一个是passport-jwt: https//github.com/themikenicholson/passport-jwt

Here is a decent guide to use it with an API server: http://slatepeak.com/guides/building-a-software-as-a-service-saas-startup-pt-2/ 以下是与API服务器一起使用的合适指南: http//slatepeak.com/guides/building-a-software-as-a-service-saas-startup-pt-2/

Here is an example with a basic express app config assumed. 这是一个假设基本快速应用程序配置的示例。

// init express app as normal..
var app = express();
// dependancies
var passport = require('passport');
var jwt = require('jwt-simple');
var User = require('path/to/your/db/model'); // eg. mongo
// initialize passport
app.use(passport.initialize());
app.use(passport.session());
// configure passport jwt strategy
var JwtStrategy = require('passport-jwt').Strategy;
module.exports = function(passport) {
  // JSON Web Token Strategy
  passport.use(new JwtStrategy({ secretOrKey: 'secret' }, function(jwt_payload, done) {
    User.findOne({id: jwt_payload.id}, function(err, user) {
      if (err) return done(err, false);
      if (user) done(null, user);
      else done(null, false);
      });
  }));
};
// now have an authentication route
app.post('/admin/authenticate', function(req, res) {
  User.findOne({
    email: req.body.email
  }, function(err, user) {
    // create jwt token
    var token = jwt.encode(user, 'secret');
    if (err) {
      res.send({success: false, msg: 'error'});
    } else {
      res.json({success: true, token: 'JWT ' + token});
    }  
  });
});
// finally require passport strategy to secure certain routes..
app.get('/admin/getsomedata', passport.authenticate('jwt', {session: false}), successFunction);

To answer your question - in my experience yes I think it offers a lot flexibility like express-jwt, if not more, and can be abstracted from your main code easily too. 回答你的问题 - 根据我的经验,我认为它提供了很多灵活性,如express-jwt,如果不是更多,也可以很容易地从主代码中抽象出来。

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

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