描述
nest中配置swagger后, swaggerUI中没有能够设置请求头的地方 (token需要通过header传递)
解决
main.ts 启用swagger的位置加上 .addBearerAuth()
const config = new DocumentBuilder().setTitle('My API').setDescription('My API').setVersion('1.0').addBearerAuth().build();const document = SwaggerModule.createDocument(app, config);SwaggerModule.setup('api/doc', app, document, {customSiteTitle: 'YNote API',});
controller控制器上加上 @ApiBearerAuth()
@ApiTags('test')
@ApiBearerAuth()
@Controller({path: 'test',
})
export class TestController {}
只要添加了@ApiBearerAuth()
的控制器接口, swaggerUI上请求时才会在header中加上authorizations