首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何为基于注解的swagger生成添加头部?

如何为基于注解的swagger生成添加头部?
EN

Stack Overflow用户
提问于 2019-01-25 21:29:00
回答 1查看 2.2K关注 0票数 0

考虑以下示例:

代码语言:javascript
代码运行次数:0
运行
复制
@RequestMapping("myPath")
public Mono<MyDto> myMethod(@RequestParam(value = "amount") Long amount, @AuthenticationPrincipal MyUser user) {

} 

为了从参数列表中跳过MyUser,我将@AuthenticationPrincipal添加到exclusions和springfox生成的swagger文档中,如下所示:

代码语言:javascript
代码运行次数:0
运行
复制
"/myPath": {
  "get": {
    "tags": [
      "my-controller"
    ],
    "summary": "myMethod",
    "operationId": "myMethodUsingGET",
    "produces": [
      "*/*"
    ],
    "parameters": [
      {
        "name": "amount",
        "in": "query",
        "description": "amount",
        "required": true,
        "type": "integer",
        "format": "int64"
      }
    ],
    "responses": {
      "200": {
        "description": "OK",
        "schema": {
          "$ref": "#/definitions/Mono«MyDto»"
        }
      },
      "401": {
        "description": "Unauthorized"
      },
      "403": {
        "description": "Forbidden"
      },
      "404": {
        "description": "Not Found"
      }
    },
    "deprecated": false
  }
}

下面是spring的配置:

代码语言:javascript
代码运行次数:0
运行
复制
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
    @Bean
    public Docket api() {
        Class[] clazz = {AuthenticationPrincipal.class};

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(clazz);
    }
}

有没有办法为指定了@AuthenticationPrincipal的每个路径在springfox项目生成的swagger文档中添加一个头文件?例如,忽略MyUser类作为参数(或参数组),并将其替换为所有找到@AuthenticationPrincipal@RequestMapping方法的头(如My-Auth-Header)。

EN

回答 1

Stack Overflow用户

发布于 2019-01-25 21:40:16

这是一个如何使用名为Auithorizationheader参数添加JWT令牌的示例

代码语言:javascript
代码运行次数:0
运行
复制
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(
                        Collections.singletonList(new ParameterBuilder()
                                .name("Authorization")
                                .description("JWT Authorization token")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header")
                                .required(false)
                                .build()));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54366267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档