且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swagger 2.0 在哪里声明基本身份验证模式

更新时间:2023-12-01 13:42:58

使用 Springfox 2.6 注释,您必须首先在配置中设置 Docket 时将基本身份验证定义为安全方案之一,如下所示:

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)
                                     ...

然后您可以在您的服务中使用 Springfox 批注为您想要要求身份验证的操作设置基本身份验证:

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();