swagger Nestjs使用JWT Token进行授权,即使在传递token之后也会返回未经授权的令牌

tjjdgumg  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(600)

我使用JWT策略进行授权。我正在使用令牌来验证所有控制器中的用户。我已经成功地在Postman中获得了所需的响应,在Auth Bearer Token中设置了令牌。但是在swagger中设置令牌后,如下所示

我收到一个未经授权的回应。原因一定是,在swagger中,我的控制器无法访问令牌。下面是swagger文档的代码。

export function createDocument(app:INestApplication): OpenAPIObject{
const builder = new DocumentBuilder()
    .setTitle(SWAGGER_CONFIG.title)
    .setVersion(SWAGGER_CONFIG.version)
    .addBearerAuth({
        type:"http",
        scheme:'bearer',
        bearerFormat:'JWT',
    
        } ,'access-token')
                        
const options = builder.build();
return SwaggerModule.createDocument(app, options);

}
下面是一个控制器的例子

@ApiTags("Users")
@Controller('users')
export class UserController {
    @UseGuards(JwtAuthGuard)
    @Get('me')
    @ApiOkResponse({ description:"Successfully returned response"})
    @ApiForbiddenResponse({ description: "Forbidden" })
    async profile(@Req() request, @Res() response){
    some code}

下面是一个swagger路由

的示例

请让我知道我在哪里得到它错误的任何帮助将不胜感激

yizd12fk

yizd12fk1#

Because you did not send jwt token, it depends what backend framework you are using, I had this experience in NestJS where I put first @UseGuards than @Post, but I should `
@UseGuards()
@Post()

decorators are calling from bottom to top
@Decorator3
@Decorator2
@Decorator1
function() {
  //logic
}

相关问题