【问题标题】:Missing authentication with status code 401 , in Hapijs?在 Hapijs 中缺少状态码 401 的身份验证?
【发布时间】:2017-08-14 05:02:43
【问题描述】:

我是 hapijs 的新手,我正在做一个使用 hapi-auth-basic 进行身份验证的演示应用程序。 但我的代码, (server.auth.strategy('simple', 'basic', { validateFunc: validate});) validate 方法没有被调用,结果总是给未授权的如下:

{
  "statusCode": 401,
  "error": "Unauthorized",
  "message": "Missing authentication"
}

我想授权,谁能建议我该怎么做以及我做错了什么?

'use strict';

const Bcrypt = require('bcrypt');
const Hapi = require('hapi');
const Basic = require('hapi-auth-basic');

const server = new Hapi.Server();
server.connection({ port: 3000 });

const users = {
    john: {
        username: 'john',
        password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',   // 'secret'
        name: 'John Doe',
        id: '2133d32a'
    }
};

const validate = function (request, username, password, callback) {
    const user = 'john';
    console.log("1");
    if (!user) {
        return callback(null, false);
    }

    Bcrypt.compare(12345, 12345, (err, isValid) => {
        console.log("2");
        callback(err, isValid, { id: user.id, name: user.name });
    });
};

server.register(Basic, (err) => {
console.log("3");
    if (err) {
        throw err;
    }

    server.auth.strategy('simple', 'basic', { validateFunc: validate});
    server.route({
        method: 'GET',
        path: '/',
        config: {
            auth: 'simple',
            handler: function (request, reply) {
                console.log("4");
                reply('hello, ' + request.auth.credentials.name);
            }
        }
    });

    server.start((err) => {

        if (err) {
            throw err;
        }

        console.log('server running at: ' + server.info.uri);
    });
});

【问题讨论】:

  • 你能解决这个问题吗...??

标签: node.js hapijs


【解决方案1】:

您应该在 Authorization 标头中发送您的身份验证数据。下面的 curl 示例,由 Postman 生成。

curl -X GET \
  http://localhost:8080 \
  -H 'Authorization: Basic am9objokMmEkMTAkaXFKU0hELkJHcjBFMkl4UXdZZ0ptZVAzTnZoUHJYQWVMU2FHQ2o2SVIvWFU1UXRqVnU1VG0=' \
  -H 'cache-control: no-cache'

【讨论】:

    【解决方案2】:

    在您的 validate func 中,Bcrypt 必须响应错误或 isValid 为 false,您必须测试这些返回条件,而不是盲目地从函数返回结果。也总是在回调之前使用 return 来显示控制流正在离开函数。使用 hapi 的样式指南之类的东西来帮助更早地发现错误。

    【讨论】:

      【解决方案3】:

      您应该创建一个方案,然后像这样注册一个策略:

          server.auth.scheme('custom', function (server, options) {
          return {
              authenticate: function (request, reply) {
                  const req = request.raw.req;
                  const authorization = req.headers.authorization;
                  if (!authorization) {
                      return reply(Boom.unauthorized(null, 'Custom'));
                  }
                  return reply.continue({ credentials: { user: 'john' } });
              }
          };
      };
      
       server.auth.strategy('default', 'custom');
      

      【讨论】:

      • 这不是回答操作问题,他正在使用模块 hapi-auth-basic
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2021-09-17
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多