knife4j icon indicating copy to clipboard operation
knife4j copied to clipboard

整合springdoc之后 knifl4j.enable 增强模式无效

Open Lingouzi opened this issue 2 years ago • 1 comments

<dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>knife4j-springdoc-ui</artifactId>
<version>3.0.3</version>
    </dependency>

    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.10</version>
    </dependency>

然后yml配置

knife4j:
  # 开启增强配置
  enable: true
  # 开启Swagger的Basic认证功能,默认是false
  basic:
    enable: true
    # Basic认证用户名
    username: test
    # Basic认证密码
    password: 123

config:

public class SwaggerConfig {
  
  private static final String AUTH_KEY = "Authorization";
  
  @Resource
  private SpringDocProperties docProperties;
  
  /**
   * 分组的API文档
   *
   * @return
   */
  @Bean
  public GroupedOpenApi userApi() {
    String[] paths = {"/**"};
    String[] packagedToMatch = {docProperties.getBasePackage()};
    return GroupedOpenApi.builder()
        .group(docProperties.getGroupName())
        .pathsToMatch(paths)
        .addOperationCustomizer((operation, handlerMethod) -> operation.addParametersItem(
            new HeaderParameter().name(AUTH_KEY)
                .example("认证")
                .description("description Test code")
                .schema(new StringSchema()._default("BR")
                    .name(AUTH_KEY)
                    .description("Test code"))))
        .packagesToScan(packagedToMatch)
        .build();
  }
  
  /**
   * 基础的文档信息
   *
   * @return
   */
  @Bean
  public OpenAPI openAPI() {
    return new OpenAPI().info(new Info().title(docProperties.getTitle())
                .description(docProperties.getDescription())
                .version(docProperties.getVersion())
                .contact(new Contact().name(docProperties.getContactName())
                    .url(docProperties.getContactUrl())
                    .email(docProperties.getContactEmail()))
        )
        .addSecurityItem(new SecurityRequirement().addList(AUTH_KEY))
        .components(new Components().addSecuritySchemes(AUTH_KEY,
            new SecurityScheme().name(AUTH_KEY)
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")));
  }
}

然后访问,可以直接打开接口调试,无需登录

Lingouzi avatar Aug 22 '22 03:08 Lingouzi

对于Springdoc-openapi项目,目前Knife4j还没有整合增强功能,仅在springfox框架下做了增强整合,后面v4版本会把这块功能整合进去,关注knife4j 4.0迭代计划

xiaoymin avatar Aug 22 '22 05:08 xiaoymin