首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hapi-auth-bearer-token仅适用于查询字符串中的access_token,不能用作标头

hapi-auth-bearer-token仅适用于查询字符串中的access_token,不能用作标头
EN

Stack Overflow用户
提问于 2019-11-07 04:36:50
回答 1查看 370关注 0票数 0

如何使用access_token作为http头,而不是在查询字符串中传递它,让hapi auth-bearer token工作?文档很清楚,这应该是可行的,但正如您在下面的屏幕截图中所看到的,它不是这样的。

代码语言:javascript
复制
const Hapi = require('hapi');
const AuthBearer = require('hapi-auth-bearer-token');

const server = Hapi.server({ port: 8080 });

const start = async () => {

    await server.register(AuthBearer)

    server.auth.strategy('simple', 'bearer-access-token', {
        allowQueryToken: true,              // optional, false by default
        validate: async (request, token, h) => {

            // here is where you validate your token
            // comparing with token from your database for example
            const isValid = token === '1234';

            const credentials = { token };
            const artifacts = { test: 'info' };

            return { isValid, credentials, artifacts };
        }
    });

    server.auth.default('simple');

    server.route({
        method: 'GET',
        path: '/',
        handler: async function (request, h) {

            return { info: 'success!' };
        }
    });

    await server.start();

    return server;
}

start()
    .then((server) => console.log(`Server listening on ${server.info.uri}`))
    .catch(err => {

        console.error(err);
        process.exit(1);
    })

查询字符串access_token的工作原理:

Header access_token不:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-12 00:41:37

hapi-auth-bearer-token需要一个持有者令牌,这意味着该值需要为Bearer 1234,而不仅仅是1234。在我的例子中,我不得不在不使用Bearer这个词的情况下使用它,所以我深入研究了插件源代码,并推出了自己的实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58738119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档