swagger在线API文档配置

package com.sjaco.lccloud.provider.olsim.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
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.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.time.LocalDateTime;
import java.util.List;

import static cn.hutool.core.collection.CollUtil.newArrayList;

/**
 * swagger在线API文档配置
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    /*引入Knife4j提供的扩展类*/
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public SwaggerConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("JWT", "Authorization", "header"));
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("lc").description("©2020 Copyright. Powered By sjaco")
                .version("1.0").contact(new Contact("孙爽", "", "xiaoshitou1s@qq.com"))
                .build();
    }

//    private ApiInfo apiInfo(String title,String desc, String version) {
//        return new ApiInfoBuilder().title(title).description(desc)
//                .version(version).contact(contact).build();
//    }


    @Bean(value = "faceIdApi")
    public Docket faceIdApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .directModelSubstitute(LocalDateTime.class,String.class)
                .apiInfo(apiInfo())
                .groupName("lc:人脸识别接口")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/trilateral/faceid/**")
//                        .or(PathSelectors.ant("/api/customersss/**"))
                )
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("lc:人脸识别接口"))
                .securityContexts(Lists.newArrayList(securityContext(),securityContext1()))
                .securitySchemes(securitySchemes());
    }

    @Bean(value = "appApi")
    public Docket appApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .directModelSubstitute(LocalDateTime.class,String.class)
                .apiInfo(apiInfo())
                .groupName("lc:App接口")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/imapi/im/**")
//                        .or(PathSelectors.ant("/api/customersss/**"))
                )
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("lc:App接口"))
                .securityContexts(Lists.newArrayList(securityContext(),securityContext1()))
                .securitySchemes(securitySchemes());
    }

    @Bean(value = "appVendorApi")
    public Docket appVendorApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .directModelSubstitute(LocalDateTime.class,String.class)
                .apiInfo(apiInfo())
                .groupName("lc:App接口(商家)")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/appVendor/**")
//                        .or(PathSelectors.ant("/api/customersss/**"))
                )
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("lc:App接口(商家)"))
                .securityContexts(Lists.newArrayList(securityContext(),securityContext1()))
                .securitySchemes(securitySchemes());
    }

    @Bean(value = "authApi")
    public Docket authApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .directModelSubstitute(LocalDateTime.class,String.class)
                .apiInfo(apiInfo())
                .groupName("lc:授权和基础信息接口")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/sys/**")
                        .or(PathSelectors.ant("/api/region/**"))
                        .or(PathSelectors.ant("/api/oss/**"))
                        .or(PathSelectors.ant("/api/test/**"))
                )
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("lc:授权和基础信息接口"))
                .securityContexts(Lists.newArrayList(securityContext(),securityContext1()))
                .securitySchemes(securitySchemes());
    }

    @Bean(value = "logApi")
    public Docket logApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .directModelSubstitute(LocalDateTime.class,String.class)
                .apiInfo(apiInfo())
                .groupName("lc:日志服务接口")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/log/**")
                )
                .build()
                .extensions(openApiExtensionResolver.buildExtensions("lc:日志服务接口"))
                .securityContexts(Lists.newArrayList(securityContext(),securityContext1()))
                .securitySchemes(securitySchemes());
    }



    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
//                .forPaths(PathSelectors.regex("/.*"))
                .forPaths(PathSelectors.regex("^((?!auth).)*$"))  //包含 auth的接口不要求 token
                .build();
    }
    private SecurityContext securityContext1() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth1())
//                .forPaths(PathSelectors.regex("/.*"))
                .forPaths(PathSelectors.regex("^((?!auth).)*$"))
                .build();
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("JWT", authorizationScopes));
    }
    List<SecurityReference> defaultAuth1() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("JWT", authorizationScopes));
    }

}

 

posted @ 2022-02-10 15:02  二次元的程序猿  阅读(217)  评论(0编辑  收藏  举报