代码转移

This commit is contained in:
tanghc
2020-01-10 14:02:21 +08:00
parent 57f272df94
commit 994b486163
6 changed files with 68 additions and 25 deletions

View File

@@ -1,68 +0,0 @@
package com.gitee.sop.gateway.controller;
import com.alibaba.fastjson.JSON;
import com.gitee.sop.gateway.manager.DbEnvGrayManager;
import com.gitee.sop.gateway.manager.DbIPBlacklistManager;
import com.gitee.sop.gateway.manager.DbIsvManager;
import com.gitee.sop.gateway.manager.DbIsvRoutePermissionManager;
import com.gitee.sop.gateway.manager.DbLimitConfigManager;
import com.gitee.sop.gateway.manager.DbRouteConfigManager;
import com.gitee.sop.gatewaycommon.bean.GatewayPushDTO;
import com.gitee.sop.gatewaycommon.bean.NacosConfigs;
import com.gitee.sop.gatewaycommon.bean.SpringContext;
import com.gitee.sop.gatewaycommon.manager.ChannelMsgProcessor;
import com.gitee.sop.gatewaycommon.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author tanghc
*/
@Slf4j
@RestController
public class ConfigChannelController {
private static Map<String, Class<? extends ChannelMsgProcessor>> processorMap = new HashMap<>(16);
static {
processorMap.put(NacosConfigs.GROUP_CHANNEL + NacosConfigs.DATA_ID_GRAY, DbEnvGrayManager.class);
processorMap.put(NacosConfigs.GROUP_CHANNEL + NacosConfigs.DATA_ID_IP_BLACKLIST, DbIPBlacklistManager.class);
processorMap.put(NacosConfigs.GROUP_CHANNEL + NacosConfigs.DATA_ID_ISV, DbIsvManager.class);
processorMap.put(NacosConfigs.GROUP_CHANNEL + NacosConfigs.DATA_ID_ROUTE_PERMISSION, DbIsvRoutePermissionManager.class);
processorMap.put(NacosConfigs.GROUP_CHANNEL + NacosConfigs.DATA_ID_LIMIT_CONFIG, DbLimitConfigManager.class);
processorMap.put(NacosConfigs.GROUP_CHANNEL + NacosConfigs.DATA_ID_ROUTE_CONFIG, DbRouteConfigManager.class);
}
@Value("${zuul.secret}")
private String secret;
@PostMapping("/configChannelMsg")
public String configChannel(HttpServletRequest request) throws IOException {
String requestJson = RequestUtil.getText(request);
String sign = request.getHeader("sign");
try {
RequestUtil.checkResponseBody(requestJson, sign, secret);
} catch (Exception e) {
log.error("configChannelMsg错误", e);
return e.getMessage();
}
GatewayPushDTO gatewayPushDTO = JSON.parseObject(requestJson, GatewayPushDTO.class);
ChannelMsgProcessor channelMsgProcessor = getChannelMsgProcessor(gatewayPushDTO);
channelMsgProcessor.process(gatewayPushDTO.getChannelMsg());
return "ok";
}
private ChannelMsgProcessor getChannelMsgProcessor(GatewayPushDTO gatewayPushDTO) {
String key = gatewayPushDTO.getGroupId() + gatewayPushDTO.getDataId();
Class<? extends ChannelMsgProcessor> aClass = processorMap.get(key);
return SpringContext.getBean(aClass);
}
}

View File

@@ -1,71 +0,0 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
import com.gitee.sop.gatewaycommon.bean.ErrorEntity;
import com.gitee.sop.gatewaycommon.manager.ServiceErrorManager;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.result.ApiResult;
import com.gitee.sop.gatewaycommon.result.JsonResult;
import com.gitee.sop.gatewaycommon.util.RequestUtil;
import com.gitee.sop.gatewaycommon.validate.taobao.TaobaoSigner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;
/**
* @author tanghc
*/
@RestController
public class ErrorLogController {
TaobaoSigner signer = new TaobaoSigner();
@Value("${zuul.secret}")
private String secret;
@GetMapping("listErrors")
public ApiResult listErrors(HttpServletRequest request) {
try {
this.check(request);
ServiceErrorManager serviceErrorManager = ApiConfig.getInstance().getServiceErrorManager();
Collection<ErrorEntity> allErrors = serviceErrorManager.listAllErrors();
JsonResult apiResult = new JsonResult();
apiResult.setData(allErrors);
return apiResult;
} catch (Exception e) {
ApiResult apiResult = new ApiResult();
apiResult.setCode("505050");
apiResult.setMsg(e.getMessage());
return apiResult;
}
}
@GetMapping("clearErrors")
public ApiResult clearErrors(HttpServletRequest request) {
try {
this.check(request);
ServiceErrorManager serviceErrorManager = ApiConfig.getInstance().getServiceErrorManager();
serviceErrorManager.clear();
return new ApiResult();
} catch (Exception e) {
ApiResult apiResult = new ApiResult();
apiResult.setCode("505050");
apiResult.setMsg(e.getMessage());
return apiResult;
}
}
private void check(HttpServletRequest request) {
Map<String, String> params = RequestUtil.convertRequestParamsToMap(request);
ApiParam apiParam = ApiParam.build(params);
boolean right = signer.checkSign(apiParam, secret);
if (!right) {
throw new RuntimeException("签名校验失败");
}
}
}

View File

@@ -1,45 +0,0 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import org.springframework.beans.factory.annotation.Value;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 传统web开发入口
* @author tanghc
*/
@WebServlet(urlPatterns = "/rest/*")
public class RestServlet extends HttpServlet {
private static final String EMPTY_VERSION = "";
@Value("${zuul.servlet-path:/zuul}")
private String path;
@Value("${sop.restful.path:/rest}")
private String restPath;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = request.getRequestURL().toString();
int index = url.indexOf(restPath);
// 取/rest的后面部分
String path = url.substring(index + restPath.length());
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, path);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, EMPTY_VERSION);
request.setAttribute(SopConstants.SOP_NOT_MERGE, true);
request.getRequestDispatcher(this.path).forward(request, response);
}
}

View File

@@ -1,66 +0,0 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.zuul.ValidateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* zuul网关入口
*
* @author tanghc
*/
@Slf4j
@Controller
public class ZuulIndexController {
@Autowired
private ValidateService validateService;
@Value("${zuul.servlet-path:/zuul}")
private String path;
/**
* 验证回调,可自定义实现接口
*/
private ValidateService.ValidateCallback callback = (currentContext -> {
try {
currentContext.getRequest().getRequestDispatcher(path).forward(currentContext.getRequest(), currentContext.getResponse());
} catch (Exception e) {
log.error("请求转发异常", e);
}
});
/**
* 网关入口
*
* @param request request
* @param response response
*/
@RequestMapping("/")
public void index(HttpServletRequest request, HttpServletResponse response) {
validateService.validate(request, response, callback);
}
@RequestMapping("/{method}/{version}/")
public void redirect(
@PathVariable("method") String method
, @PathVariable("version") String version
, HttpServletRequest request
, HttpServletResponse response
) {
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version);
validateService.validate(request, response, callback);
}
}