小编rin*_*rin的帖子

如何使用 @nestjs/swagger 和 @UseGuards 授权多个 api 密钥?

我正在使用 @UseGuards 来验证标头中的两个 api 密钥。

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
  // check two api keys('some' and 'thing') in header at once
}

Run Code Online (Sandbox Code Playgroud)

另外,我在控制器中使用 @ApiHeader 来大肆展示。

  @ApiOperation({ summary: 'blah blah' })
  @ApiHeader({ name: 'some'}, {name: 'thing'})
  @UseGuards(AuthGuard)
  @Get('/hello')
  async adminableCollections() {
    // do something
  }

Run Code Online (Sandbox Code Playgroud)

我想使用 @ApiSecurity 或其他东西代替 @ApiHeader 使用授权按钮(如图中)一次性授权,而不是为每个方法输入值。 swagger 授权按钮,可以授权我想要的所有方法

我尝试使用文档生成器添加自定义安全性,但它似乎根本不起作用。

  const swaggerConfig = new DocumentBuilder()
    .setTitle('My API')
    .setDescription('Document for my api.')
    .setVersion('0.0.1')
    .addApiKey('some', { type: 'apiKey', in: 'header', name: 'some' })
    .addApikey('thing', { type: …
Run Code Online (Sandbox Code Playgroud)

swagger typescript swagger-ui nestjs nestjs-swagger

2
推荐指数
1
解决办法
4430
查看次数

标签 统计

nestjs ×1

nestjs-swagger ×1

swagger ×1

swagger-ui ×1

typescript ×1