优化生产环境屏蔽swagger处理

pull/6293/head
EightMonth 6 months ago
parent fd0461644e
commit 9db6c1a7ac

@ -1,7 +1,7 @@
package org.jeecg.handler.swagger; package org.jeecg.handler.swagger;
import org.springframework.beans.factory.annotation.Autowired; 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.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -10,7 +10,6 @@ import springfox.documentation.swagger.web.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
/** /**
* swagger doc.html访 * swagger doc.html访
@ -21,11 +20,11 @@ import java.util.regex.Pattern;
@RequestMapping("/swagger-resources") @RequestMapping("/swagger-resources")
public class SwaggerResourceController { public class SwaggerResourceController {
private MySwaggerResourceProvider swaggerResourceProvider; private MySwaggerResourceProvider swaggerResourceProvider;
/**
@Autowired * swagger
private ApplicationContext applicationContext; */
// 生产环境profile配置模型 @Value("${knife4j.production:#{null}}")
private static final String PRODUCTION_PROFILE_NAME = "prod*"; private Boolean production;
@Autowired @Autowired
public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) { public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
@ -44,22 +43,10 @@ public class SwaggerResourceController {
@RequestMapping @RequestMapping
public ResponseEntity<List<SwaggerResource>> swaggerResources() { public ResponseEntity<List<SwaggerResource>> swaggerResources() {
// 如果激活的profile带有生产环境的profile则屏蔽swagger资源 // 是否开启生产环境屏蔽swagger
if (isProd()) { if (production != null && production) {
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK); return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
} }
return new ResponseEntity<>(swaggerResourceProvider.get(), 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;
}
} }
Loading…
Cancel
Save