增加转发接口支持byteBodys

This commit is contained in:
476474485@qq.com 2024-06-19 09:54:19 +08:00
parent b11706fb4e
commit 7fa07577f0
3 changed files with 385 additions and 25 deletions

View File

@ -417,4 +417,14 @@ public interface ISysApplicationService extends IBaseService<SysApplicationEntit
* @return
**/
JsonResultEntity enableOrDisableAppScript(JSONObject jsonObject);
/****
* ESB请求转发重载方法,临时处理传入请求对象
* @content:
* @author 👻👻👻👻👻👻👻👻 gjh
* @date 2024-06-19 9:34
* @param
* @return com.hzya.frame.web.entity.JsonResultEntity
**/
JsonResultEntity externalCallInterfaceToESB(ServletRequest servletRequest, ServletResponse servletResponse);
}

View File

@ -1,6 +1,5 @@
package com.hzya.frame.sysnew.application.service.impl;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
@ -11,6 +10,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.serviceUtil.DsDataSourceUtil;
import com.hzya.frame.sysnew.application.api.dao.ISysApplicationApiDao;
import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiDto;
@ -25,14 +25,10 @@ import com.hzya.frame.sysnew.application.apiPara.dao.ISysApplicationApiParaDao;
import com.hzya.frame.sysnew.application.apiPara.entity.SysApplicationApiParaEntity;
import com.hzya.frame.sysnew.application.apiType.dao.ISysApplicationApiTypeDao;
import com.hzya.frame.sysnew.application.apiType.entity.SysApplicationApiTypeEntity;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.database.dao.ISysApplicationDatabaseDao;
import com.hzya.frame.sysnew.application.database.entity.SysApplicationDatabaseEntity;
import com.hzya.frame.sysnew.application.entity.SysApplicationDatasourceDto;
import com.hzya.frame.sysnew.application.entity.SysApplicationDto;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationVo;
import com.hzya.frame.sysnew.application.entity.SysExtensionApiEntity;
import com.hzya.frame.sysnew.application.entity.*;
import com.hzya.frame.sysnew.application.plugin.dao.ISysApplicationPluginDao;
import com.hzya.frame.sysnew.application.plugin.entity.SysApplicationPluginDto;
import com.hzya.frame.sysnew.application.plugin.entity.SysApplicationPluginEntity;
@ -47,17 +43,12 @@ import com.hzya.frame.sysnew.messageManageDetail.entity.SysMessageManageDetailEn
import com.hzya.frame.sysnew.messageManageLog.dao.ISysMessageManageLogDao;
import com.hzya.frame.sysnew.messageManageLog.entity.SysMessageManageLogEntity;
import com.hzya.frame.sysnew.messageManageLog.entity.SysMessageManageLogPageVo;
import com.hzya.frame.sysnew.popedomInterface.entity.SysPopedomInterfaceEntity;
import com.hzya.frame.sysnew.sysInterface.entity.SysInterfaceEntity;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
import com.hzya.frame.util.AESUtil;
import com.hzya.frame.util.IPHelper;
import com.hzya.frame.uuid.UUIDUtils;
import com.hzya.frame.web.action.ApplicationContextUtil;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
@ -67,27 +58,17 @@ import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.hzya.frame.basedao.service.impl.BaseService;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* 应用表(SysApplication)表服务实现类
@ -2438,4 +2419,369 @@ public class SysApplicationServiceImpl extends BaseService<SysApplicationEntity,
return BaseResult.getSuccessMessageEntity("停用成功");
}
}
@Override
public JsonResultEntity externalCallInterfaceToESB(ServletRequest servletRequest, ServletResponse servletResponse) {
//例如A应用发送数据到中台中台转发到B应用
HttpServletRequest request = (HttpServletRequest) servletRequest;
String oldbodys = ServletUtil.getBody(servletRequest);
Map<String, String> oldheaderMap = ServletUtil.getHeaderMap(request);
String oldquerys = request.getQueryString();
//应用key
String publicKey = request.getHeader("publicKey");
//应用密钥
String secretKey = request.getHeader("secretKey");
//appId
String appId = request.getHeader("appId");
//apiCode
String apiCode = request.getHeader("apiCode");
String ip = IPHelper.getIpAddr(request);
if (publicKey == null || "".equals(publicKey)) {
return BaseResult.getFailureMessageEntity("请先传递公钥");
}
if (secretKey == null || "".equals(secretKey)) {
return BaseResult.getFailureMessageEntity("请先传递密钥");
}
if (appId == null || "".equals(appId)) {
return BaseResult.getFailureMessageEntity("请先传递接收方应用");
}
if (apiCode == null || "".equals(apiCode)) {
return BaseResult.getFailureMessageEntity("请先传递发送接口");
}
logger.info("请求参数publicKey" + publicKey + "】secretKey" + secretKey + "】appId" + appId + "】apiCode" + apiCode);
//根据请求a应用的公钥密钥是否能查找到一条数据
SysApplicationEntity sendApp = getAppByPublicKeySecretKey(publicKey, secretKey);
if (sendApp == null) {
//saveLog(new SysApplicationEntity(), new SysApplicationEntity(), new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,"公钥、密钥错误");
return BaseResult.getFailureMessageEntity("公钥、密钥错误,请联系管理员");
}
//判断应用是否启用
if (sendApp.getAppStatus() == null || !"1".equals(sendApp.getAppStatus())) {
saveLog(sendApp, new SysApplicationEntity(), new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,sendApp.getName() + "应用未启用");
return BaseResult.getFailureMessageEntity(sendApp.getName() + "应用未启用,请联系管理员");
}
SysApplicationEntity receiveApp = getAppByAppId(appId);
if (receiveApp == null) {
saveLog(sendApp, new SysApplicationEntity(), new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,"根据appId:" + appId + "未匹配到应用");
return BaseResult.getFailureMessageEntity("根据appId:" + appId + "未匹配到应用,请联系管理员");
}
//判断应用是否启用
if (receiveApp.getAppStatus() == null || !"1".equals(receiveApp.getAppStatus())) {
saveLog(sendApp, receiveApp, new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + "应用未启用" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + "应用未启用,请联系管理员");
}
//判断应用接口是否启用
if (receiveApp.getInterfaceStatus() == null || !"1".equals(receiveApp.getInterfaceStatus())) {
saveLog(sendApp, receiveApp, new SysApplicationApiEntity(), oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + "应用接口环境未启用" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + "应用接口环境未启用,请联系管理员");
}
SysApplicationApiEntity receiveApi = getApiByAppIdApiCode(receiveApp.getId(), apiCode);
if (receiveApi == null) {
saveLog(sendApp, receiveApp, receiveApi, oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + ":" + apiCode + "未启用或者未创建" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + ":" + apiCode + "未启用或者未创建");
}
SysApplicationApiAuthEntity sysApplicationApiAuthEntity = getApiAuthByNameAppId(sendApp.getId(), receiveApp.getId());
if (sysApplicationApiAuthEntity == null) {
saveLog(sendApp, receiveApp, receiveApi, oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + "应用权限配置错误" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + "应用权限配置错误");
}
if (sysApplicationApiAuthEntity.getSystemAddress() != null && !"".equals(sysApplicationApiAuthEntity.getSystemAddress())
&& !sysApplicationApiAuthEntity.getSystemAddress().contains(ip)) {
saveLog(sendApp, receiveApp, receiveApi, oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApp.getName() + "发送应用" + receiveApp.getName() + "的ip白名单配置错误" );
return BaseResult.getFailureMessageEntity(receiveApp.getName() + "发送应用" + receiveApp.getName() + "的ip白名单配置错误");
}
SysApplicationApiAuthDetailEntity sysApplicationApiAuthDetailEntity = getApiAuthDetailByAppIdApiIdTripartiteSystemId(receiveApp.getId(), receiveApi.getId(), sysApplicationApiAuthEntity.getId());
if (sysApplicationApiAuthDetailEntity == null) {
saveLog(sendApp, receiveApp, receiveApi, oldbodys, null, oldheaderMap, null, null, oldquerys, null, null, false,receiveApi.getApiName() + "未授权给" + sendApp.getName() );
return BaseResult.getFailureMessageEntity(receiveApi.getApiName() + "未授权给" + sendApp.getName() + ",请联系管理员");
}
SysExtensionApiEntity sysExtensionApiEntity = new SysExtensionApiEntity();
List<String> a = Arrays.asList(new String[]{"apicode", "appid", "secretkey", "publickey", "x-forwarded-for", "cookie", "x-forwarded-proto", "x-real-ip", "content-length", "accept-language", "host", "content-type", "connection", "cache-control", "accept-encoding", "pragma", "accept", "user-agent"});
Map<String, String> headers = new HashMap<>();
if (receiveApi.getHeaderIn() != null && !"".equals(receiveApi.getHeaderIn())) {
JSONArray jsonArray = JSONArray.parseArray(receiveApi.getHeaderIn());
if (jsonArray != null && jsonArray.size() > 0) {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object1 = jsonArray.getJSONObject(i);
headers.put(object1.getString("parameterName"), object1.getString("example"));
}
}
}
if (oldheaderMap != null && oldheaderMap.size() > 0) {
for (Map.Entry<String, String> entry : oldheaderMap.entrySet()) {
if (!a.contains(entry.getKey())) {
headers.put(entry.getKey(), entry.getValue());
}
}
}
sysExtensionApiEntity.setSendApp(sendApp);
sysExtensionApiEntity.setReceiveApp(receiveApp);
sysExtensionApiEntity.setReceiveApi(receiveApi);
sysExtensionApiEntity.setHeaders(headers);
sysExtensionApiEntity.setQuerys(oldquerys);
sysExtensionApiEntity.setBodys(oldbodys);
Method[] methods = null;
Object object = null;
// 判断是否有内部api 是否扩展api 1启用 2停用
if (receiveApi.getExtensionApi() != null && "1".equals(receiveApi.getExtensionApi())
&& receiveApi.getBeanName() != null && !"".equals(receiveApi.getBeanName())
&& receiveApi.getFunName() != null && !"".equals(receiveApi.getFunName())
) {
//获取类
try {
object = ApplicationContextUtil.getBeanByName(receiveApi.getBeanName());
} catch (SecurityException e) {
}
//获取类下面的方法
methods = object.getClass().getMethods();
if (methods == null || methods.length == 0) {
return BaseResult.getFailureMessageEntity("未找到内部方法,请联系管理员");
}
for (Method m : methods) {
if (null != m) {
if (m.getName().equals(receiveApi.getFunName().trim())) {
try {
logger.info("invoke开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
sysExtensionApiEntity = (SysExtensionApiEntity) m.invoke(object, sysExtensionApiEntity);
logger.info("invoke结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
logger.error("invokeException{}", e.getMessage());
return BaseResult.getFailureMessageEntity("内部方法执行错误,请联系管理员");
}
}
}
}
}
headers = sysExtensionApiEntity.getHeaders();
String querys = sysExtensionApiEntity.getQuerys();
byte[] byteBodys = sysExtensionApiEntity.getByteBodys();
//设置参数获取参数
StringBuffer url = new StringBuffer();
if(!receiveApi.getDestinationAddress().toLowerCase().startsWith("http")){
url.append(receiveApp.getInterfaceAddress());
}
url.append(receiveApi.getDestinationAddress());
if (querys != null) {
url.append("?");
url.append(querys);
}
Integer outTime = 6000;
if (receiveApi.getTimeoutPeriod() != null && !"".equals(receiveApi.getTimeoutPeriod())) {
outTime = Integer.valueOf(receiveApi.getTimeoutPeriod());
}
//1POST 2GET
String method = "POST";
if ("2".equals(receiveApi.getRequestMethod())) {
method = "GET";
}
//List<String> a = Arrays.asList(new String[]{"apicode", "appid", "secretkey", "publickey", "x-forwarded-for", "cookie", "x-forwarded-proto", "x-real-ip", "content-length", "accept-language", "host", "content-type", "connection", "cache-control", "accept-encoding", "pragma", "accept", "user-agent"});
//Map<String, String> headers = new HashMap<>();
//if (receiveApi.getHeaderIn() != null && !"".equals(receiveApi.getHeaderIn())) {
// JSONArray jsonArray = JSONArray.parseArray(receiveApi.getHeaderIn());
// if (jsonArray != null && jsonArray.size() > 0) {
// for (int i = 0; i < jsonArray.size(); i++) {
// JSONObject object1 = jsonArray.getJSONObject(i);
// headers.put(object1.getString("parameterName"), object1.getString("example"));
// }
// }
//}
//if (headerMap != null && headerMap.size() > 0) {
// for (Map.Entry<String, String> entry : headerMap.entrySet()) {
// if (!a.contains(entry.getKey())) {
// headers.put(entry.getKey(), entry.getValue());
// }
// }
//}
if ("POST".equals(method)) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.disableCookieManagement().build();
HttpPost post = new HttpPost(url.toString());
CloseableHttpResponse response = null;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(outTime).build();
post.setConfig(requestConfig);//设置请求参数超时时间
if (headers != null && headers.size() > 0) {
for (String key : headers.keySet()) {
post.setHeader(key, headers.get(key));
}
}
StringBuilder body = new StringBuilder();
boolean flag = true;
try {
if (byteBodys != null && !"".equals(byteBodys)) {
ByteArrayEntity entity = new ByteArrayEntity(byteBodys);
entity.setContentType("application/json");
post.setEntity(entity);
}
response = closeableHttpClient.execute(post);
HttpEntity entity = response.getEntity();
synchronized (lock) {
body.append(EntityUtils.toString(entity,"UTF-8"));
}
flag = true;
logger.info("返回结果:" + body);
} catch (Exception e) {
logger.error("请求错误:" + e.getMessage());
body.append(e.getMessage());
flag = false;
} finally {
try {
// 关闭响应对象
if (response != null) {
response.close();
}
// 关闭响应对象
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
logger.info("保存日志开始");
SysMessageManageLogEntity sysMessageManageLogEntity = saveLog(sendApp, receiveApp, receiveApi, oldbodys,String.valueOf(byteBodys), oldheaderMap,headers, headers, oldquerys,querys, body.toString(),true,null);
if (methods != null && methods.length > 0) {
for (Method m : methods) {
if (null != m) {
if (m.getName().equals(receiveApi.getFunName().trim()+"CallBack")) {
try {
logger.info("invoke开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
m.invoke(object, sysMessageManageLogEntity);
logger.info("invoke结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
logger.error("invokeException{}", e.getMessage());
}
}
}
}
}
logger.info("保存日志结束");
if (flag) {
if (JSONUtil.isTypeJSON(body.toString())) {
JSONObject jsonObject = JSONObject.parseObject(body.toString());
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", jsonObject);
}else {
return BaseResult.getFailureMessageEntity("转发失败", jsonObject);
}
} else {
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", body);
}else {
return BaseResult.getFailureMessageEntity("转发失败", body);
}
}
} else {
return BaseResult.getFailureMessageEntity("转发失败", body);
}
} else {//GET
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.disableCookieManagement().build();
HttpGet get = new HttpGet(url.toString());
CloseableHttpResponse response = null;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(outTime).build();
get.setConfig(requestConfig);//设置请求参数超时时间
if (headers != null && headers.size() > 0) {
for (String key : headers.keySet()) {
get.setHeader(key, headers.get(key));
}
}
StringBuilder body = new StringBuilder();
boolean flag = true;
try {
response = closeableHttpClient.execute(get);
HttpEntity entity = response.getEntity();
synchronized (lock) {
body.append(EntityUtils.toString(entity,"UTF-8"));
}
flag = true;
logger.info("返回结果:" + body);
} catch (Exception e) {
logger.error("请求错误:" + e.getMessage());
body.append(e.getMessage());
flag = false;
} finally {
try {
// 关闭响应对象
if (response != null) {
response.close();
}
// 关闭响应对象
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
logger.info("保存日志开始");
SysMessageManageLogEntity sysMessageManageLogEntity = saveLog(sendApp, receiveApp, receiveApi, oldbodys,String.valueOf(byteBodys), oldheaderMap,headers, headers, oldquerys,querys, body.toString(), true,null);
if (methods != null && methods.length > 0) {
for (Method m : methods) {
if (null != m) {
if (m.getName().equals(receiveApi.getFunName().trim()+"CallBack")) {
try {
logger.info("invoke开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
m.invoke(object, sysMessageManageLogEntity);
logger.info("invoke结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
logger.error("invokeException{}", e.getMessage());
}
}
}
}
}
logger.info("保存日志结束");
if (flag) {
if (JSONUtil.isTypeJSON(body.toString())) {
JSONObject jsonObject = JSONObject.parseObject(body.toString());
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", jsonObject);
}else {
return BaseResult.getFailureMessageEntity("转发失败", jsonObject);
}
} else {
if(sysMessageManageLogEntity.getStatus() != null && "3".equals(sysMessageManageLogEntity.getStatus())){
return BaseResult.getSuccessMessageEntity("转发成功", body);
}else {
return BaseResult.getFailureMessageEntity("转发失败", body);
}
}
} else {
return BaseResult.getFailureMessageEntity("转发失败", body);
}
}
}
}

View File

@ -164,5 +164,9 @@ public class EntranceController {
public JsonResultEntity externalCallInterface(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
return sysApplicationService.externalCallInterface(servletRequest,servletResponse);
}
@RequestMapping(value = "/externalCallInterface")
@ResponseBody
public JsonResultEntity externalCallInterfaceToESB(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
return sysApplicationService.externalCallInterfaceToESB(servletRequest,servletResponse);
}
}