Skip to content

守卫

微服务守卫与常规 HTTP 应用程序守卫之间没有根本区别。唯一的区别是,你应该使用 RpcException 而不是抛出 HttpException

提示

RpcException 类从 @nestjs/microservices 包中导出。

绑定守卫

以下示例使用了方法作用域的守卫。与基于 HTTP 的应用程序一样,你也可以使用控制器作用域的守卫(即在控制器类前加上 @UseGuards() 装饰器)。

typescript
@UseGuards(AuthGuard)
@MessagePattern({ cmd: 'sum' })
accumulate(data: number[]): number {
  return (data || []).reduce((a, b) => a + b);
}

基于 NestJS 官方文档翻译