【问题标题】:Hapi unknown authentication strategy jwtHapi 未知认证策略 jwt
【发布时间】:2020-07-28 16:19:55
【问题描述】:

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

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

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();

【问题讨论】:

    标签: node.js jwt hapi


    【解决方案1】:

    authService.validateJWT好像有问题我已经添加了一个HapiJs jwt身份验证的例子,请你参考一下

    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();

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 2020-06-09
      • 2015-11-12
      • 2020-08-08
      • 2017-04-01
      • 2018-12-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      相关资源
      最近更新 更多