From 9db6c1a7acbaab4f814b77db2ce848b916fbf24c Mon Sep 17 00:00:00 2001 From: EightMonth Date: Thu, 6 Jun 2024 10:39:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=9F=E4=BA=A7=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=B1=8F=E8=94=BDswagger=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../swagger/SwaggerResourceController.java | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/swagger/SwaggerResourceController.java b/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/swagger/SwaggerResourceController.java index be7667cc..04ac769a 100644 --- a/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/swagger/SwaggerResourceController.java +++ b/jeecg-server-cloud/jeecg-cloud-gateway/src/main/java/org/jeecg/handler/swagger/SwaggerResourceController.java @@ -1,7 +1,7 @@ package org.jeecg.handler.swagger; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,7 +10,6 @@ import springfox.documentation.swagger.web.*; import java.util.ArrayList; import java.util.List; -import java.util.regex.Pattern; /** * swagger聚合接口,三个接口都是 doc.html需要访问的接口 @@ -21,11 +20,11 @@ import java.util.regex.Pattern; @RequestMapping("/swagger-resources") public class SwaggerResourceController { private MySwaggerResourceProvider swaggerResourceProvider; - - @Autowired - private ApplicationContext applicationContext; - // 生产环境profile配置模型 - private static final String PRODUCTION_PROFILE_NAME = "prod*"; + /** + * 生产环境,关闭swagger文档 + */ + @Value("${knife4j.production:#{null}}") + private Boolean production; @Autowired public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) { @@ -44,22 +43,10 @@ public class SwaggerResourceController { @RequestMapping public ResponseEntity> swaggerResources() { - // 如果激活的profile带有生产环境的profile,则屏蔽swagger资源 - if (isProd()) { + // 是否开启生产环境屏蔽swagger + if (production != null && production) { return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK); } return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK); } - - private boolean isProd() { - String[] profiles = applicationContext.getEnvironment().getActiveProfiles(); - Pattern pattern = Pattern.compile(PRODUCTION_PROFILE_NAME); - for (String profile : profiles) { - if (pattern.matcher(profile).find()) { - return true; - } - } - - return false; - } } \ No newline at end of file