springdoc-swagger3,Authorization token授权头添加不生效问题
swagger3按照 swagger2的格式,添加授权请求头###但我们接口如果需要这么一个权限,使用的也是Authorization,该怎么办呢?继续查看文档,提供了一个解决方案:添加全局请求头信息只是这样还不够,还需要在每个接口上添加 Authorization 授权放行security 参数,指定要放行的权限key打开swagger:点击授权登陆,填写 token参数...
·
swagger3按照 swagger2的格式,添加授权请求头
@Operation(summary = "修改分类")
@Parameters({
@Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "认证token", example = "Bearer "),
})
@PostMapping("/update")
public ApiResultBean updateMenu(@RequestBody XXXX dto) {
return ApiResultBean.successResult();
}
使用swagger文档,填写 token 参数,发送请求,发现获取到的token值为null,断点发现,HttpServletRequest中并无key为Authorization的请求头参数,what?? 明明swagger2可以这么用的
打开springdoc官方文档,看到这么一段话:
The OpenAPI 3 specification does not allow explicitly adding Authorization header. Note: Header parameters named Accept, Content-Type and Authorization are not allowed. To describe these headers
大意就是不支持显示添加请求头授权信息 Authorization ,即便使用 @Parameters 添加了 Authorization ,openapi3服务也会删除掉他
![不支持显示授权请求头](https://img-blog.csdnimg.cn/71e422fca79046cdab8670ea8fd68971.png)
###但我们接口如果需要这么一个权限,使用的也是Authorization,该怎么办呢?
继续查看文档,提供了一个解决方案:添加全局请求头信息
只是这样还不够,还需要在每个接口上添加 Authorization 授权放行 security 参数,指定要放行的权限key
@Operation(summary = "获取方案列表", security = { @SecurityRequirement(name = "Authorization") })
@GetMapping("/schemes")
public ApiResultBean schemes() {
// AuthUserEntity authUser = AuthUtil.getAuthUser();
return ApiResultBean.successResult(getSchemeListDto());
}
打开swagger:点击授权登陆,填写 token参数
更多推荐
已为社区贡献1条内容
所有评论(0)