Spring Boot Swagger注释,用于支持AWS API网关上的API键



我正在尝试设置一个AWS API网关,该网关需要任何请求的标题中的API键。我设置了Swagger以生成JSON定义,并将其导入完美。但是,当我尝试添加API密钥安全性定义时,Amazon拒绝了我的Swagger API DOC,并使用此错误:

Your API was not imported due to errors in the Swagger file. API Key security definition 'api_key' has unexpected name or location. Ignoring.

我假设我做错了...所以我尝试了这些行的几种变体。

给定使用SpringFox创建的Spring Boot配置:

 @Bean
    public Docket swaggerSpringMvcPlugin() {
    List<ApiKey> securitySchemes = new ArrayList<>();
    ApiKey apiKey = new ApiKey("apiKey", "api_key", "header");
    list.add(apiKey);
    //the above is probably where i'm missing something
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .securitySchemes(securitySchemes)
                .select()
                .paths(PathSelectors.regex("/api/swaggertest"))
                .build();
    }

和上述API像这样定义:

@RestController
public class SwaggerTestController {
      @ApiOperation(notes = "my notes", value = "test", nickname = "testNickname",
                tags = {"tests"}, authorizations = {@Authorization(value="api_key")} )
        @ApiResponses({
                @ApiResponse(code = 200, message = "Nice!", response = Swag.class)
        })
        @RequestMapping(value = "/api/swaggertest", method = RequestMethod.GET, produces = "application/json")
        public ResponseEntity<Swag> getSwag() throws Exception {

                return ResponseEntity.ok().body(new Swag());
        }
    }

运行应用程序时,API-DOCS会产生(我删除一些部分以保持较小(:

{
swagger: "2.0",
host: "localhost:4014",
basePath: "/",
tags: [
{
    name: "swagger-test-controller",
    description: "Swagger Test Controller"
}
],
paths: {
    /api/swaggertest: {
get: {
    tags: [
    "tests"
],
summary: "test",
description: "my notes",
operationId: "testNickname",
consumes: [
    "application/json"
],
produces: [
    "application/json"
],
responses: {
    200: {
    description: "Nice!",
    schema: {
        $ref: "#/definitions/Swag"
    }
    }
   },
    security: [
    {
    api_key: [ ]
    }
    ]
    }
   }
},
securityDefinitions: {
    api_key: {
    type: "apiKey",
    name: "api_key",
    in: "header"
}
},
definitions: {
    Swag: {
    type: "object",
    properties: {
    foo: {
        type: "string"
}

} }}

如果有人可以帮助我指向如何添加API密钥支持,我将不胜感激。

安全定义应该看起来像这样,在您的方法安全性中引用 <any_name>

"<any_name>" : {
  "type" : "apiKey",
  "name" : "x-api-key",
  "in" : "header"
},

最新更新