springcloud-gateway集成knife4j的示例详解
作者:justry_deng 发布时间:2023-11-29 08:56:44
标签:springcloud,gateway,knife4j
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;
/**
* api 基础信息配置。更多配置信息项见{@link Knife4jConfig}
*
* @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
* src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
* @since 2021.0.1.D
*/
@Data
@Component
public class Knife4jApiInfoProperties {
/**
* 要扫描api的base包
*/
@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;
/**
* knife4j配置类
*
* @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
* src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
* @since 2021.0.1.D
*/
@Slf4j
@Configuration
public class Knife4jConfig implements WebMvcConfigurer {
/** 文档相关资源的链接(需保证这些资源不需要鉴权即可访问) */
public static String[] RESOURCE_URLS = new String[]{"/webjars/**", "/swagger**", "/v2/api-docs", "/doc.html"};
@Value("${spring.application.name:}")
private String applicationName;
@Bean
public Docket docket(Knife4jApiInfoProperties knife4jApiInfoProperties) {
String apiBasePackage = knife4jApiInfoProperties.getBasePackage();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo(knife4jApiInfoProperties))
.select()
.apis(RequestHandlerSelectors.basePackage(apiBasePackage))
.paths(PathSelectors.any())
.build();
if (knife4jApiInfoProperties.isEnableSecurity()) {
docket.securitySchemes(securitySchemes()).securityContexts(securityContexts());
}
return docket;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
private ApiInfo apiInfo(Knife4jApiInfoProperties knife4jApiInfoProperties) {
return new ApiInfoBuilder()
.title(knife4jApiInfoProperties.getTitle())
.description(knife4jApiInfoProperties.getDescription())
.termsOfServiceUrl(StringUtils.isBlank(applicationName) ? "" : "/" + applicationName)
.contact(new Contact(knife4jApiInfoProperties.getContactName(), knife4jApiInfoProperties.getContactUrl(), knife4jApiInfoProperties.getContactEmail()))
.version(knife4jApiInfoProperties.getVersion())
.build();
}
private List<SecurityScheme> securitySchemes() {
// 设置请求头信息
List<SecurityScheme> result = new ArrayList<>();
// 第一个参数,自定义即可。 如:BaseConstant.JWT_TOKEN_KEY=Auth-Token,然后在代码里request.getHeader(BaseConstant.JWT_TOKEN_KEY)取值
ApiKey apiKey = new ApiKey(BaseConstant.JWT_TOKEN_KEY, "Authorization", "header");
result.add(apiKey);
return result;
}
private List<SecurityContext> securityContexts() {
// 设置需要登录认证的路径
List<SecurityContext> result = new ArrayList<>();
result.add(getContextByPath("/*/.*"));
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/**", "/swagger**", "/v2/api-docs", "/doc.html"};
网关集成knife4j
编写配置类Knife4jGatewayConfig
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 网关knife4j配置
*
* @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
* @since 2021.0.1.D
*/
@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));
}
/**
* ui配置
*/
@GetMapping("/swagger-resources/configuration/ui")
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
}
/**
* 资源配置,自动路由到微服务中的各个服务的api-docs信息
*/
@GetMapping("/swagger-resources")
public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
return Mono.just(new ResponseEntity<>(swaggerResourceAdapter.get(), HttpStatus.OK));
}
/**
* favicon.ico
*/
@GetMapping("/favicon.ico")
public Mono<ResponseEntity<?>> favicon() {
return Mono.just(new ResponseEntity<>(null, HttpStatus.OK));
}
/**
* swagger资源适配器
*
* @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
* @since 2021.0.1.D
*/
@Slf4j
@Component
public static class SwaggerResourceAdapter implements SwaggerResourcesProvider {
/**
* spring-cloud-gateway是否开启了根据服务发现自动为服务创建router
*/
@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;
/**
* 根据当前所有的微服务路由信息,创建对应的SwaggerResource
*/
@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;
}
/**
* 创建swagger资源
*
* @param name
* swagger资源名(注:一般对应 {路由id})
* @param location
* swagger资源路径(注:一般对应 {路由id}/v2/api-docs)
* @return swager资源
*/
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
来源:https://blog.csdn.net/justry_deng/article/details/129474480


猜你喜欢
- 思路首先编写程序时,或多或少会存在几个固定的Filter,那么第一步就是为确定的那几个Filter指定好顺序。(通常情况下的使用场景是:你要
- 本文实例为大家分享了java实现双人五子棋游戏的具体代码,供大家参考,具体内容如下通过 上下左右 控制棋盘走动 空格落子 &nb
- 目录一. 已有倒计时方案存在的问题1. CountDownTimer2. Handler3. Timer二. 自己封装倒计时总结一. 已有倒
- WebView设置WebViewClient的方法shouldOverrideUrlLoading:在web页面里单击链接的时候,会自动调用
- import java.util.ArrayList;import java.util.Collections;import java.ut
- spring配置不扫描service层原因我将contoller给springmvc进行扫描,然后其余所有交给spring扫描然后发现ser
- 本文实例讲述了C#基于QRCode实现动态生成自定义二维码图片功能。分享给大家供大家参考,具体如下:二维码早就传遍大江南北了,总以为它是个神
- 之前有学弟问过我一道java的面试题,题目不算难。用java实现杨辉三角。我花了点时间整理了一下,发现挺有意思的,于是想写下来分享一下。在写
- 本文实例为大家分享了C#使用Aforge调用摄像头拍照的具体代码,供大家参考,具体内容如下一、新建一个Winform项目二、使用Nuget添
- 本文实例为大家分享了Android实现拼图小游戏的具体代码,供大家参考,具体内容如下目标效果: 1.activity_main.x
- 贪婪量词:先看整个字符串是不是一个匹配。如果没有发现匹配,它去掉最后字符串中的最后一个字符,并再次尝试。如果还是没有发现匹配,那么 
- java数据结构的堆什么是堆堆指的是使用数组保存完全二叉树结构,以层次遍历的方式放入数组中。如图:注意:堆方式适合于完全二叉树,对于非完全二
- screenshot截图展示import step1. Add it in your root build.gradle at the en
- springboot项目还是ssm等java常用框架都会有这样的问题,解决办法通用问题场景前端发送Post请求,前端返回400 Bad Re
- 本文实例为大家分享了Android实现布局全屏的具体代码,供大家参考,具体内容如下前言类似Launcher,希望占用的布局铺满全屏,以调整状
- RPC是远程过程调用的简称,广泛应用在大规模分布式应用中,作用是有助于系统的垂直拆分,使系统更易拓展。Java中的RPC框架比较多,各有特色
- C++中一个重要的特性就是指针,指针不仅具有获得地址的能力,还具有操作地址的能力。指针可以用于数组、或作为函数的参数,用来访问内存和对内存的
- 集合定义集合,集合是java中提供的一种容器,可以用来存储多个数据。特点:数组的长度是固定的。集合的长度是可变的。集合中存储的元素必须是引用
- 本文实例为大家分享了C#生成验证码图片的具体代码,供大家参考,具体内容如下/// <summary>
- RecyclerView 是 android-support-v7-21 版本中新增的一个 Widgets, 还有一个 CardView 会