【问题标题】:Digest auth using http-auth使用 http-auth 消化身份验证
【发布时间】:2017-12-05 01:41:38
【问题描述】:

尝试在 nodejs 中实现 Digest auth。下面是代码

var http = require('http');
var auth = require('http-auth');
var express = require('express');
var app = express();
var user;

var basic = auth.basic({
    realm: 'Sample',
    file: __dirname + "/users.htpasswd",
    algorithm:'md5'
});

basic.on('success', (result, req) => {
    console.log(`User authenticated: ${result.user}`);
    user = result.user;
});

basic.on('fail', (result, req) => {
    console.log(`User authentication failed: ${result.user}`);
    console.log(req.headers.authorization);
});

basic.on('error', (error, req) => {
    console.log(`Authentication error: ${error.code + " - " + error.message}`);
});

http.createServer(app).listen(8000);

app.use(auth.connect(basic));

app.get('/', function(req, res) {
    console.log(req.headers);
    console.log(basic);
    res.json('Hello from '+ user);
    res.end();
});

app.post('/', function(req, res) {
    console.log(req.headers);
    console.log(basic);
    res.json('Hello from '+ user);
    res.end();
});

这是 users.htpasswd 文件的内容:- ankit:示例:e4b2d19b03346a1c45ce86ad41b85c5e

每次我收到 401 时,使用 postman 调用带有用户名 ankit、pwd ankit 和领域示例的端点。

请告诉我哪里做错了。

谢谢

【问题讨论】:

    标签: node.js http-authentication


    【解决方案1】:

    您正在混合基本身份验证和摘要身份验证。将auth.basic 替换为auth.digest,您的代码应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多