From 0dc84016c5c02e42fe45f272e9fe3f811140f2e9 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 11 Jul 2023 12:07:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20swagger=E5=8F=AF=E4=BB=A5=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=98=AF=E5=90=A6=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/filter/SwaggerFilter.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 framework/gateway/src/main/java/io/metersphere/gateway/filter/SwaggerFilter.java diff --git a/framework/gateway/src/main/java/io/metersphere/gateway/filter/SwaggerFilter.java b/framework/gateway/src/main/java/io/metersphere/gateway/filter/SwaggerFilter.java new file mode 100644 index 0000000000..33dcec151d --- /dev/null +++ b/framework/gateway/src/main/java/io/metersphere/gateway/filter/SwaggerFilter.java @@ -0,0 +1,71 @@ +package io.metersphere.gateway.filter; + +import io.metersphere.security.ApiKeyHandler; +import jakarta.annotation.PostConstruct; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.Ordered; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ResponseStatusException; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import org.springframework.web.util.pattern.PathPattern; +import org.springframework.web.util.pattern.PathPatternParser; +import reactor.core.publisher.Mono; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class SwaggerFilter implements WebFilter, Ordered { + + @Value("${swagger.enabled:true}") + private boolean swaggerEnabled; + private final List swaggerPatterns = new ArrayList<>(); + + @PostConstruct + public void init() { + + swaggerPatterns.add(new PathPatternParser().parse("/swagger-ui.html")); + swaggerPatterns.add(new PathPatternParser().parse("/swagger-ui/**")); + swaggerPatterns.add(new PathPatternParser().parse("/v3/api-docs/**")); + + swaggerPatterns.add(new PathPatternParser().parse("/*/swagger-ui.html")); + swaggerPatterns.add(new PathPatternParser().parse("/*/swagger-ui/**")); + swaggerPatterns.add(new PathPatternParser().parse("/*/v3/api-docs/**")); + } + + + public static Boolean isApiKeyCall(ServerHttpRequest request) { + if (request == null) { + return false; + } + + return StringUtils.isNotBlank(request.getHeaders().getFirst(ApiKeyHandler.API_ACCESS_KEY)) + && StringUtils.isNotBlank(request.getHeaders().getFirst(ApiKeyHandler.API_SIGNATURE)); + } + + @Override + public Mono filter(final ServerWebExchange serverWebExchange, final WebFilterChain webFilterChain) { + ServerHttpRequest request = serverWebExchange.getRequest(); + if (isApiKeyCall(request)) { + return webFilterChain.filter(serverWebExchange); + } + + if (swaggerPatterns.stream().anyMatch(pathPattern -> pathPattern.matches(request.getPath().pathWithinApplication()))) { + if (!swaggerEnabled) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + } + return webFilterChain.filter(serverWebExchange); + } + + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE; + } +}