hapi-auth-cookie 正在保护所有路由,包括静态路由

hapi-auth-cookie is protecting all routes, including static

不对任何路由应用策略,hapi-auth-cookie 保护所有路由,包括静态路由。

server.register(require('hapi-auth-cookie'), function (err) {

    if (err) {
      logError(err);
    }

    server.auth.strategy('session', 'cookie', true, {
        password: 'things really long',
        clearInvalid: true,
        isSecure: false,
        validateFunc: function (request, session, callback) {

            cache.get(session.sid, (err, cached) => {

                if (err) {
                    return callback(err, false);
                }

                if (!cached) {
                    return callback(null, false);
                }

                return callback(null, true, cached.account);
            });
        }
    });
});

这是我的路线:

{
  method: 'POST',
  path: '/api/1/doSomething',
  config: {
    validate: {
      payload: someJoyObject
    },
    handler: function(request, reply) {
    stuff
    }
  }
}

{
    method: 'GET',
    path: '/{param*}',
    handler: {
        directory: {
            path: './public',
            listing: false,
            index: true
        }
    }
}

我无法加载我的应用程序的任何文件:

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

看看documentation for server.auth.strategy()。您将 true 作为第三个参数传递,这意味着 hapi 默认会将此策略应用于所有路由。

要为您的静态路由禁用它:

  1. 不要将其设置为所有路由的必需策略
  2. 在您的目录处理程序路由上明确禁用该策略:

例如:

{
    method: 'GET',
    path: '/{param*}',
    config: {
        auth: false
    },
    handler: {
        directory: {
            path: './public',
            listing: false,
            index: true
        }
    }
}