NestJS全局身份验证全局保护与RabbitMQ订阅者冲突

9rbhqvlz  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(204)

我正在使用@golevelup/nestjs-rabbitmq来处理来自队列的RabbitMQ消息,全局防护正在拦截来自队列的请求。有什么方法可以阻止全局防护获取这些请求吗?
身份验证防护

@Injectable()
export default class GqlAuthGuard extends AuthGuard(AuthStrategy.Jwt) {
  constructor(private reflector: Reflector) {
    super();
  }

  getRequest(context: ExecutionContext) {
    const ctx = GqlExecutionContext.create(context);

    return ctx.getContext();
  }

  canActivate(context: ExecutionContext) {
    const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
      context.getHandler(),
      context.getClass(),
    ]);

    if (isPublic) {
      return true;
    }

    return super.canActivate(context);
  }
}

RabbitMQ订阅者

@SubscribeToEvent({
    queue: Topic.SendSurvey,
  })
  async handler({ surveyId, schedulingEventId }: HandlerParams) {
    this.logger.info('Handling message', surveyId, schedulingEventId);

    this.surveysService.dispatch(surveyId, schedulingEventId);
  }

我得到的错误

[22-03-24 02:52:31] [error] [app] TypeError: Cannot read properties of undefined (reading 'req') {"0":"MotivatoExceptionsHandler"}
[22-03-24 02:52:31] [error] [app] TypeError: Cannot read properties of undefined (reading 'req') {"0":"MotivatoExceptionsHandler"}
[22-03-24 02:52:31] [error] [app] TypeError: Cannot read properties of undefined (reading 'req') {"0":"MotivatoExceptionsHandler"}
[22-03-24 02:52:31] [error] [app] TypeError: Cannot read properties of undefined (reading 'req') {"0":"MotivatoExceptionsHandler"}

我尝试更改getRequest,但不起作用

z9zf31ra

z9zf31ra1#

async canActivate(context: ExecutionContext): Promise<boolean> {
  const type = context.getType<'rmq'>()
  if (type === 'rmq') {
    return true
  }
  ...
}

以这种方式添加时有效

相关问题