package cn.fc.swagger2; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.ApiSelectorBuilder; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * API page /swagger-ui.html */ @configuration @enablesWagger2 public class SwaggerConfig {@value ("${jwt.header}")
    private String tokenHeader;

    @Value("${swagger.enabled}")
    private Boolean enabled;

    @Value("${swagger.host}")
    private String host;

    private static final String SPLITOR = ",";

    @Bean
    public Docket createRestApi() {
        returnNew Docket (DocumentationType SWAGGER_2). The enable (enabled). ApiInfo (apiInfo ()) / / stitching path, this is equivalent to swagger test interface, //. PathMapping () {//. PathMapping () {//."fcadmin"// The test interface must be set to localhost by default. If the test interface is not set to localhost by default, the test interface must be set to localhost by default. Arises out of the path of this project is http://fcadmin/fcadmin/api/device. The host (host) / / choose which path and the API will generate the document. The select () / / specified scan only which packages the generated API interface document below, Repeated write can lead to a scan/can't /. Apis (RequestHandlerSelectors. BasePackage ("cn.fc.modules.bus.device.rest"))
//                .apis(RequestHandlerSelectors.basePackage("cn.fc.modules.system.rest")) / / call the rewriting method, support scan multiple packages. The apis (basePackage ("cn.fc.modules.bus" + SPLITOR + "cn.fc.modules.system.rest"))
                .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build() .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } /** * Overwrite the basePackage method to make Swagger support multiple packet scans * @param basePackage * @return
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true); } private static Function<Class<? >, Boolean> handlerPackage(final String basePackage) {returnInput -> {// loop to determine the matchfor (String strPackage : basePackage.split(SPLITOR)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true; }}return false; }; } private static Optional<? extends Class<? >> declaringClass(RequestHandler input) {returnOptional.fromNullable(input.declaringClass()); } /** * Enter SwaggerUI after setting, and the "Authorization" button appears in the upper right corner. Click to enter the authentication parameters we configured. * For interfaces that do not require input parameters (including the auth interface described above), they can be accessed without input Authorization parameters. * Other interfaces will return a 401 error. Click the Authorization button in the upper right corner and enter the configured parameters to pass authentication. You do not need to enter parameters for each interface. * Configure global parameters for securitySchemes of Swagger2: Add Authorization to the ApiKey of securitySchemes, as shown in the following codetypeIs the "header" parameter. * @return
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("Authorization"."Authorization"."header"));
        returnapiKeyList; } /** * Set interfaces that require parameters (or to remove interfaces that do not need authentication parameters) in SecurityContext Swagger2, * as shown in PathSelectors."^ (? ! auth).*$"), all contain"auth"You don't need securitySchemes for your interface. * That is, Authorization as set above is not required.typeThe authentication parameter is Header. */ private List<SecurityContext>securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^ (? ! auth).*$"))
                .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global"."accessEverything");
        AuthorizationScope[] authorizationScopes = {authorizationScope};
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Fcadmin Interface Documentation")
                .version("1.0.0") .build(); }}Copy the code