springcloud-gateway集成knife4j的示例详解

2023-03-13 11:03:09 SpringCloud

springcloud-gateway集成knife4j

环境信息

环境信息

  • spring-boot:2.6.3
  • spring-cloud-alibaba:2021.0.1.0
  • knife4j-openapi2-spring-boot-starter:4.0.0

准备工作

微服务&网关引入依赖

<dependency>
    <groupId>com.GitHub.xiaoymin</groupId>
    <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>

微服务集成knife4j 第一步:编写Knife4jApiInfoProperties

import com.ideaaedi.sprinGCloud.jd.commonspring.config.Knife4jConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Data
@Component
public class Knife4jApiInfoProperties {
    
    
    @Value("${api-info.base-package:com}")
    private String basePackage;
    
    
    @Value("${api-info.enable-security:true}")
    private boolean enableSecurity;
    
    
    @Value("${api-info.title:}")
    private String title;
    
    
    @Value("${api-info.description:api info}")
    private String description;
    
    
    @Value("${api-info.version:1.0.0}")
    private String version;
    
    
    @Value("${api-info.contact-name:JustryDeng}")
    private String contactName;
    
    
    @Value("${api-info.contact-url:https://gitee.com/JustryDeng/projects}")
    private String contactUrl;
    
    
    @Value("${api-info.contact-email:13548417409@163.com}")
    private String contactEmail;
}

第二步:编写配置类Knife4jConfig

import com.ideaaedi.springcloud.jd.commonds.constant.BaseConstant;
import com.ideaaedi.springcloud.jd.commonspring.config.properties.Knife4jApiInfoProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.WEB.servlet.config.annotation.ResourceHandlerReGIStry;
import org.springframework.web.servlet.config.annotation.WebmvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
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;


@Slf4j
@Configuration
public class Knife4jConfig implements WebMvcConfigurer {
    
    
    public static String[] RESOURCE_URLS = new String[]{"/webjars.*"));
        return result;
    }
    
    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }
    
    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }
    
}

第三步:放行相关静态资源

对于管控了权限的应用,应放行以下资源

# 需要放行的资源已经定义进上面编写的Knife4jConfig中
public static String[] RESOURCE_URLS = new String[]{"/webjars
@RestController
public class Knife4jGatewayConfig {
    
    private final SecurityConfiguration securityConfiguration;
    
    private final UiConfiguration uiConfiguration;
    
    private final swaggerResourceAdapter swaggerResourceAdapter;
    
    public Knife4jGatewayConfig(@Autowired(required = false) SecurityConfiguration securityConfiguration,
                                @Autowired(required = false) UiConfiguration uiConfiguration,
                                SwaggerResourceAdapter swaggerResourceAdapter) {
        this.securityConfiguration = securityConfiguration;
        this.uiConfiguration = uiConfiguration;
        this.swaggerResourceAdapter = swaggerResourceAdapter;
    }
    
    
    @GetMapping("/swagger-resources/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    }
    
    
    @GetMapping("/swagger-resources/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }
    
    
    @GetMapping("/swagger-resources")
    public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
        return Mono.just(new ResponseEntity<>(swaggerResourceAdapter.get(), HttpStatus.OK));
    }
    
    
    @GetMapping("/favicon.ico")
    public Mono<ResponseEntity<?>> favicon() {
        return Mono.just(new ResponseEntity<>(null, HttpStatus.OK));
    }
    
    
    @Slf4j
    @Component
    public static class SwaggerResourceAdapter implements SwaggerResourcesProvider {
        
        
        @Value("${spring.cloud.gateway.discovery.locator.enabled:false}")
        private boolean autoCreateRouter;
        
        @Value("${spring.application.name:}")
        private String applicationName;
        
        @Resource
        private RouteLocator routeLocator;
        
        @Resource
        private GatewayProperties gatewayProperties;
        
        
        @Override
        public List<SwaggerResource> get() {
            List<SwaggerResource> finalResources;
            Set<String> routes = new LinkedHashSet<>(16);
            // 获取所有路由的id
            routeLocator.getRoutes().subscribe(route -> {
                String routeId = route.getId();
                routeId = routeId.replace("ReactiveCompositeDiscoveryClient_", "");
                routes.add(routeId);
            });
            // 没有开启自动创建路由,那么走配置文件中配置的路由
            if (!autoCreateRouter) {
                finalResources = new ArrayList<>(16);
                gatewayProperties.getRoutes().stream()
                        // 过滤出配置文件中定义的路由
                        .filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> {
                            route.getPredicates().stream()
                                    // 过滤出设置有Path Predicate的路由
                                    .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
                                    // 根据路径拼接成api-docs路径,生成SwaggerResource
                                    .forEach(predicateDefinition -> finalResources.add(swaggerResource(route.getId(),
                                            predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
                                                    .replace("**", "v2/api-docs"))));
                        });
            } else {
                finalResources = routes.stream().map(routeId -> swaggerResource(routeId, routeId + "/v2/api-docs")).collect(Collectors.toList());
            }
            List<SwaggerResource> resources = new ArrayList<>(finalResources);
            // resources过滤掉网关的SwaggerResource, 我们一般也不会在网关中编写业务controller
            if (StringUtils.isNotBlank(applicationName)) {
                resources = resources.stream().filter(x -> !applicationName.equalsIgnoreCase(x.getName())).collect(Collectors.toList());
            }
            // 排序
            resources.sort(Comparator.comparing(x -> x.getName().length()));
            return resources;
        }
        
        
        private SwaggerResource swaggerResource(String name, String location) {
            log.info("name:{},location:{}", name, location);
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion("2.0");
            return swaggerResource;
        }
        
    }
}

测试验证

启动微服务后,访问{网关}/doc.html完成验证

在这里插入图片描述

相关资料

SpringBoot2集成knife4j

在微服务项目中引入 knife4j

到此这篇关于springcloud-gateway集成knife4j的文章就介绍到这了,更多相关springcloud-gateway集成knife4j内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章