如何添加标头以基于注释生成摇摇欲坠?

时间:2019-01-25 13:29:00

标签: java spring-boot swagger-2.0 swagger-codegen springfox

考虑示例:

@RequestMapping("myPath")
public Mono<MyDto> myMethod(@RequestParam(value = "amount") Long amount, @AuthenticationPrincipal MyUser user) {

} 

要从参数列表中跳过MyUser,我已将@AuthenticationPrincipal添加到排除项和springfox生成的swagger文档中,如下所示:

"/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
  }
}

这是弹簧配置:

@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项目生成的庞大文档中? 例如。对于找到了MyUser的所有My-Auth-Header方法,将@RequestMapping类作为参数(或参数组)忽略,并用标头(例如@AuthenticationPrincipal)替换它。

1 个答案:

答案 0 :(得分:0)

这是一个示例,说明如何使用名为header的{​​{1}}参数来添加JWT令牌

Auithorization