繁体   English   中英

从java Annotations生成swagger docus,缺少安全定义

[英]Generate swagger docus from java Annotations, missing security definitions

我使用swagger maven插件在构建时生成swagger文档。

这适用于基本的@SwaggerDefinition注释。 但是最终的json和yaml文件中没有生成子部分securityDefinition

我在3.1.4版本中使用了swagger-maven-plugin

可能遗漏的任何想法?

@SwaggerDefinition(
        info = @Info(
                description = "Interact with example",
                version = "V1.1.0",
                title = "The example API",
                termsOfService = "http://example.com",
                contact = @Contact(
                   name = "André Schild", 
                   email = "a.schild@aarboard.ch", 
                   url = "http://example.com"
                ),
                license = @License(
                    name = "example License",
                    url = "http://example.com/"
                )
        ),
        host = "api.example.com",
        basePath = "/api/v1",
        consumes = {"application/json"},
        produces = {"application/json"},
        schemes = {SwaggerDefinition.Scheme.HTTPS},
        securityDefinition = @SecurityDefinition(
                basicAuthDefinions = {
                        @BasicAuthDefinition(key = "basicAuth")},
                apiKeyAuthDefintions = {
                        @ApiKeyAuthDefinition(key = "exampleAuth", name = "apiKey", in = ApiKeyLocation.HEADER)}),
          tags = {
                @Tag(name = "API", description = "Api for example"),
                @Tag(name = "V1", description = "V1 Api for example")
        }, 
        externalDocs = @ExternalDocs(
                value = "example",
                url = "http://example.com"
        )
)

这是最终的swagger文件的样子:

---
swagger: "2.0"
info:
  description: "Interact with example"
  version: "V1.1.0"
  title: "The example API"
  termsOfService: "http://example.com"
  contact:
    name: "André Schild"
    url: "http://example.com"
    email: "a.schild@aarboard.ch"
  license:
    name: "example License"
    url: "http://example.com/"
host: "api.example.com"
basePath: "/api/v1"
tags:
- name: "categories"
  description: "Operations about categories"
paths:
  /categories:
    get: 
.... and more paths/definitions....

我为我工作的方式是创建一个单独的类/接口,并使用安全定义用@SwaggerDefinition注释它。 它为所有API提供了安全性定义。

像这样:

@SwaggerDefinition(securityDefinition = @SecurityDefinition(apiKeyAuthDefinitions = { @ApiKeyAuthDefinition(key = "ApiKey", name = "Authorization", in = ApiKeyLocation.HEADER) }))
public interface SwaggerSecurityDefinition {

}

@Andre:

要获得ritesh工作的解决方案,您需要在资源类中包含安全性定义:

 @Api(value = "/example", description = "", authorizations = { @Authorization( value = "ApiKey" ) }) public class ExapleResource { ... } 

或者你可以使用swagger bean来全局配置定义。 swagger-examples中可以找到一个很好的例子

如果使用swagger-maven-plugin,请配置它( https://github.com/kongchen/swagger-maven-plugin ):

<securityDefinition>
    <name>MybasicAuth</name>
    <type>basic</type>
</securityDefinition>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM