NestJS 全局身份验证全局守卫与 RabbitMQ 订阅者发生冲突

发布于 2025-01-16 19:15:07 字数 1624 浏览 0 评论 0原文

我正在使用 @golevelup/nestjs-rabbitmq 来处理来自队列的 RabbitMQ 消息,全局防护正在拦截来自队列的请求。有什么办法可以阻止全局警卫收到这些请求吗?

Auth Guard

@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 Subscriber

@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 但它不起作用

I'm using @golevelup/nestjs-rabbitmq to handle RabbitMQ messages from a queue, the global guard is intercepting the requests from the queue. Is there any way to prevent the global guard to get these requests?

Auth guard

@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 Subscriber

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

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

The error I'm getting

[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"}

I tried changing the getRequest but it didn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2025-01-23 19:15:07
async canActivate(context: ExecutionContext): Promise<boolean> {
  const type = context.getType<'rmq'>()
  if (type === 'rmq') {
    return true
  }
  ...
}

以这种方式添加时有效

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

It works when adding in this way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文