当前位置: 代码迷 >> 综合 >> gateway整合knife4j swagger
  详细解决方案

gateway整合knife4j swagger

热度:27   发布时间:2023-12-15 06:50:27.0

1.前言

由于微服务项目一开始没使用网关去进行统一鉴权,原来只用了swagger-bootstarp-ui这种适用单体的项目swagger文档增强方案,但加入网关后,每个微服务模块仍然还自带一个前端ui模块,显得臃肿不堪,实际上最好的方式就是,所有文档都在网关上查看,并且只要网关所在的服务需要页面UI模块。
好在这位开源贡献者也知道这个痛点并且早早就做了实现。作者早期在github上开源,不过后期更新放在了码云gitee上了,点击直达

大佬介绍得已经特别清楚了,不过个人在整个集成改造过程中还是遇到了很多问题,特别是有些问题在编辑器中运行得挺好,一用maven打成jar包后就疯狂报错!因此这里记录一下过程中遇到的问题~

2.项目集成

  1. parent pom
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><!--Swagger Resources--><knife4j.version>1.9.6</knife4j.version><sbu.version>1.9.3</sbu.version><springfox.version>2.9.2</springfox.version><swagger.version>1.5.21</swagger.version></properties><!--锁定版本--><dependencyManagement> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-micro-spring-boot-starter</artifactId><version>${knife4j.version}</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>${knife4j.version}</version></dependency> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>${springfox.version}</version><exclusions><exclusion><groupId>io.swagger</groupId><artifactId>swagger-models</artifactId></exclusion></exclusions></dependency><dependency><groupId>io.swagger</groupId><artifactId>swagger-models</artifactId><version>${swagger.version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-bean-validators</artifactId><version>${springfox.version}</version></dependency></dependencies></dependencyManagement>
  1. 在网关聚合文档服务下,可以再把前端的ui资源引入
		<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
<!-- <exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-netty</artifactId></exclusion></exclusions>--></dependency><!-- <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency>--><!--前面的是加入gateway有关的依赖,后面是swagger2的依赖--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>${knife4j.version}</version>
</dependency>
  1. 无需引入前端展示页面的微服务引入
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-micro-spring-boot-starter</artifactId><version>${knife4j.version}</version>
</dependency>
  1. 配置聚合页面文档的配置
    在这里插入图片描述

SwaggerProvider

package com.smart.gateway.config;import lombok.AllArgsConstructor;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;import java.util.ArrayList;
import java.util.List;@Component
@Primary
@AllArgsConstructor
public class SwaggerProvider implements SwaggerResourcesProvider {
    public static final String API_URI = "/v2/api-docs";private final RouteLocator routeLocator;private final GatewayProperties gatewayProperties;@Overridepublic List<SwaggerResource> get() {
    List<SwaggerResource> resources = new ArrayList<>();List<String> routes = new ArrayList<>();routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(routeDefinition -> routeDefinition.getPredicates().stream().filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())).forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", API_URI)))));return resources;}private SwaggerResource swaggerResource(String name, String location) {
    SwaggerResource swaggerResource = new SwaggerResource();swaggerResource.setName(name);swaggerResource.setLocation(location);swaggerResource.setSwaggerVersion("2.0");return swaggerResource;}
}

SwaggerController

package com.smart.gateway.rest;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;
import java.util.Optional;@RestController
@RequestMapping("/swagger-resources")
public class SwaggerController {
    @Autowired(required = false)private SecurityConfiguration securityConfiguration;@Autowired(required = false)private UiConfiguration uiConfiguration;private final SwaggerResourcesProvider swaggerResources;@Autowiredpublic SwaggerController(SwaggerResourcesProvider swaggerResources) {
    this.swaggerResources = swaggerResources;}@GetMapping("/configuration/security")public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
    return Mono.just(new ResponseEntity<>(Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));}@GetMapping("/configuration/ui")public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
    return Mono.just(new ResponseEntity<>(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));}@GetMapping("")public Mono<ResponseEntity> swaggerResources() {
    return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));}
}

有一些低版本的可能还需要一个过滤器将header做一点改变,根据情况决定是否需要
SwaggerHeaderFilter

package com.smart.gateway.filter;import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;//@Component
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory {
    private static final String HEADER_NAME = "X-Forwarded-Prefix";private static final String URI = "/v2/api-docs";@Overridepublic GatewayFilter apply(Object config) {
    return (exchange, chain) -> {
    ServerHttpRequest request = exchange.getRequest();String path = request.getURI().getPath();if (!StringUtils.endsWithIgnoreCase(path,URI )) {
    return chain.filter(exchange);}String basePath = path.substring(0, path.lastIndexOf(URI));ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();return chain.filter(newExchange);};}
}

按照demo做到这里我相信大多数人不会有什么问题;

配置gateway的全局过滤器

这个全局过滤器主要是做一些简单的判断,包括直接放行的路径,包括token的有无,是否过期这些简单地判断;

package com.smart.gateway.filter;import com.alibaba.fastjson.JSON;
import com.smart.gateway.utils.JwtUtil;
import com.smart.pojo.Result;
import com.smart.pojo.StatusCode;
import lombok.SneakyThrows;
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.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;@Component
@Slf4j
public class AuthorizeFilter implements GlobalFilter, Ordered {
    public static final String API_URI = "/v2/api-docs";@SneakyThrows@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    ServerHttpRequest request = exchange.getRequest();ServerHttpResponse response = exchange.getResponse();String url = request.getURI().getPath();
/* if (url.indexOf("/login") >= 0){return chain.filter(exchange);}*/if (url.indexOf(API_URI) >= 0){
      // <------------------------------return chain.filter(exchange);}String authorization = request.getHeaders().getFirst(tokenSign);//后续业务逻辑代码....}
}

这里由于为对文档直接放行,少了一个上面的箭头的指向去跳过这个过滤,导致入下图的切换不成功,中间全被拦截了
在这里插入图片描述

maven打成jar包

在网关聚合文档服务中

<!--必须排除webmvc,和webflux不兼容--><exclusion><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></exclusion><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
<!-- <exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-netty</artifactId></exclusion></exclusions>--></dependency><!-- <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency>-->

gateway的依赖中自带了webflux,而webflux与webmvc是水火不容的,webflux默认自带的netty容器的依赖,而不是tomcat,虽然我在网上搜索到可以排除掉netty,再加入tomcat,打包后jar包也能重启,但是只要有数据传输就会立刻报错,因此又换回了它默认的netty容器。

3. 总结

总结一下,用到gateway的全局过滤器的时候一定要要考虑一下是否需要对某些路径直接放行,再者就是gateway服务要排除任何webmvc依赖,直接引入自带netty容器的spring-cloud-starter-gateway依赖就行。

  相关解决方案