【发布时间】: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