增加白名单域名,优化开放文档,优化展示

This commit is contained in:
暮光:城中城
2019-01-30 22:21:52 +08:00
committed by zhanghongli
parent c8ac59e59a
commit 496bb0d106
13 changed files with 156 additions and 93 deletions

View File

@@ -15,7 +15,6 @@ import com.zyplayer.doc.swagger.framework.service.MgStorageService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -86,38 +85,21 @@ public class MgDocumentController {
+ ":" + request.getServerPort() // 端口号
+ request.getContextPath();
// 是否加入自身的文档
Object enableSwaggerMgUi = SpringContextUtil.getBeanWithAnnotation(EnableSwaggerMgUi.class);
if (enableSwaggerMgUi != null) {
EnableSwaggerMgUi swaggerMgUi = enableSwaggerMgUi.getClass().getAnnotation(EnableSwaggerMgUi.class);
if (swaggerMgUi == null) {
// 直接通过superclass去找
Class<?> superclass = enableSwaggerMgUi.getClass().getSuperclass();
if (superclass != null) {
swaggerMgUi = superclass.getAnnotation(EnableSwaggerMgUi.class);
}
}
if (swaggerMgUi == null) {
// 再通过AopUtils去找
Class<?> targetClass = AopUtils.getTargetClass(enableSwaggerMgUi);
if (targetClass != null) {
swaggerMgUi = targetClass.getAnnotation(EnableSwaggerMgUi.class);
}
}
if (swaggerMgUi == null) {
EnableSwaggerMgUi swaggerMgUi = SpringContextUtil.getEnableSwaggerMgUi();
if (swaggerMgUi == null) {
resourcesSet.add(new SwaggerResourcesInfoVo(serverPath + "/swagger-resources"));
} else {
if (swaggerMgUi.selfDoc()) {
resourcesSet.add(new SwaggerResourcesInfoVo(serverPath + "/swagger-resources"));
} else {
if (swaggerMgUi.selfDoc()) {
resourcesSet.add(new SwaggerResourcesInfoVo(serverPath + "/swagger-resources"));
}
// 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
String[] defaultResources = swaggerMgUi.defaultResources();
for (String url : defaultResources) {
resourcesSet.add(new SwaggerResourcesInfoVo(url));
}
String[] defaultLocation = swaggerMgUi.defaultLocation();
for (String url : defaultLocation) {
locationList.add(new LocationListVo(url, ""));
}
}
// 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
String[] defaultResources = swaggerMgUi.defaultResources();
for (String url : defaultResources) {
resourcesSet.add(new SwaggerResourcesInfoVo(url));
}
String[] defaultLocation = swaggerMgUi.defaultLocation();
for (String url : defaultLocation) {
locationList.add(new LocationListVo(url, ""));
}
}
}

View File

@@ -7,10 +7,12 @@ import com.zyplayer.doc.swagger.controller.param.HttpRequestParam;
import com.zyplayer.doc.swagger.controller.vo.HttpCookieVo;
import com.zyplayer.doc.swagger.controller.vo.HttpHeaderVo;
import com.zyplayer.doc.swagger.controller.vo.HttpRequestVo;
import com.zyplayer.doc.swagger.framework.service.MgStorageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.List;
@@ -20,7 +22,7 @@ import java.util.stream.Collectors;
/**
* 后台代理网络请求的控制器
*
*
* @author 暮光:城中城
* @since 2018年8月21日
*/
@@ -28,8 +30,20 @@ import java.util.stream.Collectors;
@RequestMapping("/swagger-mg-ui/http")
public class MgHttpRequestController {
@Resource
MgStorageService mgStorageService;
@PostMapping(value = "/request")
public DocResponseJson<HttpRequestVo> post(HttpRequestParam param) {
String paramUrl = param.getUrl();
List<String> whiteDomain = mgStorageService.getProxyRequestWhiteDomain();
if (whiteDomain == null || whiteDomain.isEmpty()) {
return DocResponseJson.warn("未设置代理请求白名单,不能代理请求");
}
long inWhiteList = whiteDomain.stream().filter(paramUrl::startsWith).count();
if (inWhiteList <= 0) {
return DocResponseJson.warn("该域名不在白名单内,不能代理请求");
}
HttpRequest request = param.createRequest();
HttpResponse response = request.execute();
HttpRequestVo httpRequestVo = new HttpRequestVo();

View File

@@ -1,13 +1,14 @@
package com.zyplayer.doc.swagger.framework.configuration;
import java.lang.annotation.Annotation;
import java.util.Map;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.util.Map;
/**
* context工具类
*/
@@ -15,6 +16,7 @@ import org.springframework.stereotype.Component;
public class SpringContextUtil implements ApplicationContextAware {
public static ApplicationContext context;
private static EnableSwaggerMgUi ENABLE_SWAGGER_MG_UI;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
@@ -50,5 +52,38 @@ public class SpringContextUtil implements ApplicationContextAware {
}
return null;
}
/**
* 获取EnableSwaggerMgUi
* @date 2019/1/29 12:58
**/
public static EnableSwaggerMgUi getEnableSwaggerMgUi() {
if (ENABLE_SWAGGER_MG_UI != null) {
return ENABLE_SWAGGER_MG_UI;
}
Object annotation = SpringContextUtil.getBeanWithAnnotation(EnableSwaggerMgUi.class);
if (annotation != null) {
EnableSwaggerMgUi swaggerMgUi = annotation.getClass().getAnnotation(EnableSwaggerMgUi.class);
if (swaggerMgUi == null) {
// 直接通过superclass去找
Class<?> superclass = annotation.getClass().getSuperclass();
if (superclass != null) {
swaggerMgUi = superclass.getAnnotation(EnableSwaggerMgUi.class);
}
}
if (swaggerMgUi == null) {
// 再通过AopUtils去找
Class<?> targetClass = AopUtils.getTargetClass(annotation);
if (targetClass != null) {
swaggerMgUi = targetClass.getAnnotation(EnableSwaggerMgUi.class);
}
}
if (swaggerMgUi != null) {
ENABLE_SWAGGER_MG_UI = swaggerMgUi;
}
return swaggerMgUi;
}
return null;
}
}

View File

@@ -45,4 +45,11 @@ public interface MgStorageService {
*/
void remove(String key);
}
/**
* 获取代理请求白名单
* @author 暮光:城中城
* @since 2018年8月19日
*/
List<String> getProxyRequestWhiteDomain();
}