【问题标题】:nodejs authenticate not workingnodejs身份验证不起作用
【发布时间】:2016-07-30 02:59:17
【问题描述】:

我正在使用 nodejs rest api 和身份验证,使用护照基本策略。

当我发送带有身份验证的请求时,服务器会响应:

未知的身份验证策略“基本”

这是我的基本策略:

passport.use(new BasicStrategy(function (userName, password, callback) {
    User.findOne({username: userName}, function (err, user) {
        if (err) return callback(err);
        if (!user) return callback(null, false);
        user.checkPassword(password, function (err, isMatch) {
            if (err)return callback(err);
            if (!isMatch) return callback(null, false);
            return callback(null, user);
        })
    })
}));

这是我的路由器:

router.get("/",  passport.authenticate('basic', { session: false }), function (req, res) {
    bookModel.find(function (err, books) {
        if (!err) {
            return res.json(books);
        } else {
            res.statusCode = 500;
            log.error("Internal error (%d): %s", res.statusCode, err.message)
            return res.json({error: "Server error"});
        }
    });
});

我的java客户端okhttp:

private static void getBooks() throws IOException
    {
        Request request;
        Response response;
        String credentials = Credentials.basic("Axror", "topsecret");
        System.out.println(credentials);
        request = new Request.Builder()
            .url("http://localhost:1337/api/book/")
            .header("Authorization", credentials)
            .build();
        response = client.newCall(request).execute();
        System.out.println(response.body().string());
    }

【问题讨论】:

    标签: node.js authentication passport.js


    【解决方案1】:

    您可能错过了初始化护照。您需要:

    var app = express();
    ......
    ......
    app.use(passport.initialize());
    

    【讨论】:

      【解决方案2】:

      这是你写的吗?

      var BasicStrategy = require('passport-http').BasicStrategy;
      

      这是使用BasicStrategy 所必需的。

      【讨论】:

        猜你喜欢
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        • 2016-02-26
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多