首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hapi未知认证策略jwt

Hapi未知认证策略jwt
EN

Stack Overflow用户
提问于 2020-07-29 00:19:55
回答 1查看 250关注 0票数 0

我得到了错误Hapi unknown authentication strategy jwt,但是我不确定为什么。我确信我可能设置错了什么,但这是我的服务器index.js:

我应该使用不同的身份验证策略吗?另外,stackoverflow不让我提交我的问题,因为它主要是代码,但我不确定还能提交什么。所有的细节都在这里,我不知道还能添加什么来提供我能提供的更多信息。我只是将auth与数组中的config.auth.strategy和jwt一起使用。

代码语言:javascript
复制
const Hapi = require('@hapi/hapi');
const objection = require('objection');
const knex = require('./knex');
const authService = require('./auth/auth-service');
const JWTAuth = require('hapi-auth-jwt2');

const init = async () => {

    const server = Hapi.server({
        port: 9000,
        host: 'localhost',
        routes: { cors: {
            origin: ['*'],
            headers: ['Authorization'], 
            exposedHeaders: ['Accept'], 
            additionalExposedHeaders: ['Accept'], 
            maxAge: 60,
            credentials: true 
        }}
    });
    objection.Model.knex(require('./knex'));

    await server.register([
            {plugin: JWTAuth},
            {
                plugin: require('./movies/movie-routes'),
                routes: {prefix: '/movies'}
            }, {
                plugin: require('./user/user-routes'),
            }
        ])

    server.auth.strategy('jwt', 'jwt',{
            key: authService.jwtKey,
            validate: authService.validateJWT,
            verifyOptions: {algorithms: ['HS256']}, 
            errorFunc: (err)=> {return err},
            cookieKey: 'id_token'
        })
    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

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

init();
EN

回答 1

Stack Overflow用户

发布于 2020-08-02 21:54:11

似乎authService.validateJWT有问题,我已经添加了一个HapiJs jwt身份验证的示例,请只看一次供您参考

代码语言:javascript
复制
const Hapi = require('@hapi/hapi');
const JWTAuth = require('hapi-auth-jwt2');

const init = async () => {

    const server = Hapi.server({
        port: 9000,
        host: 'localhost',
        routes: {
            cors: {
                origin: ['*'],
                headers: ['Authorization'],
                exposedHeaders: ['Accept'],
                additionalExposedHeaders: ['Accept'],
                maxAge: 60,
                credentials: true
            }
        }
    });

    await server.register([
        { plugin: JWTAuth }
    ])
    server.auth.strategy('jwt', 'jwt', {
        key: 'your-key',
        validate: async function (decoded, request) {
            if (!decoded) {
                return { isValid: false };
            } else {
                
                request.auth.credentials = {
                    'user': decoded,
                    'token': request.headers['authorization']
                };
                return { isValid: true };
            }
        },
        verifyOptions: { algorithms: ['HS256'] },
        cookieKey: 'id_token'
    });
    
    server.auth.default('jwt');
    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });

    
    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

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

init();

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

https://stackoverflow.com/questions/63138562

复制
相关文章

相似问题

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