Swagger 2.0 where to declare Basic Auth Schema

前端 未结 3 2240
自闭症患者
自闭症患者 2021-02-18 16:48

How do I define basic authentication using Swagger 2.0 annotations and have it display in swagger UI.

In the resource I have:

@ApiOperation(value = \"Re         


        
相关标签:
3条回答
  • 2021-02-18 17:28

    I struggeled with this as well. In my case i used the swagger-maven-plugin. To solve this i added this within the maven plugin:

    <securityDefinitions>
      <securityDefinition>
        <name>basicAuth</name>
        <type>basic</type>
      </securityDefinition>
    </securityDefinitions>
    

    After that i was able to add it on my resource like this:

    @Api(value = "My REST Interface", authorizations = {@Authorization(value="basicAuth")})
    

    The generated json included the security element for each endpoint:

    "security":[{
      "basicAuth" : []
     }]
    

    And the security definition:

      "securityDefinitions" : {
        "basicAuth" : {
          "type" : "basic"
        }
      }
    

    I hope this helps others as well.

    0 讨论(0)
  • 2021-02-18 17:31

    You can use the @SwaggerDefinition http://swagger.io/customizing-your-auto-generated-swagger-definitions-in-1-5-x/

    or you can configure the swagger object directly, here's an example http://www.programcreek.com/java-api-examples/index.php?source_dir=rakam-master/rakam/src/main/java/org/rakam/WebServiceRecipe.java

    0 讨论(0)
  • 2021-02-18 17:32

    Using Springfox 2.6 annotations, you must first define Basic authentication as one of the security schemes when you set up the Docket in your configuration, like this:

    List<SecurityScheme> schemeList = new ArrayList<>();
    schemeList.add(new BasicAuth("basicAuth"));
    
    return new 
      Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo)
                                         .securitySchemes(schemeList)
                                         ...
    

    Then you can use the Springfox annotations in your service to set Basic Auth for the operation for which you want to require authentication:

    @ApiOperation(value = "Return list of categories", response=Category.class, responseContainer="List", httpMethod="GET", authorizations = {@Authorization(value="basicAuth")})
    public Response getCategories();
    
    0 讨论(0)
提交回复
热议问题