【问题标题】:springdoc-openapi-ui add JWT header parameter to generated swaggerspringdoc-openapi-ui 将 header 参数添加到生成的 swagger
【发布时间】:2020-12-19 14:59:15
【问题描述】:

在我的 Spring Boot 应用程序中,我的端点由 SpringBoot 应用程序中的 header 参数验证。 当前的招摇 json 看起来像这样:

// part of current swagger.json
...
  "paths": {
    "/path1/{param1}": {
      "get": {
        "parameters": [
          {
            "name": "param1",
            "in": "path",
            "type": "string",
            "required": true
          }
        ]
      }
    }
  }
...

我想使用springdoc-openapi-ui 配置添加缺少的参数,所以它看起来像这样:

// I want to achieve swagger.json which contains additional parameter
...
  "paths": {
    "/path1/{param1}": {
      "get": {
        "parameters": [
          {
            "name": "param1",
            "in": "path",
            "type": "string",
            "required": true
          },
          {
            "name": "missingParam",
            "in": "header",
            "type": "string",
            "required": true
          }
        ]
      }
    }
  }
...

我尝试通过从Common Parameters for Various Paths 添加到我的appplication.yml 解决方案来实现这一目标

#application.yml
...
components:
  parameters:
    hiddenParam:
      in: header
      name: missingParam
      required: true
      schema:
        type: string
paths:
  /path1:
    get:
      parameters:
        - $ref: '#/components/parameters/hiddenParam'

但它不起作用。

我的问题:

  1. 有没有办法使用应用程序配置修改我的招摇结果?
  2. 我想定义参数模板并将其添加到所有端点,我该如何实现?

【问题讨论】:

    标签: spring-boot swagger springdoc-openapi-ui


    【解决方案1】:

    您可以使用OperationCustomizer 添加全局参数,如标题,如下所示。这会将您的 parameter 添加到每个服务中

    @Configuration
    public class SwaggerConfiguration {
    
        @Bean
        public OperationCustomizer customGlobalHeaders() {
    
            return (Operation operation, HandlerMethod handlerMethod) -> {
    
                Parameter missingParam1 = new Parameter()
                        .in(ParameterIn.HEADER.toString())
                        .schema(new StringSchema())
                        .name("missingParam1")
                        .description("header description2")
                        .required(true);
                        
                Parameter missingParam2 = new Parameter()
                        .in(ParameterIn.HEADER.toString())
                        .schema(new StringSchema())
                        .name("missingParam2")
                        .description("header description2")
                        .required(true);
    
                operation.addParametersItem(missingParam1);
                operation.addParametersItem(missingParam2);
    
                return operation;
            };
        }
    }
    

    【讨论】:

    • 感谢您的输入,这行得通。附加问题,如何从自定义中排除某些路径?事实证明,我有一个不应自定义的端点。
    • 它将适用于所有路径,据我所知,我们不能排除特定路径。您可以做的一个是定义参数并使用$ref 将其用于所有适用的路径
    • 我接受您的回答,因为这解决了我原来的问题。当我改变解决问题的方法时,我在最终解决方案中添加了另一个答案,谢谢。
    【解决方案2】:

    最后我决定使用不同的方法。 我定义了security scheme 并将其全局应用为authorization header

        @Bean
        public OpenAPI customOpenAPI() {
            return new OpenAPI()
                    .info(new Info().title("My App").version("1.0.0"))
                    // Components section defines Security Scheme "mySecretHeader"
                    .components(new Components()
                            .addSecuritySchemes("mySecretHeader", new SecurityScheme()
                                    .type(SecurityScheme.Type.APIKEY)
                                    .in(SecurityScheme.In.HEADER)
                                    .name("missingParam")))
                    // AddSecurityItem section applies created scheme globally
                    .addSecurityItem(new SecurityRequirement().addList("mySecretHeader"));
        }
    

    现在swagger-ui.html 允许根据测试人员的要求测试带有或不带有授权标头的端点。

    干杯!

    【讨论】:

    • 谢谢,这是一个很棒的解决方案,而且运行顺畅
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2022-10-25
    • 2021-06-03
    • 2021-03-03
    • 2020-06-22
    相关资源
    最近更新 更多