nestjs swagger文档调用需要鉴权的接口

目标

nestjs经常需要设置一些鉴权(登录后)才能访问的接口,但是生成的swagger文档可以发起接口请求,文档发起的请求默认是不携带登录token的,所以需要移除swagger文档发起请求的守卫拦截。

nestjs守卫拦截设置见另一篇博客 nestjs守卫/全局守卫校验jwt-CSDN博客

方案一

关闭swagger文档请求守卫拦截

global.guard.ts

import { Injectable, NestInterceptor, ExecutionContext, HttpException, HttpStatus } from '@nestjs/common';

@Injectable()
export class GlobalGuard implements NestInterceptor {
  intercept(context: ExecutionContext, next): Observable<any> {
    const request = context.switchToHttp().getRequest();
    // 判断是否是swagger文档访问,如果请求头referer包含/api-docs,则认为是swagger文档访问
    // 文档前缀在main.ts设置,本项目文档前缀设置的api-docs
    const apiDocAccess = request.headers['referer'].indexOf('/api-docs') > -1;
    if (!apiDocAccess) {
      // 非文档访问、需要鉴权才能访问的接口,执行鉴权逻辑
      // ...
    }
    // 其他场景直接放行
    return next.handle();
  }
}

方案二

swagger文档添加鉴权请求头信息

接口添加 jwt 鉴权后,接口文档调用接口请求头没有添加 authorization,请求会返回403。为此,需要给文档需要鉴权的接口请求头也添加 authorization

main.ts 配置 BearerAuth 校验
const config = new DocumentBuilder()
  .setTitle('接口文档')
  .setDescription('接口文档描述')
  .setVersion('1.0')
  .addBearerAuth() // 注意此处:文档添加BearerAuth
  .build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document); // 文档前缀设为 api-docs
在需要鉴权的接口添加 @ApiBearerAuth() 装饰器
import { ApiBearerAuth } from '@nestjs/swagger';
 
@Controller('api')
@ApiBearerAuth() // 在此处添加,表示/api/的接口请求头全都需要添加authorization
export class ApiController {
 
  @Get('getUserInfo')
  @UseGuards(AuthGuard)
  @ApiBearerAuth() // 在此处添加,表示当前接口请求头需要添加authorization
  getUserInfo(): any {
    return this.apiService.getUserInfo();
  }
}
使用

先调用登录接口获取到 jwt_token

注意:可以设置默认请求参数,参数用一个swagger测试账号,就不用每次调用再改参数了。设置方法如下:

class LoginDto {
  @ApiProperty({description: '用户名', default: 'swagger-test'})
  name: string
  @ApiProperty({description: '密码', default: '123456'})
  password: string
}

点击文档顶部Authorize按钮

输入获取到的 jwt_token,并点击Authorize,然后关闭弹窗

再调用需要鉴权的接口,就可以鉴权通过了

注意:请求头的 Authorization 参数会在最前面添加 Bearer 字符,可以在守卫中将此字符移除

this.jwtService.verify(token.replace('Bearer ', ''))

相关推荐

  1. 基于SpringCloudGateway实现接口

    2023-12-25 12:46:04       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 12:46:04       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 12:46:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 12:46:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 12:46:04       18 阅读

热门阅读

  1. [网络安全] NTFS权限

    2023-12-25 12:46:04       42 阅读
  2. 《PCI Express体系结构导读》随记 —— 前言

    2023-12-25 12:46:04       32 阅读
  3. Mybatis使用详解

    2023-12-25 12:46:04       40 阅读
  4. Linux 文件管理命令----pwd 命令

    2023-12-25 12:46:04       38 阅读
  5. C/C++编译问题

    2023-12-25 12:46:04       25 阅读
  6. (c语言)素数的判断方法

    2023-12-25 12:46:04       45 阅读
  7. 力扣labuladong——一刷day79

    2023-12-25 12:46:04       36 阅读
  8. qt的血泪教训——地图存储与绘制

    2023-12-25 12:46:04       40 阅读
  9. Redis-实战案例

    2023-12-25 12:46:04       47 阅读
  10. sql server 增删改查(基本用法)

    2023-12-25 12:46:04       38 阅读
  11. 【AI】人工智能本地环境集成安装

    2023-12-25 12:46:04       33 阅读
  12. 5G RedCap:轻量5G技术的新宠

    2023-12-25 12:46:04       40 阅读