swagger core 2.0禁用端点

时间:2018-06-01 09:40:50

标签: java openapi

我使用Swagger Core 2.0生成openAPI 3.0定义文件和
我无法禁用"安全"对于特定端点。 我定义了securitySchemes和root安全元素:

{
  "openapi" : "3.0.1",
  "security" : [ {
    "JWT" : [ ]
  } ],
  "paths" : {   
    "/auth" : {
      "post" : {
        "summary" : "authenticate user",
        "operationId" : "authenticate",
        "requestBody" : {
          "content" : {
            "application/json" : {
              "schema" : {
                "$ref" : "#/components/schemas/AuthenticationRequest"
              }
            }
          }
        },
        "responses" : {
          "200" : {
            "description" : "when user is successfully authenticated",
            "content" : {
              "application/json" : {
                "schema" : {
                  "$ref" : "#/components/schemas/AuthenticateUserOutput"
                }
              }
            }
          },       
          "401" : {
            "description" : "when email/password not valid or user is blocked/inactive"
          }       
        }
      }
    },
  },
  "components" : {
    "schemas" : {
      "AuthenticateUserOutput" : {
        "type" : "object",
        "properties" : {
          "token" : {
            "type" : "string"
          },
          "lastLoginAt" : {
            "type" : "string",
            "format" : "date-time"
          },        
          "lastProjectId" : {
            "type" : "string"
          }
        }
      },
      ...,
      "AuthenticationRequest" : {
        "required" : [ "email", "password" ],
        "type" : "object",
        "properties" : {
          "email" : {
            "type" : "string"
          },
          "password" : {
            "type" : "string"
          }
        }
      }
    },
    "securitySchemes" : {
      "JWT" : {
        "type" : "http",
        "scheme" : "bearer",
        "bearerFormat" : "JWT"
      }
    }
  }
}

根据OPEN API 3规范https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#securityRequirementObject我将能够覆盖全局"安全要求&# 34;用于个人操作。我想"禁用"一些操作的JWT安全性,根据https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#securityRequirementObject可以完成:

  

要删除顶级安全声明,可以使用空数组。

不幸的是,我正在努力定义"空安全数组"在操作级别上使用注释... 我试着指定

security = {}

security = @SecurityRequirement(name ="")

但根本没有生成操作中的安全元素.... 任何的想法 ?

下面是我的java代码(我用于swagger dropwizard集成),它允许一个人定义SecurityScheme和根级安全性

 Info info = new Info()
            .title("someTitle")
            .description("some description")
            .version("1.0")

    SecurityScheme jwtSecurity = new SecurityScheme()
            .type(SecurityScheme.Type.HTTP)
            .name("Authorization")
            .in(SecurityScheme.In.HEADER)
            .scheme("bearer")
            .bearerFormat("JWT");

    String securitySchemaName = "JWT";
    OpenAPI oas = new OpenAPI()
            .info(info)
            .components(new Components().addSecuritySchemes(securitySchemaName, jwtSecurity))
            .addSecurityItem(new SecurityRequirement().addList(securitySchemaName));

    SwaggerConfiguration oasConfig = new SwaggerConfiguration()
            .openAPI(oas)
            .prettyPrint(true)
            .resourcePackages(Stream.of("my.resources.package")
                    .collect(Collectors.toSet()));
    environment.jersey().register(new OpenApiResource()
            .openApiConfiguration(oasConfig));

然后在一些专用端点上我想禁用安全性,所以我正在尝试:

    @POST
@Operation(
        summary = "authenticate user",
        responses = {
                @ApiResponse(responseCode = "200", description = "when user is successfully authenticated",
                        content = @Content(schema = @Schema(implementation = AuthenticateUserOutput.class))),                   
                @ApiResponse(responseCode = "401", description = "when email/password not valid or user is blocked/inactive"),
        }
        ,security = what to put here ?
)

2 个答案:

答案 0 :(得分:0)

根据对OpenAPI-Specifiction GitHub项目的评论。应该有可能。

您尝试过吗?

security: [
  {}
]

答案 1 :(得分:-1)

我在 Java SpringBoot webapp 上遇到了同样的问题(依赖 org.springdoc:springdoc-openapi-ui:1.5.2)。根据 this answer,我解决了在操作上添加一个空的 @SecurityRequirements 注释的问题。例如:

@POST
@SecurityRequirements
@Operation(
    summary = "authenticate user",
    responses = {
    @ApiResponse(responseCode = "200", description = "when user is successfully authenticated",
                 content = @Content(schema = @Schema(implementation = AuthenticateUserOutput.class))),                   
        @ApiResponse(responseCode = "401", description = "when email/password not valid or user is blocked/inactive"),
    } )
)