content: package jnpf;
  
  import io.swagger.annotations.ApiOperation;
  import jnpf.util.Constants;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import springfox.documentation.builders.ApiInfoBuilder;
  import springfox.documentation.builders.PathSelectors;
  import springfox.documentation.builders.RequestHandlerSelectors;
  import springfox.documentation.oas.annotations.EnableOpenApi;
  import springfox.documentation.service.*;
  import springfox.documentation.spi.DocumentationType;
  import springfox.documentation.spi.service.contexts.SecurityContext;
  import springfox.documentation.spring.web.plugins.Docket;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Swagger配置类
   *
   * @author JNPF开发平台组
   * @version V3.1.0
   * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
   * @date 2021-03-23
   */
  @Configuration
  @EnableOpenApi
  public class SwaggerConfig {
  
      //是否开启swagger
      @Value("${swagger.enable:false}")
      private Boolean isEnable;
  
      @Bean
      public Docket loginApi() {
          return new Docket(DocumentationType.OAS_30)
                  .enable(isEnable)
                  .apiInfo(apiInfo())
                  .pathMapping("/")
                  .select() // 选择那些路径和api会生成document
                  .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                  //不显示错误的接口地址
                  .build()
                  .securitySchemes(securitySchemes())
                  .securityContexts(securityContexts());
      }
  
      /**
       * 安全模式，这里指定token通过Authorization头请求头传递
       */
      private List<SecurityScheme> securitySchemes() {
          List<SecurityScheme> apiKeyList = new ArrayList<>();
          apiKeyList.add(new ApiKey(Constants.AUTHORIZATION, Constants.AUTHORIZATION, "header"));
          return apiKeyList;
      }
  
      /**
       * 授权信息全局应用
       */
      private List<SecurityContext> securityContexts() {
          List<SecurityContext> securityContexts = new ArrayList<>();
          securityContexts.add(SecurityContext.builder()
                  .securityReferences(defaultAuth())
                  .build());
          return securityContexts;
      }
  
      private List<SecurityReference> defaultAuth() {
          AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
          AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
          authorizationScopes[0] = authorizationScope;
          List<SecurityReference> securityReferences = new ArrayList<>();
          securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
          return securityReferences;
      }
  
      private ApiInfo apiInfo() {
          return new ApiInfoBuilder()
                  .title("接口文档")
                  //描述
                  .description("jnpf接口文档")
                  .version(Constants.SWAGGER_VERSION)
                  .build();
      }
  }
  