NestJs/Passport 身份验证不适用于异步传递的路由
NestJs/Passport authentication doesn't work for routes passed asynchronously
当我在 promise 中传递数据库中的选定路由时,授权不起作用。这意味着对传递路由的请求始终是授权的。
protected applyRoutes(consumer: MiddlewaresConsumer) {
let paths = this.authPathService.findAll();
paths.then((resultPaths) => {
let result: {}[] = [];
for (let path of resultPaths) {
result.push({
path: path.path,
method: RequestMethod.ALL
})
}
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes(...result);
return result;
}, (error) => {
console.log('error', error);
});
}
当我在对象数组中传递路由时效果很好
protected applyRoutes(consumer: MiddlewaresConsumer) {
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes(...[
{ path: '/auth/authorized', method: RequestMethod.ALL },
{ path: '/auth/test', method: RequestMethod.ALL }]);
}
无法使用 MiddlewaresConsumer
异步应用中间件。相反,注册一个将获取所有路径的异步组件(https://docs.nestjs.com/fundamentals/async-components),例如 AUTH_PATHS
,然后将其注入到您的模块 class,比方说 AuthModule
并使用它configure()
方法中的数组。
当我在 promise 中传递数据库中的选定路由时,授权不起作用。这意味着对传递路由的请求始终是授权的。
protected applyRoutes(consumer: MiddlewaresConsumer) {
let paths = this.authPathService.findAll();
paths.then((resultPaths) => {
let result: {}[] = [];
for (let path of resultPaths) {
result.push({
path: path.path,
method: RequestMethod.ALL
})
}
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes(...result);
return result;
}, (error) => {
console.log('error', error);
});
}
当我在对象数组中传递路由时效果很好
protected applyRoutes(consumer: MiddlewaresConsumer) {
consumer
.apply(passport.authenticate('jwt', { session: false }))
.forRoutes(...[
{ path: '/auth/authorized', method: RequestMethod.ALL },
{ path: '/auth/test', method: RequestMethod.ALL }]);
}
无法使用 MiddlewaresConsumer
异步应用中间件。相反,注册一个将获取所有路径的异步组件(https://docs.nestjs.com/fundamentals/async-components),例如 AUTH_PATHS
,然后将其注入到您的模块 class,比方说 AuthModule
并使用它configure()
方法中的数组。