用护照-jwt快递:我在邮递员中不断遭到未经授权

时间:2019-05-29 21:00:48

标签: node.js express jwt

我的passport-jwt文件是根据const passport = require("passport"); const User = require("../models/user"); const config = require("../config"); const JwtStrategy = require("passport-jwt").Strategy; const ExtractJwt = require("passport-jwt").ExtractJwt; // Setup options for JWT JwtStrategy const jwtOptions = { jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken("authorization"), secretOrKey: "secret" }; // Create JWT Strategy const jwtLogin = new JwtStrategy(jwtOptions, function(jwt_payload, done) { // See if the user id in the payload exists in our database // If so, call 'done' with that user // Otherwise, call done without a user object User.findOne({ id: jwt_payload.sub }, function(err, user) { if (err) { return done(err, false); } if (user) { return done(null, user); } else { return done(null, false); } }); }); // Tell Passport to use this strategy passport.use(jwtLogin); 文档编写的:

const passport = require("passport");
const User = require("../models/user");
const config = require("../config");
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;

// Setup options for JWT JwtStrategy
const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromHeader("authorization"),
  secretOrKey: "secret"
};

// Create JWT Strategy
const jwtLogin = new JwtStrategy(jwtOptions, function(jwt_payload, done) {
  // See if the user id in the payload exists in our database
  // If so, call 'done' with that user
  // Otherwise, call done without a user object
  User.findOne({ id: jwt_payload.sub }, function(err, user) {
    if (err) {
      return done(err, false);
    }
    if (user) {
      return done(null, user);
    } else {
      return done(null, false);
    }
  });
});

// Tell Passport to use this strategy
passport.use(jwtLogin);

但是我在邮递员那里得到了无法解释的未授权。

然后我像上面那样重构上面的文件:

authentication.js

什么都没有,我遵循了该指南: passport-jwt 401 Unauthorized 本指南: Passport-jwt Unauthorized 本指南: Passport Jwt Unauthorized 一个这个: Express middleware Passport not responding unauthorized

所以这些解决方案都不适合我。

是我的const jwt = require("jwt-simple"); const User = require("../models/user"); const config = require("../config"); function tokenForUser(user) { const timestamp = new Date().getTime(); return jwt.encode({ sub: user.id, iat: timestamp }, config.secret); } exports.signup = function(req, res, next) { const email = req.body.email; const password = req.body.password; if (!email || !password) { return res .status(422) .send({ error: "You must provide an email and password" }); } // See if a user with a given email exists User.findOne({ email: email }, function(err, existingUser) { if (err) { return next(err); } // If a user with email does exist, return an error if (existingUser) { return res.status(422).send({ error: "Email is in use" }); } // If a user with email does not exist, create and save user record const user = new User({ email: email, password: password }); user.save(function(err) { if (err) { return next(err); } // Respond to request indicating the user was created res.json({ token: tokenForUser(user) }); }); }); }; 文件中的问题代码:

router.js

是我的const Authentication = require("./controllers/authentication"); const passportService = require("./services/passport"); const passport = require("passport"); const requireAuth = passport.authenticate("jwt", { session: false }); module.exports = function(app) { app.get("/", requireAuth, function(req, res) { res.send({ greeting: "howdy" }); }); app.post("/signup", Authentication.signup); }; 文件中的问题代码:

"passport": "0.4.0",
"passport-jwt": "4.0.0"

我正在使用:

http://localhost:3090/signup

因此,我首先向raw中的JSON (application/json)发送{ "email": "test902@example.com", "password": "123" } 的POST请求,并添加电子邮件和密码,如下所示:

http://localhost:3090/

我成功获取了相同JSON格式的令牌,然后创建了对Headers的GET请求,单击InputStream input = new FileInputStream("Path to//config.properties"); Properties prop = new Properties(); prop.load(input); System.setProperty("log4j2.configurationFile", prop.getProperty("log4j.path"));并键入以下内容: enter image description here

0 个答案:

没有答案