首页 文章

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

提问于
浏览
1

我使用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....

3 回答

  • 0

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

    像这样:

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

    @Andre:

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

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

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

  • 0

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

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

相关问题