我得到了错误Hapi unknown authentication strategy jwt,但是我不确定为什么。我确信我可能设置错了什么,但这是我的服务器index.js:
我应该使用不同的身份验证策略吗?另外,stackoverflow不让我提交我的问题,因为它主要是代码,但我不确定还能提交什么。所有的细节都在这里,我不知道还能添加什么来提供我能提供的更多信息。我只是将auth与数组中的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();发布于 2020-08-02 21:54:11
似乎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();
https://stackoverflow.com/questions/63138562
复制相似问题