Commit d1f3e7bf authored by zhangc's avatar zhangc

修复api日志收集bug

parent 03a1b37f
......@@ -140,10 +140,10 @@ CREATE TABLE `t_api_req_log` (
`response_params`text COMMENT '返回参数',
`request_url` varchar(64) DEFAULT NULL COMMENT '请求路径',
`request_method` varchar(100) DEFAULT NULL COMMENT '请求方法',
`request_type` varchar(20) DEFAULT NULL COMMENT '请求类型(方式)',
`encry_mode` varchar(20) DEFAULT NULL COMMENT '加密方式:MD5,RSA',
`trans_mode` varchar(20) DEFAULT NULL COMMENT '传输方式:POST,GET',
`request_token` varchar(64) DEFAULT NULL COMMENT '客户请求token',
`handler_status` varchar(20) NOT NULL DEFAULT '0' COMMENT '处理状态: 0未处理,1已处理'
`request_consuming` varchar(64) DEFAULT NULL COMMENT '请求总耗时',
`request_start_time` datetime DEFAULT NULL COMMENT '请求开始时间',
`request_end_time` datetime DEFAULT NULL COMMENT '请求结束时间',
......
......@@ -21,29 +21,37 @@ public enum OpenApiResultCode implements ResultCode {
/** 无效参数 */
ILLEGAL_ARGUMENT("ILLEGAL_ARGUMENT", "无效参数"),
/** 请求数据异常 */
REQUEST_DATA_EXCEPTION("REQUEST_DATA_EXCEPTION", "请求数据异常"),
/** 名类型不支持 */
SIGN_TYPE_NOT_SUPPORT("SIGN_TYPE_NOT_SUPPORT", "签名类型不支持"),
/** 数据签名错误 */
DATA_SIGN_ERROR("DATA_SIGN_ERROR", "数据签名错误"),
/** 公钥格式错误 */
/* *//** 公钥格式错误 *//*
PUBLIC_KEY_FORMAT_ERROR("PUBLIC_KEY_FORMAT_ERROR", "公钥格式错误"),
/** 私钥格式错误 */
PRIVATE_KEY_FORMAT_ERROR("PRIVATE_KEY_FORMAT_ERROR", "私钥格式错误"),
*//** 私钥格式错误 *//*
PRIVATE_KEY_FORMAT_ERROR("PRIVATE_KEY_FORMAT_ERROR", "私钥格式错误"),*/
/** 响应数据格式错误 */
RESPONSE_DATA_FORMAT_ERROR("RESPONSE_DATA_FORMAT_ERROR", "响应数据格式错误"),
/** 签名校验错误 */
SIGN_VERIFY_ERROR("SIGN_VERIFY_ERROR", "签名校验错误"),
/*
*//** 不支持该信息摘要算法 *//*
NO_SUCH_MD_ALGORITHM("NO_SUCH_MD_ALGORITHM", "不支持该信息摘要算法"),*/
/** 不支持该信息摘要算法 */
NO_SUCH_MD_ALGORITHM("NO_SUCH_MD_ALGORITHM", "不支持该信息摘要算法"),
/*
*/
/** 信息摘要错误 *//*
/** 信息摘要错误 */
MESSAGE_DIGEST_ERROR("MESSAGE_DIGEST_ERROR", "信息摘要错误"),
*/
/** 数据加密错误 */
DATA_ENCRYPTION_ERROR("DATA_ENCRYPTION_ERROR", "数据加密错误"),;
......
package com.jz.dm.config;
import com.jz.dm.web.interceptor.AccessLimitInterceptor;
import com.jz.dm.web.request.ApiGatewayRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
......
......@@ -79,7 +79,7 @@ public class AuthFilter extends AbstractFilter {
try {
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
if (null == apiInterface) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
//下架状态$$ 检查是否有有效调用的api,如果有就放行,没有就置为无效
if (ApiStatusEnum.SOLDOUT.name().equals(apiInterface.getStatus())) {
......
......@@ -42,7 +42,7 @@ public class CheckArgsFilter extends AbstractFilter {
request.setFormat(Format.JSON.name());
}
try {
request.setVersion("v1.0.0");
request.setVersion("1.0.0");
//格式,目前仅支持JSON
Format.valueOf(request.getFormat());
//请求使用的编码格式,如UTF-8,GBK,GB2312等
......
package com.jz.dm.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.constant.LoggingConstants;
......@@ -9,6 +10,7 @@ import com.jz.dm.gateway.DefaultOpenApiDispatcher;
import com.jz.dm.models.enity.DispatchContext;
import com.jz.dm.models.enity.GatewayRequest;
import com.jz.dm.models.enity.GatewayResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -19,6 +21,7 @@ import org.springframework.stereotype.Component;
* @author key
*/
@Component("invokeRouteFilter")
@Slf4j
public class InvokeRouteFilter extends AbstractFilter {
/** openapi dispatcher logger */
......@@ -41,14 +44,13 @@ public class InvokeRouteFilter extends AbstractFilter {
protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) {
String openApiResponse = null;
//当前系统时间戳
long startTime = System.currentTimeMillis();
try {
DispatchContext context = new DispatchContext();
context.setApiKey(request.getApiKey());//apiKey
context.setOpenApiMethod(request.getMethod()); //方法 例如:tradd.add
context.setOpenApiParams(request.getParams());//入参
context.setOpenApiVersion(request.getVersion()); //版本号
context.setApiKey(request.getApiKey());
context.setOpenApiMethod(request.getMethod());
context.setOpenApiParams(request.getParams());
context.setOpenApiVersion(request.getVersion());
//扩展参数
for (GatewayRequest.Attribute attribute : request.getExtAttributes().values()) {
if (attribute != null && attribute.isPass) {
......@@ -56,10 +58,11 @@ public class InvokeRouteFilter extends AbstractFilter {
}
}
openApiResponse = defaultOpenApiDispatcher.doDispatch(context);
JSONObject jsonObject = (JSONObject) JSON.parse(openApiResponse);
response.setAttribute(jsonObject);
} finally {
}catch (JSONException ex){
log.error("json exchange exception =",ex.getMessage());
}finally {
long elapseTime = System.currentTimeMillis() - startTime;
LogUtil.info(DISPATCHER_LOGGER,
"gateway do default dispatch,request=" + request + ",response=" + openApiResponse
......
......@@ -5,7 +5,6 @@ import com.jz.common.utils.Md5;
import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.exception.SignatureException;
import com.jz.dm.common.util.LogUtil;
import com.jz.dm.common.util.MapUtil;
import com.jz.dm.models.domian.ApiAuth;
......@@ -52,7 +51,7 @@ public class VerifySignFilter extends AbstractFilter {
//需要传入授权码
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(), authCode);
if (null == apiAuthInfo) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH);
}
String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt());
if (!request.getSign().equals(sign)) {
......@@ -63,9 +62,8 @@ public class VerifySignFilter extends AbstractFilter {
}
}
chain.doFilter(request, response);
} catch (SignatureException ex) {
LogUtil.error(LOGGER, ex,
"sign response error. response=" + response.getResponse());
} catch (GatewayException ex) {
LogUtil.error(LOGGER, ex,"sign response error. response=" + response.getResponse());
response.clearAttributes();
response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg());
......@@ -73,11 +71,6 @@ public class VerifySignFilter extends AbstractFilter {
if (ex instanceof GatewayException) {
throw (GatewayException) ex;
}
LogUtil.error(LOGGER, ex,
"signatureFilter doFilter error. response=" + response.getResponse());
response.clearAttributes();
response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg());
}
}
......
......@@ -85,8 +85,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
return JSON.toJSONString(response.getAttributes());
}
if (StringUtil.isEmpty(response.getCode())) {
response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg());
response.setCode(OpenApiResultCode.REQUEST_DATA_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.REQUEST_DATA_EXCEPTION.getMsg());
}
return JSON.toJSONString(response.getAttributes());
}
......
......@@ -149,4 +149,15 @@ public class ApiInterface extends BaseObject implements Serializable {
@TableField(exist = false)
private String respCode;
/*---------------------------------日志查询--------------------------------*/
/**
* 日志id
* */
@TableField(exist = false)
private Long logId;
/**
* 是否测试
* */
@TableField(exist = false)
private Boolean isTest;
}
......@@ -105,7 +105,4 @@ public class ApiInterfaceCustom extends BaseObject implements Serializable {
@TableField("page_row")
private Long pageRow;
}
......@@ -88,13 +88,17 @@ public class ApiReqLog implements Serializable {
*/
@TableField("request_token")
private String requestToken;
/**
* 处理状态 默认为0 未处理
*/
@TableField("handler_status")
private String handlerStatus;
/**
* 请求总耗时
*/
@ApiModelProperty("请求总耗时")
@TableField("request_consuming")
private Double requestConsuming;
private String requestConsuming;
/**
* 请求开始时间
*/
......
package com.jz.dm.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jz.common.bean.PageInfoResponse;
import com.jz.common.utils.Result;
import com.jz.dm.models.domian.ApiReqLog;
import com.jz.dm.models.dto.DataBankNumberErrorDto;
import com.jz.dm.models.req.DataBankNumberErrorReq;
import com.jz.dm.models.req.LogInfoDetailReq;
import com.jz.dm.models.req.LogInfoListReq;
import net.sf.json.JSONObject;
import java.util.List;
/**
* @author ZC
......@@ -45,9 +41,9 @@ public interface ApiLogService {
/**
* 根据id更新日志
* @param id
* @param jsonObject
* @param response
*/
void updateLog(Long id, JSONObject jsonObject);
void updateLog(Long id,String response);
/**
* 统计API调用数据
......@@ -63,4 +59,10 @@ public interface ApiLogService {
*/
Result<DataBankNumberErrorDto> queryNumberAndError(DataBankNumberErrorReq req);
/**
* 根据apikey查询日志信息
* @param apiKey
* @return
*/
Long getReqLogging(String apiKey);
}
package com.jz.dm.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -16,12 +15,12 @@ import com.jz.dm.models.req.LogInfoDetailReq;
import com.jz.dm.models.req.LogInfoListReq;
import com.jz.dm.service.ApiLogService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
......@@ -46,8 +45,10 @@ public class ApiLogServiceImpl implements ApiLogService {
// 加锁
@Autowired
private ReentrantRedisLock lock;
/**
* 日志信息列表
*
* @param req
* @return
*/
......@@ -55,26 +56,26 @@ public class ApiLogServiceImpl implements ApiLogService {
public IPage<ApiReqLog> listApiLog(LogInfoListReq req) {
IPage<ApiReqLog> page = new Page<>(req.getPageNum(), req.getPageSize());
QueryWrapper<ApiReqLog> query = new QueryWrapper<>();
if (StringUtils.isNotBlank(req.getApiKey())){
query.eq("api_key",req.getApiKey());
if (StringUtils.isNotBlank(req.getApiKey())) {
query.eq("api_key", req.getApiKey());
}
if (StringUtils.isNotBlank(req.getStatus())){
query.eq("status",req.getStatus());
if (StringUtils.isNotBlank(req.getStatus())) {
query.eq("status", req.getStatus());
}
if (StringUtils.isNotBlank(req.getRequestToken())){
query.eq("request_token",req.getRequestToken());
if (StringUtils.isNotBlank(req.getRequestToken())) {
query.eq("request_token", req.getRequestToken());
}
if (StringUtils.isNotBlank(req.getCreateDate())){
if (StringUtils.isNotBlank(req.getCreateDate())) {
String startTime = req.getCreateDate().substring(0, 10) + " 00:00:00";
String endTime = req.getCreateDate().substring(11, 21) + " 23:59:59";
query.between("create_date",startTime, endTime);
query.between("create_date", startTime, endTime);
}
query.eq("is_deleted",0);
query.eq("is_deleted", 0);
query.orderByDesc("create_date");
IPage<ApiReqLog> apiReqLogIPage = apiReqLogMapper.selectPage(page, query);
// 数据银行-历史查询返回参数
if (req.getHistoryQuery().equals("01") && req.getHistoryQuery() != null){
if (req.getHistoryQuery().equals("01") && req.getHistoryQuery() != null) {
for (ApiReqLog record : apiReqLogIPage.getRecords()) {
DataBankNumberErrorReq errorReq = new DataBankNumberErrorReq();
List<Map<String, String>> reqList = new ArrayList<>();
......@@ -97,6 +98,7 @@ public class ApiLogServiceImpl implements ApiLogService {
/**
* 查询日志详情
*
* @param req
* @return
*/
......@@ -107,27 +109,30 @@ public class ApiLogServiceImpl implements ApiLogService {
/**
* 保存日志
*
* @param reqLog
*/
@Override
public void insetLogInfo(ApiReqLog reqLog) {
apiReqLogMapper.insert(reqLog);
}
/**
* api计量统计
*
* @param date
* @return
*/
@Override
public Result countAPiCallStat(String date ) {
Date dateParam =null;
public Result countAPiCallStat(String date) {
Date dateParam = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM");
dateParam = dateFormat.parse(date);
}catch (Exception ex){
log.error("时间转换异常:{}",ex.getMessage());
} catch (Exception ex) {
log.error("时间转换异常:{}", ex.getMessage());
}
return Result.of_success(apiReqLogMapper.countApiCallMonthData(dateParam));
return Result.of_success(apiReqLogMapper.countApiCallMonthData(dateParam));
}
/**
......@@ -150,43 +155,70 @@ public class ApiLogServiceImpl implements ApiLogService {
}
/**
* 根据id更新日志
*
* @param id
* @param jsonObject
* @param response
*/
@Override
public void updateLog(Long id, JSONObject jsonObject) {
public void updateLog(Long id,String response) {
com.alibaba.fastjson.JSONObject jsonObject = null;
if (StringUtils.isNotBlank(response)) {
jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response);
}
try {
/*ApiReqLog apiReqLog = apiReqLogMapper.maxId(id);*/
ApiReqLog apiReqLog = apiReqLogMapper.selectById(id);
if (null != apiReqLog){
if (null != apiReqLog) {
UpdateWrapper<ApiReqLog> update = new UpdateWrapper<>();
if (200 == jsonObject.getInt("code")){
update.set("status", GeneralStatusTypeEnum.SUCCEED);
}else {
update.set("status", GeneralStatusTypeEnum.FAIL);
}
update.set("response_params",jsonObject.toString());
update.set("update_date",new Date());
update.set("request_end_time",new Date());
if (null != response){
if (jsonObject.getString("return_code").equals("ESC00000") &&
jsonObject.getString("return_message").equals("SUCCESS")) {
update.set("status", GeneralStatusTypeEnum.SUCCEED);
} else {
update.set("status", GeneralStatusTypeEnum.FAIL);
}
}else {
update.set("status", GeneralStatusTypeEnum.SUCCEED);
}
update.set("response_params", response);
update.set("update_date", new Date());
update.set("request_end_time", new Date());
update.set("handler_status", "1");
Calendar calendar = Calendar.getInstance();
calendar.setTime(apiReqLog.getRequestStartTime());
Long time = System.currentTimeMillis() - calendar.getTimeInMillis() / 1000;
double timeConsuming = time.doubleValue();
update.set("request_consuming",timeConsuming);
update.eq("id",id);
if (apiReqLogMapper.update(null,update) == 0){
Long time = (System.currentTimeMillis() - calendar.getTimeInMillis());
update.set("request_consuming", time.toString() + "ms");
update.eq("id", id);
if (apiReqLogMapper.update(null, update) == 0) {
log.info("~~~~~~~~~~~更新日志信息失败~~~~~~~~");
}
}else {
log.info("id为:{}",id+"--------------日志信息不存在");
} else {
log.info("id为:{}", id + "--------------日志信息不存在");
}
}catch (Exception ex){
log.error("更新日志返回信息异常:{}",ex.getMessage());
} catch (Exception ex) {
log.error("更新日志返回信息异常:{}", ex.getMessage());
}
}
/**
* 根据apiKey查询日志
*
* @param apiKey
* @return
*/
@Override
public Long getReqLogging(String apiKey) {
QueryWrapper<ApiReqLog> queryWra = new QueryWrapper<>();
queryWra.select("id");
queryWra.eq("api_key", apiKey);
queryWra.eq("handler_status", 0);
queryWra.isNull("status");
List<ApiReqLog> apiReqLogs = apiReqLogMapper.selectList(queryWra);
if (!CollectionUtils.isEmpty(apiReqLogs)) {
return apiReqLogs.get(apiReqLogs.size() - 1).getId();
}
return null;
}
}
......@@ -17,8 +17,8 @@ import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.service.ApiLogService;
import com.jz.dm.service.AuthService;
import com.jz.dm.web.annotation.AccessLimit;
import com.jz.dm.web.annotation.ApiLogAspect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
......@@ -26,8 +26,6 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
......@@ -41,13 +39,13 @@ import java.util.Map;
* @DAY_NAME_SHORT: 周三
* @Description:
**/
@Service("apiQueryService")
@Service
@Slf4j
public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Override
public String getOpenApiMethod() {
return "data.query";
return "request";
}
@Override
......@@ -63,6 +61,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
private RedisUtils redisUtils;
@Autowired
private HttpsUtils httpsUtils;
@Autowired
private ApiLogService reqLogService;
/**
* 数据银行扣款链接
......@@ -78,9 +78,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
*/
@Override
@ApiLogAspect(description = "API请求日志")
@AccessLimit(limit = 10000, sec = 1)
//@AccessLimit(limit = 10000, sec = 1)
@SentinelResource(value = "api.gateway", fallback = "fallbackGateway")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void doService(OpenApiRequest request, OpenApiResponse response) {
boolean bResult = false;
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
......@@ -88,40 +87,48 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
ApiAuth apiAuth = null;
JSONObject jsonParams = null;
try {
Boolean isTest = parameter.getBoolean("isTest");
String reqParams = parameter.getString("reqParams");
Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",false);
}
String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
jsonParams.put("is_test", isTest);
apiInterface.setIsTest(isTest);
}
verifyApiInterface(apiInterface);
apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
//取出缓存数据
//String redisReqParam = redisUtils.get(request.getApiKey());
String redisReqParam = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String apiType = jsonObject.getString("apiType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap,
jsonParams, apiType, apiInterface, response);
} else {//不存在查询数据库
if (!isTest) {//是否是测试
apiInterface.setLogId(reqLogService.getReqLogging(apiInterface.getApiKey()));
String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
verifyApiInterface(apiInterface);
apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
//取出缓存数据
//String redisReqParam = redisUtils.get(request.getApiKey());
String redisReqParam = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String apiType = jsonObject.getString("apiType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap,
jsonParams, apiType, apiInterface, response);
} else {//不存在查询数据库
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
}
} else {
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
} catch (Exception ex) {
if (ex instanceof GatewayException) {
......@@ -131,7 +138,6 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
}
throw (GatewayException) ex;
}
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}", ex.getMessage());
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
......@@ -163,9 +169,9 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if ("10002".equals(apiType) || "10009".equals(apiType)) {//数据表查询
return dataTableSelect(outputType, targetUrl, param, jsonParams, apiInterface, response);
} else if ("10004".equals(apiType) || "10008".equals(apiType)) {//三方查询
return thirdSelect(targetUrl, apiInterface,jsonParams, response);
return thirdSelect(targetUrl, apiInterface, jsonParams, response);
} else if ("10005".equals(apiType) || "10007".equals(apiType)) {//数据包查询
return dataBagDownload(targetUrl,jsonParams,response);
return dataBagDownload(targetUrl, jsonParams, response,apiInterface);
} else {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
......@@ -179,31 +185,33 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param response
* @return
*/
private boolean thirdSelect(String targetUrl, ApiInterface apiInterface,JSONObject jsonParams, OpenApiResponse response) {
private boolean thirdSelect(String targetUrl, ApiInterface apiInterface, JSONObject jsonParams, OpenApiResponse response) {
if ("POST".equalsIgnoreCase(apiInterface.getReqType())) {
return callMethodResponse(httpsUtils.submitPost(targetUrl, jsonParams.toString()), response);
return callMethodResponse(httpsUtils.submitPost(targetUrl, jsonParams.toString()), response,apiInterface);
} else {
Map map = JSONObject.parseObject(jsonParams.toString(), Map.class);
return callMethodResponse(httpsUtils.doGet(targetUrl,map), response);
return callMethodResponse(httpsUtils.doGet(targetUrl, map), response,apiInterface);
}
}
/**
* 数据包下载 $$ 数据银行+DMP
*
* @param targetUrl
* @param param
* @return
*/
private boolean dataBagDownload(String targetUrl,JSONObject param,OpenApiResponse response) {
private boolean dataBagDownload(String targetUrl, JSONObject param, OpenApiResponse response,ApiInterface apiInterface) {
String fileLocation = param.getString("file_location");
String datasourceId = param.getString("datasourceId");
if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
}
JSONObject requestParams = new JSONObject();
requestParams.put("file_location",fileLocation);
requestParams.put("datasourceId",datasourceId);
HttpDownload.postDownload(targetUrl,requestParams);
requestParams.put("file_location", fileLocation);
requestParams.put("datasourceId", datasourceId);
HttpDownload.postDownload(targetUrl, requestParams);
reqLogService.updateLog(apiInterface.getLogId(),null);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
......@@ -223,7 +231,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) {
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo);
return flowRequestMethod(targetUrl, param, response, apiCustomInfo,apiInterface);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo);
} else {
......@@ -270,31 +278,28 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @return
*/
private boolean flowRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo,
ApiInterface apiInterface) {
String dataSize = param.get("dataSize");
Integer valueOf = -1;
if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize);
}
String request$Filed =null;
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
/* try {
request$Filed = URLEncoder.encode(reqParams.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("流下载入参编码异常-------",e.getMessage());
}*/
JSONObject requestParams = new JSONObject();
requestParams.put("datasourceId",apiCustomInfo.getEsDataSource());
requestParams.put("query_database",apiCustomInfo.getEsDataBase());
requestParams.put("query_table",apiCustomInfo.getEsTable());
requestParams.put("request_fileds",reqParams);
requestParams.put("response_fields",assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size",valueOf);
HttpDownload.postDownload(targetUrl,requestParams);
requestParams.put("datasourceId", apiCustomInfo.getEsDataSource());
requestParams.put("query_database", apiCustomInfo.getEsDataBase());
requestParams.put("query_table", apiCustomInfo.getEsTable());
requestParams.put("request_fileds", reqParams);
requestParams.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size", valueOf);
HttpDownload.postDownload(targetUrl, requestParams);
reqLogService.updateLog(apiInterface.getLogId(),null);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
/**
* 数据查询--json请求方式
*
......@@ -307,6 +312,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject();
ApiInterface apiInterface = new ApiInterface();
apiInterface.setApiKey(apiCustomInfo.getApiKey());
try {
params.put("datasourceId", apiCustomInfo.getEsDataSource());//数据源id
params.put("query_database", apiCustomInfo.getEsDataBase());//数据源库名称
......@@ -314,7 +321,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test",jsonParams.get("is_test"));//是否是测试
Boolean isTest = jsonParams.getBoolean("is_test");
params.put("is_test", isTest);//是否是测试
Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageRow());
......@@ -325,12 +333,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (null != pageSize) {
params.put("page_size", pageSize);
}
apiInterface.setIsTest(isTest);
} catch (Exception ex) {
log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace();
}
String respResult = httpsUtils.submitPost(targetUrl, params.toString());
return callMethodResponse(respResult, response);
return callMethodResponse(respResult, response,apiInterface);
}
/**
......@@ -357,7 +367,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param result
* @param response
*/
private boolean callMethodResponse(String result, OpenApiResponse response) {
private boolean callMethodResponse(String result, OpenApiResponse response,ApiInterface apiInterface) {
if (null == result) {
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
......@@ -365,10 +375,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (null != resp && "ESC00000".equals(resp.getString("return_code"))) {
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute(resp);
response.setAttribute("responseResult",resp);
if (null != apiInterface.getLogId()){
reqLogService.updateLog(apiInterface.getLogId(),result);
}
return true;
} else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
reqLogService.updateLog(apiInterface.getLogId(),result);
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
}
......
/*
package com.jz.dm.service.request;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils;
import com.jz.dm.common.constant.LoggingConstants;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum;
import com.jz.dm.common.enums.auth.AuthModeEnum;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.common.util.stream.HttpDownload;
import com.jz.dm.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.web.annotation.AccessLimit;
import com.jz.dm.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
*/
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
......@@ -30,76 +40,407 @@ import java.util.Map;
* @DATE: 2021-1-25/10:13
* @DAY_NAME_SHORT: 周一
* @Description:
**/
@Service("apiQueryTestService")
**//*
@Service
@Slf4j
public class ApiQueryTestService extends ApiParamVerify implements OpenApiService {
public class ApiQueryTestService extends ApiParamVerify implements OpenApiService {
@Override
public String getOpenApiMethod() {
return "test";
return "data.request";
}
@Override
public String getOpenApiVersion() {
return "v1.0.2";
return "1.0.0";
}
@Autowired
private ApiQueryService apiQueryService;
@Resource
private ApiInterfaceService apiInterfaceService;
@Autowired
private AuthService authService;
@Autowired
private RedisUtils redisUtils;
/**
* API测试实现
@Autowired
private HttpsUtils httpsUtils;
*/
/**
* 数据银行扣款链接
*//*
@Value("${data.bank.balanceUrl}")
private String balanceUrl;
*/
/**
* API请求逻辑处理
*
* @param request
* @param response
*/
*//*
@Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
@AccessLimit(sec = 1,limit = 1000)
@SentinelResource(value = "api.gatetest", fallback = "fallbackGateway")
// @ApiLogAspect(description = "API请求日志")
//@AccessLimit(limit = 10000, sec = 1)
@SentinelResource(value = "api.gateway", fallback = "fallbackGateway")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void doService(OpenApiRequest request, OpenApiResponse response) {
boolean bResult = false;
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
verifyApiInterface(apiInterface);
String reqParams = parameter.getString("reqParams");
Boolean isTest = parameter.getBoolean("isTest");
ApiAuth apiAuth = null;
JSONObject jsonParams = null;
Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",true);
try {
String reqParams = parameter.getString("reqParams");
Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",false);
}
if (!isTest){
String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
verifyApiInterface(apiInterface);
apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
//取出缓存数据
//String redisReqParam = redisUtils.get(request.getApiKey());
String redisReqParam = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String apiType = jsonObject.getString("apiType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap,
jsonParams, apiType, apiInterface, response);
} else {//不存在查询数据库
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
}
}else {
bResult=rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
} catch (Exception ex) {
if (ex instanceof GatewayException) {
//调用失败回调数据银行解冻
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
}
throw (GatewayException) ex;
}
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}", ex.getMessage());
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
}
}
*/
/**
* 远程请求获取数据
*
* @param outputType
* @param targetUrl
* @param param
* @param apiType
* @param response
*//*
private boolean rangRequestTarget(String outputType, String targetUrl,
Map<String, String> param, JSONObject jsonParams, String apiType,
ApiInterface apiInterface, OpenApiResponse response) {
if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name();
}
if (StringUtils.isBlank(targetUrl)) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
if (StringUtils.isBlank(apiType)) {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
if ("10002".equals(apiType) || "10009".equals(apiType)) {//数据表查询
return dataTableSelect(outputType, targetUrl, param, jsonParams, apiInterface, response);
} else if ("10004".equals(apiType) || "10008".equals(apiType)) {//三方查询
return thirdSelect(targetUrl, apiInterface,jsonParams, response);
} else if ("10005".equals(apiType) || "10007".equals(apiType)) {//数据包查询
return dataBagDownload(targetUrl,jsonParams,response);
} else {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
}
*/
/**
* 三方查询 $$ 数据银行+DMP
*
* @param targetUrl
* @param apiInterface
* @param response
* @return
*//*
private boolean thirdSelect(String targetUrl, ApiInterface apiInterface,JSONObject jsonParams, OpenApiResponse response) {
if ("POST".equalsIgnoreCase(apiInterface.getReqType())) {
return callMethodResponse(httpsUtils.submitPost(targetUrl, jsonParams.toString()), response);
} else {
Map map = JSONObject.parseObject(jsonParams.toString(), Map.class);
return callMethodResponse(httpsUtils.doGet(targetUrl,map), response);
}
}
*/
/**
* 数据包下载 $$ 数据银行+DMP
* @param targetUrl
* @param param
* @return
*//*
private boolean dataBagDownload(String targetUrl,JSONObject param,OpenApiResponse response) {
String fileLocation = param.getString("file_location");
String datasourceId = param.getString("datasourceId");
if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
}
JSONObject requestParams = new JSONObject();
requestParams.put("file_location",fileLocation);
requestParams.put("datasourceId",datasourceId);
HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
*/
/**
* 数据表查询
*
* @param outputType
* @param targetUrl
* @param param
* @param apiInterface
* @param response
* @return
*//*
protected boolean dataTableSelect(String outputType, String targetUrl, Map<String, String> param,
JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) {
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo);
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
}
}
*/
/**
* 校验参数合法性
*
* @param param
* @param apiInterface
* @return
*//*
private ApiInterfaceCustom checkParamLegal(Map<String, String> param, ApiInterface apiInterface) {
ApiInterfaceCustom apiCustomInfo = apiInterfaceService.getApiCustomInfo(apiInterface.getId());
boolean tag = false;
if (null != apiCustomInfo) {
List<Map> mapList = JSONObject.parseArray(apiCustomInfo.getRequestParam(), Map.class);
if (CollectionUtils.isNotEmpty(mapList)) {
for (Map map : mapList) {
if ((Boolean) map.get("required")) {
String name = (String) map.get("name");
String field = param.get(name);
if (null == field) {
tag = true;
}
}
}
if (tag) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
}
}
return apiCustomInfo;
}
*/
/**
* 数据查询---flow流请求方式
*
* @param targetUrl
* @param param
* @param response
* @return
*//*
private boolean flowRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
String dataSize = param.get("dataSize");
Integer valueOf = -1;
if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize);
}
String request$Filed =null;
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
*/
/* try {
request$Filed = URLEncoder.encode(reqParams.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("流下载入参编码异常-------",e.getMessage());
}*//*
JSONObject requestParams = new JSONObject();
requestParams.put("datasourceId",apiCustomInfo.getEsDataSource());
requestParams.put("query_database",apiCustomInfo.getEsDataBase());
requestParams.put("query_table",apiCustomInfo.getEsTable());
requestParams.put("request_fileds",reqParams);
requestParams.put("response_fields",assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size",valueOf);
HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
*/
/**
* 数据查询--json请求方式
*
* @param targetUrl
* @param param
* @param response
* @param apiCustomInfo
* @return
*//*
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject();
try {
params.put("datasourceId", apiCustomInfo.getEsDataSource());//数据源id
params.put("query_database", apiCustomInfo.getEsDataBase());//数据源库名称
params.put("query_table", apiCustomInfo.getEsTable());//数据源库表
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test",jsonParams.get("is_test"));//是否是测试
Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageRow());
params.put("page_num", 1);
if (null != pageNum) {
params.put("page_num", pageNum);
}
if (null != pageSize) {
params.put("page_size", pageSize);
}
} catch (Exception ex) {
log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace();
}
//取出缓存数据
// String redisReqParam = redisUtils.get(request.getApiKey());
String redisReqParam = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name();
String respResult = httpsUtils.submitPost(targetUrl, params.toString());
return callMethodResponse(respResult, response);
}
*/
/**
* 组装响应参数
*
* @param respParams
* @return
*//*
private String assembleResponseParams(String respParams) {
StringBuilder builder = new StringBuilder();
List<Map> mapList = JSONObject.parseArray(respParams, Map.class);
if (CollectionUtils.isNotEmpty(mapList)) {
for (Map map : mapList) {
String name = (String) map.get("name");
builder.append(name).append(LoggingConstants.SEP);
}
if (StringUtils.isBlank(targetUrl)) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
return builder.substring(0, builder.length() - 1);
}
*/
/**
* 调用方法处理结果
*
* @param result
* @param response
*//*
private boolean callMethodResponse(String result, OpenApiResponse response) {
if (null == result) {
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
JSONObject resp = JSONObject.parseObject(result);
if (null != resp && "ESC00000".equals(resp.getString("return_code"))) {
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute(resp);
return true;
} else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
}
*/
/**
* 通知扣款
*
* @param parameter
* @param bResult
*//*
private void notifierMinusMoney(JSONObject parameter, boolean bResult) {
Integer assetsId = parameter.getInteger("assetsId");
Integer userId = parameter.getInteger("userId");
String dataPrice = parameter.getString("dataPrice");
JSONObject jsonReq = new JSONObject();
jsonReq.put("assetsId", assetsId);
jsonReq.put("userId", userId);
jsonReq.put("dataPrice", dataPrice);
jsonReq.put("callStatus", bResult);//true 调用成功 扣款 false 调用失败,解冻金额
String responseResult = httpsUtils.submitPost(balanceUrl + "/mall/financeCustomerAssets/unfreezeMoney", jsonReq.toString());
JSONObject paramsResult = JSONObject.parseObject(responseResult);
if (null != paramsResult) {
if (200 != paramsResult.getInteger("code")) {
log.info("~~~~~~~~~~~~~~~调用数据银行扣款失败~~~~~~~~~~~~~");
throw new GatewayException(GatewayResultCode.CALL_AMOUNT_NOT_ENOUGH);
}
apiQueryService.dataTableSelect(outputType, targetUrl, paramMap,
jsonParams, apiInterface, response);
} else {//不存在查询数据库
apiQueryService.dataTableSelect(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface, response);
}
}
/**
*/
/**
* 限流返回方法
*
* @param request
* @param response
*/
*//*
public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) {
log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey());
throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
}
}
*/
package com.jz.dm.web.aspect;
import com.jz.common.utils.IpUtils;
import com.jz.common.utils.JsonUtils;
import com.jz.common.utils.UrlUtil;
......@@ -17,10 +16,8 @@ import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
......@@ -54,25 +51,26 @@ public class SystemLogAspect {
@Resource
private ApiReqLogMapper apiReqLogMapper;
/* //前置通知切入点
//前置通知切入点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void beforeAspect() {
}*/
}
//最终通知切入点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
/* @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void lastAspect() {
}
}*/
//环绕通知切入点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
/* @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void aroundAspect() {
}
}*/
@Around("aroundAspect()")
/*@Around("aroundAspect()")
public void doAround(ProceedingJoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
String ip = IpUtils.getIpAddr(request);
//获取用户请求方法的参数并序列化为JSON格式字符串
String params = "";
......@@ -103,11 +101,9 @@ public class SystemLogAspect {
reqLog.setRequestIp(ip);
reqLog.setApiKey(jsonObject.getString("apiKey"));
reqLog.setRequestParams(params);
reqLog.setResponseParams("");//响应参数
reqLog.setRequestUrl(url);
reqLog.setRequestMethod(contextPath);
reqLog.setEncryMode(jsonObject.getString("signType")); //加密方式:MD5,RSA
//reqLog.setEncryMode("MD5"); //加密方式暂时写死MD5
reqLog.setTransMode(request.getMethod());//传输方式 GET POST
reqLog.setRequestToken(jsonParamsList.getString("authCode"));
reqLog.setRequestStartTime(new Date());
......@@ -116,10 +112,12 @@ public class SystemLogAspect {
if (null != reqLog) {
apiReqLogMapper.insert(reqLog);
}
/* Object result = joinPoint.proceed(joinPoint.getArgs());
Object proceed = joinPoint.proceed();
System.out.println("proceed"+proceed);
Object result = joinPoint.proceed(joinPoint.getArgs());
jsonResult = JSONObject.fromObject(result);
log.info("around响应结果为{}", jsonResult);
apiLogService.updateLog(reqLog.getId(), jsonResult);*/
apiLogService.updateLog(reqLog.getId(), jsonResult);
} catch (GatewayException ex) {
log.info("切面处理保存异常信息:{}", ex.getMessage());
apiLogService.updateLog(reqLog.getId(), jsonResult);
......@@ -127,10 +125,9 @@ public class SystemLogAspect {
throwable.printStackTrace();
}
}
}
/*@Before("beforeAspect()")
}*/
@Before("beforeAspect()")
public void doBefore(JoinPoint joinPoint) {
//日志信息收集切面
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = IpUtils.getIpAddr(request);
//获取用户请求方法的参数并序列化为JSON格式字符串
......@@ -140,47 +137,55 @@ public class SystemLogAspect {
params += JsonUtils.objectToJson(joinPoint.getArgs()[i]) + ";";
}
}
if (StringUtils.isNotBlank(params)) {
params = params.substring(0, params.length() - 1).split(";")[0];
}
//获取请求路径
String url = UrlUtil.getServerUrl(request);
// 访问项目名
String contextPath = request.getContextPath();
//JSONObject jsonObject = JSONObject.parseObject(params);
//String param = jsonObject.getString("params");
//JSONObject object = JSONObject.parseObject(param);
//String token = object.getString("token");
try {
log.info("~~~~~~~~~~~~~~~~~~~~~~~前置通知记录请求信息~~~~~~~~~~~~~~~~");
ApiReqLog reqLog = new ApiReqLog();
reqLog.setRequestIp(ip);
// reqLog.setApiKey(jsonObject.getString("apiKey"));
reqLog.setRequestParams(params);
reqLog.setResponseParams("");//响应参数
reqLog.setRequestUrl(url);
reqLog.setRequestMethod(contextPath);
reqLog.setEncryMode(SignType.MD5.name()); //加密方式:MD5,RSA
reqLog.setTransMode(request.getMethod());//传输方式 GET POST
// reqLog.setRequestToken(token);
reqLog.setRequestTime(new Date());
reqLog.setRemark(getServiceMethodDescription(joinPoint));
System.out.println(reqLog);
if (null != reqLog) {
apiLogService.insetLogInfo(reqLog);
log.info("around请求参数为{}", params);
//动态修改其参数
//注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args)
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(params);
String paramsList = jsonObject.getString("openApiParams");
com.alibaba.fastjson.JSONObject jsonParamsList = com.alibaba.fastjson.JSONObject.parseObject(paramsList);
ApiReqLog reqLog = new ApiReqLog();
JSONObject jsonResult = null;
Boolean isTest = jsonParamsList.getBoolean("isTest");
//不保存测试数据
if (!isTest) {
try {
reqLog.setRequestIp(ip);
reqLog.setApiKey(jsonObject.getString("apiKey"));
reqLog.setRequestParams(params);
reqLog.setRequestUrl(url);
reqLog.setEncryMode("MD5");
reqLog.setTransMode(request.getMethod());
reqLog.setRequestToken(jsonParamsList.getString("authCode"));
reqLog.setRequestStartTime(new Date());
reqLog.setRemark(getServiceMethodDescription(joinPoint));
log.info("请求参数:", reqLog);
if (null != reqLog) {
apiReqLogMapper.insert(reqLog);
}
} catch (GatewayException ex) {
log.error("切面处理保存异常信息:{}", ex.getMessage());
/* apiLogService.updateLog(reqLog.getId(), jsonResult);*/
} catch (Throwable throwable) {
throwable.printStackTrace();
}
} catch (Exception e) {
log.error("~~~~~~~~~~~~~~~~~~~~~~~前置通知异常~~~~~~~~~~~~~~~~~~~~~~~");
log.error("异常信息{}", e.getMessage());
}
}*/
/**
}
/* *//**
* 返回异常通知,返回抛出异常的时候执行的通知,可以获得返回的异常
* 可以访问到异常对象,且可以指定在出现特定异常的时候再执行通知代码
*/
*//*
@AfterReturning(value = "lastAspect()", returning = "result")
public void afterReturn(JoinPoint joinPoint, Object result) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
log.info("~~~~~~~~~~~~~~~~~~~~~~最终通知执行~~~~~~~~~~~~~~~~~~~~~~~~");
String classType = joinPoint.getTarget().getClass().getName();
try {
Class<?> clazz = Class.forName(classType);
......@@ -199,8 +204,8 @@ public class SystemLogAspect {
JSONObject jsonObject = JSONObject.fromObject(args[1]);
Map mapResult = (Map) jsonObject;
//将返回的result参数取出
/* Map<String, Object> res = (Map<String, Object>) mapResult.get("data");
Integer id = (Integer) res.get("id");*/
Map<String, Object> res = (Map<String, Object>) mapResult.get("attributes");
Integer id = (Integer) res.get("id");
JSONObject data = jsonObject.getJSONObject("attributes");
log.info("最终通知得到结果:{}",jsonObject);
// apiLogService.updateLog(reqLog.getId(), jsonResult);
......@@ -210,7 +215,7 @@ public class SystemLogAspect {
log.error("异常信息{}", e.getMessage());
}
}
}*/
/**
* 获取注解中对方法的描述信息 用于service层注解
......
......@@ -24,12 +24,14 @@ public class UrlUtil {
int port = request.getServerPort();
// 访问项目名
String contextPath = request.getContextPath();
String url = "%s://%s%s%s";
//接口路径
String requestURI = request.getRequestURI();
String url = "%s://%s%s%s%s";
String portStr = "";
if (port != 80) {
portStr += ":" + port;
}
return String.format(url, agreement, serverName, portStr, contextPath);
return String.format(url, agreement, serverName, portStr, contextPath,requestURI);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment