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

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();
}

View File

@@ -113,6 +113,9 @@ $(document).ready(function(){
// debugger;
// 模拟请求开始
postWithFile("swagger-mg-ui/http/request", formDataToServer, function(result){
if (!validateResult(result)) {
return;
}
var afterSendTime = new Date().getTime();
//console.log(result);
var requestObj = result.data;

View File

@@ -113,6 +113,9 @@ $(document).ready(function(){
// debugger;
// 模拟请求开始
postWithFile("swagger-mg-ui/http/request", formDataToServer, function(result){
if (!validateResult(result)) {
return;
}
var afterSendTime = new Date().getTime();
//console.log(result);
var requestObj = result.data;

View File

@@ -58,9 +58,6 @@
Toast.success("刷新成功!");
},
deleteDebugData: function(event){
if(!confirm("确定要删除吗?")) {
return;
}
var tr = $(event.currentTarget).parents("tr");
var index = tr.data("index");
var delKey = app.debugDataList[index].key;

View File

@@ -53,7 +53,7 @@
<div class="modal-body">
<div class="input-line">
地址:
<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例http://192.168.0.172/swagger-resources">
<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例http://127.0.0.1/swagger-resources 或 http://127.0.0.1/v2/api-docs">
</div>
<div class="input-line">
重写域名地址:

View File

@@ -9,12 +9,15 @@
<body>
<div id="app">
<div class="alert alert-primary">
<div class="content">Tips开放文档地址 可以不需要登录即可访问</div>
</div>
<table class="table table-bordered setting-table">
<thead>
<tr>
<td style="width: 50px;">序号</td>
<td>地址</td>
<td>唯一文档地址</td>
<td>开放文档地址</td>
<td>重写域名地址</td>
<td>操作</td>
</tr>
@@ -34,7 +37,6 @@
<tr>
<td colspan="5" align="center">
<button class="btn" type="button" v-on:click="btnRefreshList"> 刷新 </button>
<button class="btn btn-info" type="button" v-on:click="exportDocument">导出文档</button>
<button class="btn btn-primary" type="button" v-on:click="addNewDocument">增加文档</button>
</td>
</tr>
@@ -53,7 +55,7 @@
<div class="modal-body">
<div class="input-line">
地址:
<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例http://192.168.0.172/swagger-resources 或 http://192.168.0.172/v2/api-docs">
<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例http://127.0.0.1/swagger-resources 或 http://127.0.0.1/v2/api-docs">
</div>
<div class="input-line">
重写域名地址:
@@ -134,9 +136,6 @@
}
});
},
exportDocument: function () {
getExport().exportDocument();
},
deleteDocUrl: function (event) {
if (!confirm("确定要删除吗?")) {
return;