Commit 0717005a authored by zhangc's avatar zhangc

添加api请求接口

parent a0979d18
...@@ -11,7 +11,7 @@ CREATE TABLE `t_api_interface` ( ...@@ -11,7 +11,7 @@ CREATE TABLE `t_api_interface` (
`api_type` varchar(50) NULL DEFAULT '' COMMENT 'api类型:1 数据银行制作API 2 数据银行制作数据包, 3 API实时接入 6 标签查询 9自定义', `api_type` varchar(50) NULL DEFAULT '' COMMENT 'api类型:1 数据银行制作API 2 数据银行制作数据包, 3 API实时接入 6 标签查询 9自定义',
`api_function` varchar(200) DEFAULT NULL COMMENT '接口功能', `api_function` varchar(200) DEFAULT NULL COMMENT '接口功能',
`join_type` varchar(50) NULL DEFAULT NULL COMMENT '接入类型:字典表对应key值', `join_type` varchar(50) NULL DEFAULT NULL COMMENT '接入类型:字典表对应key值',
`test_example` varchar(255) NULL DEFAULT NULL COMMENT '测试实例'; `test_example` varchar(255) NULL DEFAULT NULL COMMENT '测试实例',
`status` varchar(50) NOT NULL COMMENT '状态(DRAFT-草稿 ISSUE-发布 OUTMODED-过时 4FREEZE-冻结 EXPIRY-失效)', `status` varchar(50) NOT NULL COMMENT '状态(DRAFT-草稿 ISSUE-发布 OUTMODED-过时 4FREEZE-冻结 EXPIRY-失效)',
`output_type` varchar(50) NULL DEFAULT 'JSON' COMMENT '输出类型:flow 流形式输出, json格式输出', `output_type` varchar(50) NULL DEFAULT 'JSON' COMMENT '输出类型:flow 流形式输出, json格式输出',
`page` tinyint(2) NULL DEFAULT '0' COMMENT '是否分页:0 false ,1 true', `page` tinyint(2) NULL DEFAULT '0' COMMENT '是否分页:0 false ,1 true',
...@@ -19,6 +19,8 @@ CREATE TABLE `t_api_interface` ( ...@@ -19,6 +19,8 @@ CREATE TABLE `t_api_interface` (
`sign_type` varchar(50) DEFAULT NULL COMMENT '加密方式: MD5 RSA', `sign_type` varchar(50) DEFAULT NULL COMMENT '加密方式: MD5 RSA',
`timeout` varchar(50) DEFAULT NULL COMMENT '超时时间', `timeout` varchar(50) DEFAULT NULL COMMENT '超时时间',
`file_id` bigint(20) NOT NULL COMMENT '文件夹id', `file_id` bigint(20) NOT NULL COMMENT '文件夹id',
`req_type` varchar(20) DEFAULT NULL COMMENT '限制类型:DAY 按天,MONTH 按月, YEAR 按年',
`req_frequency` bigint(20) DEFAULT '100' COMMENT '限制次数',
`is_test` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否测试数据:0 否,1 是', `is_test` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否测试数据:0 否,1 是',
`remark` varchar(500) DEFAULT NULL COMMENT '备注', `remark` varchar(500) DEFAULT NULL COMMENT '备注',
`create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
...@@ -103,6 +105,8 @@ CREATE TABLE `t_api_auth` ( ...@@ -103,6 +105,8 @@ CREATE TABLE `t_api_auth` (
`api_interface_id` bigint(20) NOT NULL COMMENT 'api信息表id', `api_interface_id` bigint(20) NOT NULL COMMENT 'api信息表id',
`api_org_id` bigint(20) NOT NULL COMMENT 'api组织id', `api_org_id` bigint(20) NOT NULL COMMENT 'api组织id',
`auth_type` varchar(20) DEFAULT NULL COMMENT '授权类型:DATA_BANK_AUTH 数据银行购买 DMP_AUTH dmp授权', `auth_type` varchar(20) DEFAULT NULL COMMENT '授权类型:DATA_BANK_AUTH 数据银行购买 DMP_AUTH dmp授权',
`req_type` varchar(20) DEFAULT NULL COMMENT '限制类型:DAY 按天,MONTH 按月, YEAR 按年',
`req_frequency` bigint(20) DEFAULT '100' COMMENT '限制次数',
`auth_code` varchar(100) DEFAULT NULL COMMENT '授权码', `auth_code` varchar(100) DEFAULT NULL COMMENT '授权码',
`salt` varchar(200) DEFAULT NULL COMMENT '盐值', `salt` varchar(200) DEFAULT NULL COMMENT '盐值',
`auth_mode` varchar(50) DEFAULT NULL COMMENT '授权方式:POWER_CALL_MODE 按次调用 ,RECORD_TIME_MODE 按时间调用 PERMANENT_TIME_MODE 永久有效', `auth_mode` varchar(50) DEFAULT NULL COMMENT '授权方式:POWER_CALL_MODE 按次调用 ,RECORD_TIME_MODE 按时间调用 PERMANENT_TIME_MODE 永久有效',
......
...@@ -97,18 +97,29 @@ ...@@ -97,18 +97,29 @@
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency> </dependency>
<!-- 校验--> <!-- validation校验-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId> <artifactId>spring-boot-starter-validation</artifactId>
</dependency> </dependency>
<!--Sentinel 限流框架依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<!-- <version>0.9.0.RELEASE</version>-->
</dependency>
<dependency> <dependency>
<groupId>net.sf.json-lib</groupId> <groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId> <artifactId>json-lib</artifactId>
<version>2.2.3</version> <version>2.2.3</version>
<classifier>jdk15</classifier><!-- jdk版本 --> <classifier>jdk15</classifier><!-- jdk版本 -->
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -48,4 +48,7 @@ public class LoggingConstants { ...@@ -48,4 +48,7 @@ public class LoggingConstants {
/** "&" 符号*/ /** "&" 符号*/
public static final String AND_SPILT = "&"; public static final String AND_SPILT = "&";
/** "=" 符号*/
public static final String AND_EQUAL = "=";
} }
...@@ -67,6 +67,11 @@ public enum GatewayResultCode implements ResultCode { ...@@ -67,6 +67,11 @@ public enum GatewayResultCode implements ResultCode {
ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"), ORG_STATE_EXCEPTION("ORG_STATE_EXCEPTION", "请求组织状态异常"),
/** 请求信息不存在 */ /** 请求信息不存在 */
REQUEST_INFO_UNEXIST("REQUEST_INFO_UNEXIST", "请求信息不存在"), REQUEST_INFO_UNEXIST("REQUEST_INFO_UNEXIST", "请求信息不存在"),
/** 远程请求异常 */
DISTANCE_REQUEST_EXCEPTION("DISTANCE_REQUEST_EXCEPTION", "流形式输出异常"),
/** 单次调用金额不足 */
CALL_AMOUNT_NOT_ENOUGH("CALL_AMOUNT_NOT_ENOUGH", "单次调用金额不足"),
/** 输出类型错误 */ /** 输出类型错误 */
OUTPUT_TYPE_EXCEPTION("OUTPUT_TYPE_EXCEPTION", "输出类型错误!"); OUTPUT_TYPE_EXCEPTION("OUTPUT_TYPE_EXCEPTION", "输出类型错误!");
......
package com.jz.dm.common.enums.auth;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.enums.auth
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthReqTypeEnum
* @DATE: 2021-1-8/18:44
* @DAY_NAME_SHORT: 周五
* @Description:
**/
public enum AuthReqTypeEnum {
DAY("天"),
MONTH("月"),
YEAR("年");
private String text;
AuthReqTypeEnum(String text) {
this.text = text;
}
public String getText() {
return text;
}
public static AuthReqTypeEnum fromAuthReqTypeEnum(String typeName) {
for (AuthReqTypeEnum type : AuthReqTypeEnum.values()) {
if (type.name().equals(typeName)) {
return type;
}
}
return null;
}
}
package com.jz.dm.common.util; package com.jz.dm.common.util;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date;
/** /**
* @author ZC * @author ZC
...@@ -27,7 +29,44 @@ public class DateUtil { ...@@ -27,7 +29,44 @@ public class DateUtil {
return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000; return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
} }
/**
* 计算当前距离下月一号剩余时间
* @return
*/
public static long calculateNowMonthTime(){
Calendar c = Calendar.getInstance();//获取一个Calendar对象
c.setTime(new Date() );
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 0);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return (c.getTimeInMillis() - System.currentTimeMillis()) / 1000;
}
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(calculateNowResidueTime()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//System.out.println(calculateNowResidueTime());
Calendar c = Calendar.getInstance();//获取一个Calendar对象
c.setTime(new Date() );
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 0);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
Date time = c.getTime();
String format = dateFormat.format(time);
System.out.println(format);
Calendar cal = Calendar.getInstance();//获取一个Calendar对象
cal.setTime(new Date() );
cal.add(Calendar.MONTH, 0);//获取当前时间的下一个月
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));//获取下一个月的最后一天
Date preMonth=cal.getTime();//得到下个月的最后一天
String format1 = dateFormat.format(preMonth);
System.out.println(format1);
System.out.println(calculateNowMonthTime()/60/60/24);
} }
} }
package com.jz.dm.common.util; package com.jz.dm.common.util;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.jz.dm.common.constant.LoggingConstants;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
...@@ -30,9 +31,9 @@ public class MapUtil { ...@@ -30,9 +31,9 @@ public class MapUtil {
*/ */
public static String getSignValue(String apiKey,String method,String signType,String timestamp){ public static String getSignValue(String apiKey,String method,String signType,String timestamp){
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("apiKey=").append(apiKey).append("&") builder.append("apiKey=").append(apiKey).append(LoggingConstants.AND_SPILT)
.append("method=").append(method).append("&") .append("method=").append(method).append(LoggingConstants.AND_SPILT)
.append("signType=").append(signType).append("&") .append("signType=").append(signType).append(LoggingConstants.AND_SPILT)
.append("timestamp=").append(timestamp); .append("timestamp=").append(timestamp);
return builder.toString(); return builder.toString();
} }
......
package com.jz.dm.common.util.stream;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import static com.jz.common.utils.HttpClientPool.getHttpClient;
/**
* @author ZC
* @PACKAGE_NAME: com.sentinel.project.util
* @PROJECT_NAME: jz-dm-parent
* @NAME: HttpDownload
* @DATE: 2021-1-7/18:04
* @DAY_NAME_SHORT: 周四
* @Description:
**/
@Slf4j
public class HttpDownload {
public static final int cache = 10 * 1024;
public static final boolean isWindows;
public static final String splash;
public static final String root;
static {
if (System.getProperty("name") != null && System.getProperty("name").toLowerCase().contains("windows")) {
isWindows = true;
splash = "\\";
root = "D:";
} else {
isWindows = false;
splash = "/";
root = "/search";
}
}
/**
* 根据url下载文件,文件名从response header头中获取
*
* @param url
* @return
*/
public static void download(String url) {
download(url, null);
}
/**
* 根据url下载文件,保存到filepath中
*
* @param baseUrl
* @param filepath
* @return
*/
public static void download(String baseUrl, String filepath) {
InputStream is = null;
FileOutputStream fileOut = null;
try {
CloseableHttpClient client = getHttpClient();
HttpGet httpGet = new HttpGet(baseUrl);
HttpResponse response = client.execute(httpGet);
//获取消息头
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
System.out.println(MessageFormat.format("header:{0}={1}", header.getName(), header.getValue()));
}
String fileName = response.getHeaders("Content-Disposition")[0].getValue().split("filename=")[1];
System.out.println("文件名为" + fileName);
HttpEntity entity = response.getEntity();
is = entity.getContent();
if (fileName == null) {
fileName = getFilePath(response);
}
if (filepath != null) {
fileName = filepath + splash + fileName;
}else {
fileName= splash + fileName;
}
File file = new File(fileName);
file.getParentFile().mkdirs();
fileOut = new FileOutputStream(file);
/**
* 根据实际运行效果 设置缓冲区大小
*/
byte[] buffer = new byte[cache];
int ch = 0;
while ((ch = is.read(buffer)) != -1) {
fileOut.write(buffer, 0, ch);
}
fileOut.flush();
log.info("文件下载成功!");
} catch (Exception e) {
log.info("数据下载异常:{}", e.getMessage());
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fileOut) {
try {
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 获取response要下载的文件的默认路径
*
* @param response
* @return
*/
public static String getFilePath(HttpResponse response) {
String filepath = root + splash;
String filename = getFileName(response);
if (filename != null) {
filepath += filename;
} else {
filepath += getRandomFileName();
}
return filepath;
}
/**
* 获取response header中Content-Disposition中的filename值
*
* @param response
* @return
*/
public static String getFileName(HttpResponse response) {
Header contentHeader = response.getFirstHeader("Content-Disposition");
String filename = null;
if (contentHeader != null) {
HeaderElement[] values = contentHeader.getElements();
if (values.length == 1) {
NameValuePair param = values[0].getParameterByName("filename");
if (param != null) {
try {
//filename = new String(param.getValue().toString().getBytes(), "utf-8");
//filename=URLDecoder.decode(param.getValue(),"utf-8");
filename = param.getValue();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return filename;
}
/**
* 获取随机文件名
*
* @return
*/
public static String getRandomFileName() {
return String.valueOf(System.currentTimeMillis());
}
public static void outHeaders(HttpResponse response) {
Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
}
public static void main(String[] args) {
String zip = "http://192.168.1.140:8090/api/download/";
String text = "http://192.168.1.140:8090/api/download/txt";
String docx = "http://192.168.1.140:8090/api/download/docx";
String pdf = "http://192.168.1.140:8090/api/download/pdf";
String xlsx = "http://192.168.1.140:8090/api/download/xlsx";
String filepath = "C:\\Users\\key\\Desktop\\ideaIU-2019.3.3";
HttpDownload.download(xlsx, filepath);
}
}
package com.jz.dm.config;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.config
* @PROJECT_NAME: jz-dm-parent
* @NAME: SentinelConfig
* @DATE: 2021-1-7/15:57
* @DAY_NAME_SHORT: 周四
* @Description: 限流熔断初始化配置
**/
@Configuration
public class SentinelConfig {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
/**
* 默认最大请求数
*/
@Value("${sentinel.request.max}")
private Integer defaultMaxRequest;
/**
* 限流/熔断资源配置
*/
@Value("${sentinel.resource}")
private String limitSource ;
@PostConstruct
private void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource(limitSource);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(defaultMaxRequest); //每秒中可以有多少个请求
rules.add(rule);
FlowRuleManager.loadRules(rules);
//降级规则,可以多个degradeRule rule
//DegradeRuleManager.getRules()可以获取到已经设置的降级规则
List<DegradeRule> degradeRules = new ArrayList<>();
DegradeRule degradeRule = new DegradeRule();
//设置资源名称,sentinel降级都是以资源为单位进行
degradeRule.setResource(limitSource);
//使用异常统计降级,分钟统计,滑动时间窗口
degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
//异常数达到的数量阈值
degradeRule.setCount(defaultMaxRequest);
//秒级时间窗口,该值必须有且必须大于零,否则降级将无法生效
degradeRule.setTimeWindow(1);
degradeRules.add(degradeRule);
//重新加载限流规则,此处将覆盖原有的限流,所以如果想要不覆盖
//请使用DegradeRuleManager.getRules()获取到的加入到rules中
DegradeRuleManager.loadRules(degradeRules);
}
}
package com.jz.dm.filter; package com.jz.dm.filter;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils; import com.jz.common.utils.RedisUtils;
import com.jz.dm.common.constant.Constants; import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.constant.LoggingConstants; import com.jz.dm.common.constant.LoggingConstants;
import com.jz.dm.common.enums.GatewayResultCode; import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.enums.apiInterface.ApiStatusEnum; import com.jz.dm.common.enums.apiInterface.ApiStatusEnum;
import com.jz.dm.common.enums.auth.AuthReqTypeEnum;
import com.jz.dm.common.enums.org.OrgStatusEnum; import com.jz.dm.common.enums.org.OrgStatusEnum;
import com.jz.dm.common.exception.GatewayException; import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.DateUtil; import com.jz.dm.common.util.DateUtil;
...@@ -51,9 +53,10 @@ public class AuthFilter extends AbstractFilter { ...@@ -51,9 +53,10 @@ public class AuthFilter extends AbstractFilter {
private ApiInterfaceService apiInterfaceService; private ApiInterfaceService apiInterfaceService;
@Autowired @Autowired
private RedisUtils redisUtils; private RedisUtils redisUtils;
@Autowired
@Value("${request.limit.max}") private HttpsUtils httpUtils;
private Long limit; @Value("${data.bank.balanceUrl}")
private String balanceUrl;
@Override @Override
public String getFilterName() { public String getFilterName() {
...@@ -124,15 +127,23 @@ public class AuthFilter extends AbstractFilter { ...@@ -124,15 +127,23 @@ public class AuthFilter extends AbstractFilter {
throw new GatewayException(GatewayResultCode.ORG_STATE_EXCEPTION); throw new GatewayException(GatewayResultCode.ORG_STATE_EXCEPTION);
} }
//查询数据银行银行余额是否充足 //查询数据银行银行余额是否充足
getDataAmountResult(request);
try { //记录请求次数(每天限制请求次数) try { //记录请求次数(每天限制请求次数)
String limitKey = request.getApiKey() + LoggingConstants.AND_SPILT + authCode; String limitKey = request.getApiKey() + LoggingConstants.AND_SPILT + authCode;
String reqKey = redisUtils.get(limitKey); String reqKey = redisUtils.get(limitKey);
long timeOut = DateUtil.calculateNowResidueTime(); long timeOut =0;
if (AuthReqTypeEnum.DAY.name().equals(authAuth.getReqType())){ //按天
timeOut = DateUtil.calculateNowResidueTime();
}else if (AuthReqTypeEnum.MONTH.name().equals(authAuth.getReqType())){//按月
timeOut=DateUtil.calculateNowMonthTime();
}else if (AuthReqTypeEnum.YEAR.name().equals(authAuth.getReqType())){//按年
//暂时不支持年
}
if (null != reqKey) { if (null != reqKey) {
Integer value = Integer.valueOf(reqKey); Integer value = Integer.valueOf(reqKey);
if (value > limit) {//超出最大请求次数 if (value > authAuth.getReqFrequency()) {//超出最大请求次数
throw new GatewayException(GatewayResultCode.REQUEST_LIMIT_EXCEPTION); throw new GatewayException(GatewayResultCode.REQUEST_LIMIT_EXCEPTION);
} else if (value <= limit) { } else if (value <= authAuth.getReqFrequency()) {
redisUtils.delAndAdd(limitKey, limitKey, value + 1, timeOut); redisUtils.delAndAdd(limitKey, limitKey, value + 1, timeOut);
} }
} else { } else {
...@@ -160,4 +171,26 @@ public class AuthFilter extends AbstractFilter { ...@@ -160,4 +171,26 @@ public class AuthFilter extends AbstractFilter {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
} }
} }
/**
* 按次调用检查余额是否充足
* @param request
*/
private void getDataAmountResult(GatewayRequest request) {
JSONObject params = JSONObject.parseObject(request.getParams());
if (null != params){
JSONObject pObject = new JSONObject();
pObject.put("assetsId",params.getInteger("assetsId"));
pObject.put("userId",params.getInteger("userId"));
pObject.put("dataPrice",params.getString("dataPrice"));
String respResult = httpUtils.submitPost(balanceUrl, pObject.toString());
JSONObject result = JSONObject.parseObject(respResult);
if (null != result){
if (200 !=result.getInteger("code")){
log.info("~~~~~~~~~~~~~~~~~~异常信息为:{}",result.getString("message"));
throw new GatewayException(GatewayResultCode.CALL_AMOUNT_NOT_ENOUGH);
}
}
}
}
} }
...@@ -41,17 +41,19 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -41,17 +41,19 @@ public class VerifySignFilter extends AbstractFilter {
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { FilterChain chain) {
try { try {
//对签约参数进行字典排序
String signParams = MapUtil.getSignValue(request.getApiKey(),request.getMethod(), String signParams = MapUtil.getSignValue(request.getApiKey(),request.getMethod(),
request.getSignType(),String.valueOf(request.getTimestamp())); request.getSignType(),String.valueOf(request.getTimestamp()));
if (StringUtils.isNotBlank(signParams)){ if (StringUtils.isNotBlank(signParams)){
JSONObject jsonObject = JSONObject.parseObject(request.getParams()); JSONObject jsonObject = JSONObject.parseObject(request.getParams());
String authCode = jsonObject.getString("authCode");
//需要传入授权码 //需要传入授权码
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(), jsonObject.getString("authCode")); ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(),authCode );
if (null == apiAuthInfo){ if (null == apiAuthInfo){
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
} }
String salt = Md5.encrypt(signParams, apiAuthInfo.getSalt()); String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt());
if (!request.getSign().equals(salt)){ if (!request.getSign().equals(sign)){
throw new GatewayException(GatewayResultCode.SIGN_ERROR); throw new GatewayException(GatewayResultCode.SIGN_ERROR);
} }
}else { }else {
......
...@@ -43,6 +43,17 @@ public class ApiAuth extends BaseObject implements Serializable { ...@@ -43,6 +43,17 @@ public class ApiAuth extends BaseObject implements Serializable {
*/ */
@TableField("auth_type") @TableField("auth_type")
private String authType; private String authType;
/**
* 限制类型:DAY 按天,MONTH 按月, YEAR 按年
*/
@TableField("req_type")
private String reqType;
/**
* 限制次数
*/
@TableField("req_frequency")
private Long reqFrequency;
/** /**
* 授权码 * 授权码
......
...@@ -108,6 +108,17 @@ public class ApiInterface extends BaseObject implements Serializable { ...@@ -108,6 +108,17 @@ public class ApiInterface extends BaseObject implements Serializable {
@TableField("sign_type") @TableField("sign_type")
private String signType; private String signType;
/**
* 限制类型:DAY 按天,MONTH 按月, YEAR 按年
*/
@TableField("req_type")
private String reqType;
/**
* 限制次数
*/
@TableField("req_frequency")
private Long reqFrequency;
/** /**
* 超时时间 * 超时时间
*/ */
......
...@@ -64,5 +64,4 @@ public class ApiInterfaceFile extends BaseObject implements Serializable { ...@@ -64,5 +64,4 @@ public class ApiInterfaceFile extends BaseObject implements Serializable {
@TableField("status") @TableField("status")
private String status; private String status;
} }
...@@ -46,6 +46,12 @@ public class ApiInterfaceReq implements Serializable { ...@@ -46,6 +46,12 @@ public class ApiInterfaceReq implements Serializable {
@NotNull(message="超时时间不能为空") @NotNull(message="超时时间不能为空")
public String timeout; public String timeout;
@ApiModelProperty(value = "限制类型:DAY 按天,MONTH 按月, YEAR 按年",required = true)
@NotNull(message = "限制类型不能为空")
public String reqType;
@ApiModelProperty(value = "限制次数",required = true)
public Long reqFrequency;
@ApiModelProperty(value = "api描述",required = false) @ApiModelProperty(value = "api描述",required = false)
public String apiFunction; public String apiFunction;
......
...@@ -40,10 +40,21 @@ public class MakeDataBankApiReq implements Serializable { ...@@ -40,10 +40,21 @@ public class MakeDataBankApiReq implements Serializable {
@NotNull(message = "输出类型不能为空") @NotNull(message = "输出类型不能为空")
public String outputType; public String outputType;
@ApiModelProperty(value = "是否分页:",required = false) @ApiModelProperty(value = "是否分页:",required = true)
//@NotNull(message = "是否分页不能为空") @NotNull(message = "是否分页不能为空")
public Boolean page; public Boolean page;
@ApiModelProperty(value = "限制类型:DAY 按天,MONTH 按月",required = true)
@NotNull(message = "限制类型不能为空")
public String reqType;
@ApiModelProperty(value = "文件id",required = true)
@NotNull(message = "文件id不能为空")
public Long fileId;
@ApiModelProperty(value = "限制次数",required = true)
public Long reqFrequency;
@ApiModelProperty(value = "请求地址",required = false) @ApiModelProperty(value = "请求地址",required = false)
public String apiPath; public String apiPath;
...@@ -74,9 +85,7 @@ public class MakeDataBankApiReq implements Serializable { ...@@ -74,9 +85,7 @@ public class MakeDataBankApiReq implements Serializable {
@ApiModelProperty(value = "api样例",required = false) @ApiModelProperty(value = "api样例",required = false)
public String apiExample; public String apiExample;
@ApiModelProperty(value = "文件id",required = false)
//@NotNull(message = "文件id不能为空")
public Long fileId;
//-------------------------前端无效传参---------------------- //-------------------------前端无效传参----------------------
//@ApiModelProperty(value = "状态",hidden = true) //@ApiModelProperty(value = "状态",hidden = true)
......
...@@ -63,4 +63,11 @@ public interface AuthService { ...@@ -63,4 +63,11 @@ public interface AuthService {
* @return * @return
*/ */
ApiAuth getAuthUser( String authCode,Long apiId); ApiAuth getAuthUser( String authCode,Long apiId);
/**
* 获取API授权信息
* @param authCode
* @return
*/
ApiAuth getAuthInfo( String authCode);
} }
package com.jz.dm.service.impl; package com.jz.dm.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jz.dm.common.enums.apiInterface.ApiStatusEnum;
import com.jz.dm.mapper.ApiAuthMapper; import com.jz.dm.mapper.ApiAuthMapper;
import com.jz.dm.mapper.ApiInterfaceMapper; import com.jz.dm.mapper.ApiInterfaceMapper;
import com.jz.dm.models.domian.ApiAuth; import com.jz.dm.models.domian.ApiAuth;
...@@ -69,6 +70,7 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService { ...@@ -69,6 +70,7 @@ public class ApiInterfaceServiceImpl implements ApiInterfaceService {
public ApiInterface getReqTargetInfo(String apiKey) { public ApiInterface getReqTargetInfo(String apiKey) {
QueryWrapper<ApiInterface> query = new QueryWrapper<>(); QueryWrapper<ApiInterface> query = new QueryWrapper<>();
query.eq("api_key",apiKey); query.eq("api_key",apiKey);
query.eq("status", ApiStatusEnum.ISSUE.name());
query.eq("is_deleted",0); query.eq("is_deleted",0);
return apiInterfaceMapper.selectOne(query); return apiInterfaceMapper.selectOne(query);
} }
......
package com.jz.dm.service.impl; package com.jz.dm.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jz.common.utils.Result; import com.jz.common.utils.Result;
import com.jz.dm.common.enums.GeneralStatusTypeEnum;
import com.jz.dm.config.ReentrantRedisLock; import com.jz.dm.config.ReentrantRedisLock;
import com.jz.dm.mapper.ApiReqLogMapper; import com.jz.dm.mapper.ApiReqLogMapper;
import com.jz.dm.models.domian.ApiReqLog; import com.jz.dm.models.domian.ApiReqLog;
...@@ -78,16 +80,23 @@ public class ApiLogServiceImpl implements ApiLogService { ...@@ -78,16 +80,23 @@ public class ApiLogServiceImpl implements ApiLogService {
@Override @Override
public void updateLog(Long id, JSONObject jsonObject) { public void updateLog(Long id, JSONObject jsonObject) {
try { try {
ApiReqLog apiReqLog = apiReqLogMapper.maxId(id); /*ApiReqLog apiReqLog = apiReqLogMapper.maxId(id);*/
ApiReqLog apiReqLog = apiReqLogMapper.selectById(id);
if (null != apiReqLog){ if (null != apiReqLog){
ApiReqLog reqLog = new ApiReqLog(); UpdateWrapper<ApiReqLog> update = new UpdateWrapper<>();
reqLog.setId(apiReqLog.getId()); if (200 == jsonObject.getInt("code")){
reqLog.setStatus(apiReqLog.getStatus()); update.set("status", GeneralStatusTypeEnum.SUCCEED);
reqLog.setResponseParams(jsonObject.toString());
reqLog.setUpdateDate(new Date());
apiReqLogMapper.updateById(reqLog);
}else { }else {
log.info("id为:{}",id+"-----------------日志信息不存在"); update.set("status", GeneralStatusTypeEnum.FAIL);
}
update.set("response_params",jsonObject.toString());
update.set("update_date",new Date());
update.eq("id",id);
if (apiReqLogMapper.update(null,update) == 0){
log.info("~~~~~~~~~~~更新日志信息失败~~~~~~~~");
}
}else {
log.info("id为:{}",id+"--------------日志信息不存在");
} }
}catch (Exception ex){ }catch (Exception ex){
log.error("更新日志返回信息异常:{}",ex.getMessage()); log.error("更新日志返回信息异常:{}",ex.getMessage());
......
...@@ -189,6 +189,8 @@ public class AuthServiceImpl implements AuthService { ...@@ -189,6 +189,8 @@ public class AuthServiceImpl implements AuthService {
ApiAuth apiAuth = new ApiAuth(); ApiAuth apiAuth = new ApiAuth();
apiAuth.setApiInterfaceId(apiInterface.getId()); apiAuth.setApiInterfaceId(apiInterface.getId());
apiAuth.setApiOrgId(apiOrg.getId()); apiAuth.setApiOrgId(apiOrg.getId());
apiAuth.setReqType(apiInterface.getReqType());
apiAuth.setReqFrequency(apiInterface.getReqFrequency());
BeanUtils.copyProperties(req, apiAuth); BeanUtils.copyProperties(req, apiAuth);
//商城只有外部授权 //商城只有外部授权
String authCode =""; String authCode ="";
...@@ -274,8 +276,8 @@ public class AuthServiceImpl implements AuthService { ...@@ -274,8 +276,8 @@ public class AuthServiceImpl implements AuthService {
String salt = RandomUtil.getStringRandom(8); String salt = RandomUtil.getStringRandom(8);
UpdateWrapper<ApiAuth> update = new UpdateWrapper<>(); UpdateWrapper<ApiAuth> update = new UpdateWrapper<>();
update.set("salt", salt); update.set("salt", salt);
update.set("create_date", new Date()); update.set("update_date", new Date());
update.set("create_user", ""); update.set("update_user", "");
update.eq("id", apiAuth.getId()); update.eq("id", apiAuth.getId());
if (apiAuthMapper.update(null, update) > 0) { if (apiAuthMapper.update(null, update) > 0) {
response.setSalt(salt); response.setSalt(salt);
...@@ -299,6 +301,18 @@ public class AuthServiceImpl implements AuthService { ...@@ -299,6 +301,18 @@ public class AuthServiceImpl implements AuthService {
query.eq("is_deleted",0); query.eq("is_deleted",0);
return apiAuthMapper.selectOne(query); return apiAuthMapper.selectOne(query);
} }
/**
* 获取API授权信息
* @param authCode
* @return
*/
@Override
public ApiAuth getAuthInfo(String authCode) {
QueryWrapper<ApiAuth> query = new QueryWrapper<>();
query.eq("auth_code",authCode);
query.eq("is_deleted",0);
return apiAuthMapper.selectOne(query);
}
private ApiInterface getInterface(String apiKey) { private ApiInterface getInterface(String apiKey) {
QueryWrapper<ApiInterface> queryInface = new QueryWrapper<>(); QueryWrapper<ApiInterface> queryInface = new QueryWrapper<>();
......
...@@ -38,6 +38,7 @@ import javax.annotation.Resource; ...@@ -38,6 +38,7 @@ import javax.annotation.Resource;
import java.util.*; import java.util.*;
import static com.jz.dm.common.enums.apiInterface.ApiStatusEnum.fromApiStatusName; import static com.jz.dm.common.enums.apiInterface.ApiStatusEnum.fromApiStatusName;
import static com.jz.dm.common.enums.auth.AuthReqTypeEnum.fromAuthReqTypeEnum;
/** /**
* @author ZC * @author ZC
...@@ -133,6 +134,11 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -133,6 +134,11 @@ public class ProducerServiceImpl implements ProducerService {
!req.getOutputType().contains(ApiInfoOutTypeEnum.FLOW.name()) )){ !req.getOutputType().contains(ApiInfoOutTypeEnum.FLOW.name()) )){
return Result.of_error("输出类型不存在!"); return Result.of_error("输出类型不存在!");
} }
if(StringUtils.isNotBlank(req.getReqType()) ){
if (null == fromAuthReqTypeEnum(req.getReqType())){
return Result.of_error("限制类型不存在!");
}
}
String apiKey =""; String apiKey ="";
if (StringUtils.isNotBlank(req.getJoinType()) if (StringUtils.isNotBlank(req.getJoinType())
&& "10006".equals(req.getJoinType())) {//数据银行制作API && "10006".equals(req.getJoinType())) {//数据银行制作API
...@@ -263,6 +269,11 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -263,6 +269,11 @@ public class ProducerServiceImpl implements ProducerService {
if (null == currentUser) { if (null == currentUser) {
return Result.of_error("用户信息不存在"); return Result.of_error("用户信息不存在");
} }
if(StringUtils.isNotBlank(req.getReqType()) ){
if (null == fromAuthReqTypeEnum(req.getReqType())){
return Result.of_error("限制类型不存在!");
}
}
QueryWrapper<ApiInterfaceFile> query = new QueryWrapper<>(); QueryWrapper<ApiInterfaceFile> query = new QueryWrapper<>();
query.eq("project_id", req.getProjectId()); query.eq("project_id", req.getProjectId());
query.eq("parent_id", req.getParentId()); query.eq("parent_id", req.getParentId());
...@@ -329,6 +340,11 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -329,6 +340,11 @@ public class ProducerServiceImpl implements ProducerService {
if (StringUtils.isBlank(req.getSignType()) || "0".equals(req.getSignType())) { if (StringUtils.isBlank(req.getSignType()) || "0".equals(req.getSignType())) {
return Result.of_error("加密类型错误!"); return Result.of_error("加密类型错误!");
} }
if(StringUtils.isNotBlank(req.getReqType()) ){
if (null == fromAuthReqTypeEnum(req.getReqType())){
return Result.of_error("限制类型不存在!");
}
}
apiInterface.setApiType(req.getApiType()); apiInterface.setApiType(req.getApiType());
apiInterface.setApiDesc(req.getApiDesc()); apiInterface.setApiDesc(req.getApiDesc());
apiInterface.setJoinType(req.getJoinType()); apiInterface.setJoinType(req.getJoinType());
......
package com.jz.dm.service.request;
import com.jz.common.constant.ResultMsg;
import com.jz.common.utils.Result;
import com.jz.dm.common.enums.GeneralStatusTypeEnum;
import com.jz.dm.common.enums.apiInterface.ApiStatusEnum;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiParamVerify
* @DATE: 2021-1-9/15:53
* @DAY_NAME_SHORT: 周六
* @Description: api参数验证
**/
public class ApiParamVerify {
/**
* 校验Api信息
* @param apiInterface
* @return
*/
public Result verifyApiInterface(ApiInterface apiInterface){
if (null == apiInterface){
throw new GatewayException("300",ResultMsg.DATA_NOT_FOUND.getMsg());
}
if (!ApiStatusEnum.ISSUE.name().equals(apiInterface.getStatus())){
throw new GatewayException("300","API未发布!");
}
if (1 == apiInterface.getIsDeleted()){
throw new GatewayException("300","API不存在!");
}
return Result.of_success(ResultMsg.SUCCESS);
}
/**
* 校验授权
* @param apiAuth
* @return
*/
public Result verifyAuth(ApiAuth apiAuth){
if (null == apiAuth){
throw new GatewayException("300",ResultMsg.DATA_NOT_FOUND.getMsg());
}
if (!GeneralStatusTypeEnum.VALID.name().equals(apiAuth.getStatus())){
throw new GatewayException("300","无效授权!");
}
if (1 == apiAuth.getIsDeleted()){
throw new GatewayException("300","授权信息无效!");
}
return Result.of_success(ResultMsg.SUCCESS);
}
}
package com.jz.dm.service.request; package com.jz.dm.service.request;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils; import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils; 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.GatewayResultCode;
import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum; 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.exception.GatewayException;
import com.jz.dm.common.util.OpenApiRequest; import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse; 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.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface; import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.service.ApiInterfaceService; import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.service.AuthService;
import com.jz.dm.web.annotation.AccessLimit; import com.jz.dm.web.annotation.AccessLimit;
import com.jz.dm.web.annotation.ApiLogAspect; import com.jz.dm.web.annotation.ApiLogAspect;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -33,7 +40,8 @@ import java.util.Map; ...@@ -33,7 +40,8 @@ import java.util.Map;
**/ **/
@Service("apiQueryService") @Service("apiQueryService")
@Slf4j @Slf4j
public class ApiQueryService implements OpenApiService { public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Override @Override
public String getOpenApiMethod() { public String getOpenApiMethod() {
return "query"; return "query";
...@@ -47,94 +55,66 @@ public class ApiQueryService implements OpenApiService { ...@@ -47,94 +55,66 @@ public class ApiQueryService implements OpenApiService {
@Autowired @Autowired
private ApiInterfaceService apiInterfaceService; private ApiInterfaceService apiInterfaceService;
@Autowired @Autowired
private AuthService authService;
@Autowired
private RedisUtils redisUtils; private RedisUtils redisUtils;
@Autowired @Autowired
private HttpsUtils httpsUtils; private HttpsUtils httpsUtils;
@Value("${data.bank.withholdUrl}")
private String withholdUrl;
/**
* API请求逻辑处理
*
* @param request
* @param response
*/
@Override @Override
@ApiLogAspect(description = "API请求日志") @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.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void doService(OpenApiRequest request, OpenApiResponse response) { public void doService(OpenApiRequest request, OpenApiResponse response) {
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams()); JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
String selectType = parameter.getString("selectType"); String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
verifyApiInterface(apiInterfaceService.getApiInfo(request.getApiKey()));
ApiAuth apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
String reqParams = parameter.getString("reqParams"); String reqParams = parameter.getString("reqParams");
Map paramMap =null; Map paramMap = null;
if (null !=reqParams && reqParams.length() > 0) { if (StringUtils.isNotBlank(reqParams)) {
paramMap = (Map) JSONObject.parseObject(reqParams); paramMap = (Map) JSONObject.parseObject(reqParams);
} }
String apiInterface = redisUtils.get(request.getApiKey()); //取出缓存数据
if (StringUtils.isNotBlank(selectType)) { String redisReqParam = redisUtils.get(request.getApiKey());
boolean bResult = false;
try { try {
switch (selectType) { if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
case "10001": //实时接入查询 //解析出API制作成功时的参数配置
break; JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
case "10003": //标签查询
break;
case "10002"://大数据查询
if (StringUtils.isNotBlank(apiInterface)){//redis中存在
JSONObject jsonBigData = JSONObject.parseObject(apiInterface);
String targetUrl = jsonBigData.getString("targetUrl");
rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(),targetUrl,paramMap,response);
}else { //缓存中不存在 查询数据库
ApiInterface apiInfo= apiInterfaceService.getApiInfo(request.getApiKey());
if (apiInfo == null){
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(),apiInfo.getTargetUrl(),paramMap,response);
}
break;
case "10004": //自定义查询(三方查询)
if (StringUtils.isNotBlank(apiInterface)){//redis中存在
JSONObject jsonCustom = JSONObject.parseObject(apiInterface);
String targetUrl = jsonCustom.getString("targetUrl");
rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(),targetUrl,null,response);
}else {//redis中不存在,查询数据库
ApiInterface apiReqDto =
apiInterfaceService.getReqTargetInfo(request.getApiKey());
if (null == apiReqDto) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
rangRequestTarget(ApiInfoOutTypeEnum.JSON.name(),apiReqDto.getTargetUrl(),null,response);
}
break;
case "10006": //数据银行查询
if (StringUtils.isNotBlank(apiInterface)) {//redis中存在
JSONObject jsonObject = JSONObject.parseObject(apiInterface);
String targetUrl = jsonObject.getString("targetUrl"); String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType"); String outputType = jsonObject.getString("outputType");
rangRequestTarget(outputType, targetUrl, paramMap, response); bResult = rangRequestTarget(outputType, targetUrl, paramMap, response);
} else {//不存在查询数据库 } else {//不存在查询数据库
ApiInterface apiReqDto = ApiInterface apiInterface =
apiInterfaceService.getReqTargetInfo(request.getApiKey());
if (null == apiReqDto) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
}
rangRequestTarget(apiReqDto.getOutputType(), apiReqDto.getTargetUrl(),paramMap,response);
}
break;
case "10007": //数据银行数据包查询
if (StringUtils.isNotBlank(apiInterface)){//从redis中获取数据
JSONObject jsonObject = JSONObject.parseObject(apiInterface);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
rangRequestTarget(outputType, targetUrl, paramMap, response);
}else {//不存在查询数据库
ApiInterface apiReqDto =
apiInterfaceService.getReqTargetInfo(request.getApiKey()); apiInterfaceService.getReqTargetInfo(request.getApiKey());
if (null == apiReqDto) { if (null == apiInterface) {
throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST); throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
} }
rangRequestTarget(apiReqDto.getOutputType(), apiReqDto.getTargetUrl(),paramMap,response); bResult = rangRequestTarget(apiInterface.getOutputType(), apiInterface.getTargetUrl(), paramMap, response);
} }
break; //调用成功请求数据银行扣款
default: if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())){
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); notifierMinusMoney(parameter, bResult);
} }
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof GatewayException){ if (ex instanceof GatewayException) {
throw(GatewayException) ex; throw (GatewayException) ex;
} }
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~"); log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}", ex.getMessage()); log.error("异常信息:{}", ex.getMessage());
...@@ -142,17 +122,88 @@ public class ApiQueryService implements OpenApiService { ...@@ -142,17 +122,88 @@ public class ApiQueryService implements OpenApiService {
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg()); response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
} }
} }
}
private void rangRequestTarget(String outputType, String targetUrl, Map<String, String> param, OpenApiResponse response) {
/**
* 远程请求获取数据
*
* @param outputType
* @param targetUrl
* @param param
* @param response
*/
private boolean rangRequestTarget(String outputType, String targetUrl,
Map<String, String> param, OpenApiResponse response) {
if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name();
}
if (StringUtils.isBlank(targetUrl)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求 if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
// httpsUtils.doGetImg()TODO 流形式未实现 StringBuilder builder = new StringBuilder();
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { builder.append(targetUrl).append("?");
for (Map.Entry<String, String> entry : param.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
builder.append(key).append(LoggingConstants.AND_EQUAL)
.append(value).append(LoggingConstants.AND_SPILT);
}
String baseUrl = builder.substring(0, builder.length() - 1);
HttpDownload.download(baseUrl);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
response.setAttribute("调用成功!"); response.setAttribute("调用成功!");
/*String respResult = httpsUtils.doGet(targetUrl, param); String respResult = httpsUtils.doGet(targetUrl, param);
response.setAttribute(respResult);*/ JSONObject resp = JSONObject.parseObject(respResult);
if (null != resp && 200 == resp.getInteger("code")) {
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute(resp.getString("data"));
return true;
} else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
} else { } else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION); throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_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);//调用成功 扣款 false 调用失败,解冻金额
String responseResult = httpsUtils.submitPost(withholdUrl, 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);
}
}
}
/**
* 限流返回方法
*
* @param request
* @param response
*/
public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) {
log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey());
throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
}
} }
...@@ -4,7 +4,6 @@ package com.jz.dm.web.aspect; ...@@ -4,7 +4,6 @@ package com.jz.dm.web.aspect;
import com.jz.common.utils.IpUtils; import com.jz.common.utils.IpUtils;
import com.jz.common.utils.JsonUtils; import com.jz.common.utils.JsonUtils;
import com.jz.common.utils.UrlUtil; import com.jz.common.utils.UrlUtil;
import com.jz.dm.common.enums.SignType;
import com.jz.dm.mapper.ApiReqLogMapper; import com.jz.dm.mapper.ApiReqLogMapper;
import com.jz.dm.models.domian.ApiReqLog; import com.jz.dm.models.domian.ApiReqLog;
import com.jz.dm.service.ApiLogService; import com.jz.dm.service.ApiLogService;
...@@ -17,7 +16,9 @@ import lombok.extern.slf4j.Slf4j; ...@@ -17,7 +16,9 @@ import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
...@@ -36,7 +37,7 @@ import java.util.Map; ...@@ -36,7 +37,7 @@ import java.util.Map;
* @NAME: SystemLogAspect * @NAME: SystemLogAspect
* @DATE: 2020-12-25/16:03 * @DATE: 2020-12-25/16:03
* @DAY_NAME_SHORT: 周五 * @DAY_NAME_SHORT: 周五
* @Description: * @Description: 日志收集
**/ **/
@Aspect @Aspect
...@@ -85,28 +86,30 @@ public class SystemLogAspect { ...@@ -85,28 +86,30 @@ public class SystemLogAspect {
log.info("around请求参数为{}", joinPoint.getArgs()); log.info("around请求参数为{}", joinPoint.getArgs());
//动态修改其参数 //动态修改其参数
//注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args) //注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args)
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(params);
String paramsList = jsonObject.getString("params");
com.alibaba.fastjson.JSONObject jsonParamsList = com.alibaba.fastjson.JSONObject.parseObject(paramsList);
try { try {
ApiReqLog reqLog = new ApiReqLog(); ApiReqLog reqLog = new ApiReqLog();
reqLog.setRequestIp(ip); reqLog.setRequestIp(ip);
// reqLog.setApiKey(jsonObject.getString("apiKey")); reqLog.setApiKey(jsonObject.getString("apiKey"));
reqLog.setRequestParams(params); reqLog.setRequestParams(params);
reqLog.setResponseParams("");//响应参数 reqLog.setResponseParams("");//响应参数
reqLog.setRequestUrl(url); reqLog.setRequestUrl(url);
reqLog.setRequestMethod(contextPath); reqLog.setRequestMethod(contextPath);
reqLog.setEncryMode(SignType.MD5.name()); //加密方式:MD5,RSA reqLog.setEncryMode(jsonObject.getString("signType")); //加密方式:MD5,RSA
reqLog.setTransMode(request.getMethod());//传输方式 GET POST reqLog.setTransMode(request.getMethod());//传输方式 GET POST
// reqLog.setRequestToken(token); reqLog.setRequestToken(jsonParamsList.getString("authCode"));
reqLog.setRequestTime(new Date()); reqLog.setRequestTime(new Date());
reqLog.setRemark(getServiceMethodDescription(joinPoint)); reqLog.setRemark(getServiceMethodDescription(joinPoint));
System.out.println(reqLog); log.info("请求参数:",reqLog);
if (null != reqLog) { if (null != reqLog) {
apiReqLogMapper.insert(reqLog); apiReqLogMapper.insert(reqLog);
} }
Object result = joinPoint.proceed(joinPoint.getArgs()); Object result = joinPoint.proceed(joinPoint.getArgs());
JSONObject jsonObject = JSONObject.fromObject(result); JSONObject jsonResult = JSONObject.fromObject(result);
log.info("around响应结果为{}", jsonObject); log.info("around响应结果为{}", jsonResult);
apiLogService.updateLog(reqLog.getId(), jsonObject); apiLogService.updateLog(reqLog.getId(), jsonResult);
} catch (Throwable throwable) { } catch (Throwable throwable) {
throwable.printStackTrace(); throwable.printStackTrace();
} }
......
package com.jz.dm.web.interceptor; package com.jz.dm.web.interceptor;
import com.jz.common.utils.IpUtils; import com.jz.common.utils.IpUtils;
import com.jz.common.utils.RedisUtils;
import com.jz.dm.web.annotation.AccessLimit; import com.jz.dm.web.annotation.AccessLimit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
...@@ -19,13 +21,12 @@ import java.lang.reflect.Method; ...@@ -19,13 +21,12 @@ import java.lang.reflect.Method;
* @NAME: AccessLimitInterceptor * @NAME: AccessLimitInterceptor
* @DATE: 2020-12-26/17:39 * @DATE: 2020-12-26/17:39
* @DAY_NAME_SHORT: 周六 * @DAY_NAME_SHORT: 周六
* @Description: * @Description: 使用redis限流
**/ **/
public class AccessLimitInterceptor implements HandlerInterceptor { public class AccessLimitInterceptor implements HandlerInterceptor {
//使用RedisTemplate操作redis @Autowired
/* @Autowired private RedisUtils redisUtils;
private RedisTemplate<String, Integer> redisTemplate;*/
@Override @Override
public boolean preHandle(HttpServletRequest request, public boolean preHandle(HttpServletRequest request,
...@@ -44,16 +45,17 @@ public class AccessLimitInterceptor implements HandlerInterceptor { ...@@ -44,16 +45,17 @@ public class AccessLimitInterceptor implements HandlerInterceptor {
int limit = accessLimit.limit(); int limit = accessLimit.limit();
int sec = accessLimit.sec(); int sec = accessLimit.sec();
String key = IpUtils.getIpAddr(request) + request.getRequestURI(); String key = IpUtils.getIpAddr(request) + request.getRequestURI();
/*Integer maxLimit = redisTemplate.opsForValue().get(key); String result = redisUtils.get(key);
Integer maxLimit = Integer.valueOf(result);
if (maxLimit == null) { if (maxLimit == null) {
//set时一定要加过期时间 //set时一定要加过期时间
redisTemplate.opsForValue().set(key, 1, sec, TimeUnit.SECONDS); redisUtils.set(key,1,sec);
} else if (maxLimit < limit) { } else if (maxLimit < limit) {
redisTemplate.opsForValue().set(key, maxLimit + 1, sec, TimeUnit.SECONDS); redisUtils.set(key, maxLimit + 1, sec);
} else { } else {
output(response, "请求太频繁~!"); output(response, "请求太频繁~!");
return false; return false;
}*/ }
} }
return true; return true;
} }
......
...@@ -77,6 +77,22 @@ dmp: ...@@ -77,6 +77,22 @@ dmp:
timeout: timeout:
default: 5000 default: 5000
max: 5000 max: 5000
request: #api请求次数
limit: #流处理配置
max: 10 windows: #文件流输出地址设置
name: stream
isWindows: true
#sentinel限流配置
sentinel: #sentinel每秒中最大的请求个数
resource: api.gateway
request:
max: 100
#数据银行调用配置
data:
bank:
balanceUrl: http://localhost:8081/mall/financeCustomerAssets/findAssets #余额信息url
withholdUrl: http://localhost:8081/mall/financeCustomerAssets/unfreezeMoney #扣款信通知url
...@@ -19,7 +19,21 @@ spring: ...@@ -19,7 +19,21 @@ spring:
max-request-size: 50MB max-request-size: 50MB
profiles: profiles:
active: test #默认使用的配置文件 active: test #默认使用的配置文件
cloud:
sentinel: #sentinel 限流框架配置
transport:
# 项目本地会启动一个服务的端口号,默认8719,用于与sentinel-dashboard通讯
port: 8719
# sentinel-dashboard服务地址 # sentinel-dashboard-1.6.0 的访问路径 ,启动方式java -jar sentinel-dashboard-1.6.0.jar
dashboard: localhost:9090
filter:
# 需要进行限流监控的接口,多个匹配用逗号隔开
url-patterns: /*
servlet:
# 触发限流后重定向的页面
block-page: /sentinel/block
#取消Sentinel控制台懒加载
#eager: true
# ====================MybatisPlus==================== # ====================MybatisPlus====================
mybatis-plus: mybatis-plus:
......
package com.jz.dm.gateway.api;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.dm.common.util.MapUtil;
import com.jz.dm.common.util.Md5;
import com.jz.dm.gateway.SpringTestCase;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.api
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiReqTest
* @DATE: 2021-1-6/14:13
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public class ApiReqTest extends SpringTestCase {
public static final String url ="";
@Autowired
private HttpsUtils httpsUtils;
@Test
public void TestGatewayReq() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("apiKey", "9jNA1W5Ydn7Stp12");
jsonObject.put("method", "query");
jsonObject.put("signType", "MD5");
long time = System.currentTimeMillis();
jsonObject.put("timestamp", "1609919983045");
JSONObject params = new JSONObject();
params.put("authCode", "20200000000221231111455Jg8ug1621");
params.put("selectType", "10006");
params.put("reqParams", new JSONObject());
jsonObject.put("params",params);
try {
String apiKey = jsonObject.getString("apiKey");
String method = jsonObject.getString("method");
String signType = jsonObject.getString("signType");
String timestamp= jsonObject.getString("timestamp");
String signature = MapUtil.getSignValue(apiKey, method, signType, timestamp);
String salt = Md5.encrypt(signature, "OOmeF1gh");
jsonObject.put("sign", salt);
String response = httpsUtils.submitPost(url, jsonObject.toString());
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
...@@ -10,7 +10,6 @@ import com.jz.dm.models.req.organizationManage.OrganizationManageDetailQueryReq; ...@@ -10,7 +10,6 @@ import com.jz.dm.models.req.organizationManage.OrganizationManageDetailQueryReq;
import com.jz.dm.models.req.organizationManage.OrganizationManageListQueryReq; import com.jz.dm.models.req.organizationManage.OrganizationManageListQueryReq;
import com.jz.dm.models.req.organizationManage.OrganizationManageUpdateReq; import com.jz.dm.models.req.organizationManage.OrganizationManageUpdateReq;
import com.jz.dm.service.OrganizationManageService; import com.jz.dm.service.OrganizationManageService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
/** /**
...@@ -30,7 +29,7 @@ public class TestOrganizationManage extends SpringTestCase { ...@@ -30,7 +29,7 @@ public class TestOrganizationManage extends SpringTestCase {
/** /**
* 查询类列表信息 * 查询类列表信息
*/ */
@Test // @Test
public void getOrgList(){ public void getOrgList(){
OrganizationManageListQueryReq req = new OrganizationManageListQueryReq(); OrganizationManageListQueryReq req = new OrganizationManageListQueryReq();
req.setPageNum(1); req.setPageNum(1);
...@@ -41,7 +40,7 @@ public class TestOrganizationManage extends SpringTestCase { ...@@ -41,7 +40,7 @@ public class TestOrganizationManage extends SpringTestCase {
/** /**
* 查询详情信息 * 查询详情信息
*/ */
@Test // @Test
public void getOrgDetail(){ public void getOrgDetail(){
OrganizationManageDetailQueryReq req = new OrganizationManageDetailQueryReq(); OrganizationManageDetailQueryReq req = new OrganizationManageDetailQueryReq();
req.setId(1L); req.setId(1L);
...@@ -52,7 +51,7 @@ public class TestOrganizationManage extends SpringTestCase { ...@@ -52,7 +51,7 @@ public class TestOrganizationManage extends SpringTestCase {
/** /**
* 添加组织信息 * 添加组织信息
*/ */
@Test //@Test
public void addOrg(){ public void addOrg(){
OrganizationManageAddReq req = new OrganizationManageAddReq(); OrganizationManageAddReq req = new OrganizationManageAddReq();
req.setOrgName("数据银行"); req.setOrgName("数据银行");
...@@ -69,7 +68,7 @@ public class TestOrganizationManage extends SpringTestCase { ...@@ -69,7 +68,7 @@ public class TestOrganizationManage extends SpringTestCase {
/** /**
* 更新组织信息 * 更新组织信息
*/ */
@Test //@Test
public void updateOrg(){ public void updateOrg(){
OrganizationManageUpdateReq req = new OrganizationManageUpdateReq(); OrganizationManageUpdateReq req = new OrganizationManageUpdateReq();
req.setId(8L); req.setId(8L);
......
package com.jz.dm.gateway.orther;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.gateway.SpringTestCase;
import org.junit.jupiter.api.Test;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.orther
* @PROJECT_NAME: jz-dm-parent
* @NAME: TestAmountCall
* @DATE: 2021-1-9/15:04
* @DAY_NAME_SHORT: 周六
* @Description:
**/
public class TestAmountCall extends SpringTestCase {
private static final String baseUrl ="http://192.168.1.139:8081/mall/financeCustomerAssets/findAssets";
private static final String baseUrl2 ="http://192.168.1.139:8081/mall/financeCustomerAssets/unfreezeMoney";
/* @Autowired
private HttpsUtils httpUtils;*/
@Test
public void TestDataBankReq(){
HttpsUtils utils = new HttpsUtils();
JSONObject jsonObject = new JSONObject();
jsonObject.put("assetsId",1);
jsonObject.put("userId",1);
jsonObject.put("dataPrice","50");
String respResult = utils.submitPost(baseUrl, jsonObject.toString());
JSONObject paramsResult = JSONObject.parseObject(respResult);
if (null != paramsResult){
Integer code = paramsResult.getInteger("code");
if (200 != code){
System.out.println("~~~~~~~~~~~~~~~~~~单次调用金额不足~~~~~~~~~~~~~~~");
System.out.println(respResult);
throw new GatewayException(GatewayResultCode.CALL_AMOUNT_NOT_ENOUGH);
}else {
System.out.println(paramsResult.getString("message"));
}
}
}
/**
* 通知扣款
*/
@Test
public void notifierDataBankReq(){
HttpsUtils utils = new HttpsUtils();
JSONObject jsonObject = new JSONObject();
jsonObject.put("assetsId",1);
jsonObject.put("userId",1);
jsonObject.put("dataPrice","50");
jsonObject.put("callStatus",true);//调用成功 扣款 false 调用失败,解冻金额
String respResult = utils.submitPost(baseUrl2, jsonObject.toString());
JSONObject paramsResult = JSONObject.parseObject(respResult);
if (null != paramsResult){
Integer code = paramsResult.getInteger("code");
if (200 != code){
System.out.println("~~~~~~~~~~~~~~~~~~操作失败~~~~~~~~~~~~~~~");
System.out.println(respResult);
throw new GatewayException(GatewayResultCode.CALL_AMOUNT_NOT_ENOUGH);
}else {
System.out.println(paramsResult.getString("message"));
}
}
}
}
...@@ -3,7 +3,7 @@ package com.jz.dm.gateway.orther; ...@@ -3,7 +3,7 @@ package com.jz.dm.gateway.orther;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils; import com.jz.common.utils.HttpsUtils;
import com.jz.dm.gateway.SpringTestCase; import com.jz.dm.gateway.SpringTestCase;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap; import java.util.HashMap;
......
...@@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils; import com.jz.common.utils.HttpsUtils;
import com.jz.dm.gateway.SpringTestCase; import com.jz.dm.gateway.SpringTestCase;
import com.jz.dm.service.ProducerService; import com.jz.dm.service.ProducerService;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap; import java.util.HashMap;
......
...@@ -2,7 +2,7 @@ package com.jz.dm.gateway.orther; ...@@ -2,7 +2,7 @@ package com.jz.dm.gateway.orther;
import com.jz.dm.common.base.BaseCurrentUserObject; import com.jz.dm.common.base.BaseCurrentUserObject;
import com.jz.dm.gateway.SpringTestCase; import com.jz.dm.gateway.SpringTestCase;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
......
package com.jz.dm.gateway.orther;
import com.jz.dm.gateway.SpringTestCase;
import lombok.extern.slf4j.Slf4j;
import static com.jz.dm.common.util.stream.HttpDownload.download;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.orther
* @PROJECT_NAME: jz-dm-parent
* @NAME: TestStreamReq
* @DATE: 2021-1-7/16:51
* @DAY_NAME_SHORT: 周四
* @Description:
**/
@Slf4j
public class TestStreamReq extends SpringTestCase {
private static final String baseUrl ="http://192.168.1.140:8090/api/download";
//@Test
public void testStreamReq() {
download(baseUrl);
}
}
...@@ -195,15 +195,16 @@ public class HttpsUtils { ...@@ -195,15 +195,16 @@ public class HttpsUtils {
* @param url * @param url
* @throws IOException * @throws IOException
*/ */
public CloseableHttpClient doGetImg(String url) { public CloseableHttpResponse doGetImg(String url) {
CloseableHttpClient httpClient = getHttpClient(); CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response=null;
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
try { try {
httpClient.execute(httpGet); response = httpClient.execute(httpGet);
} catch (IOException e) { } catch (IOException e) {
log.error("发起http request异常:{}", e); log.error("发起http request异常:{}", e);
} }
return httpClient; return response;
} }
/** /**
......
package com.jz.manage; package com.jz.manage;
import com.jz.common.utils.DateUtils;
import com.jz.manage.moduls.controller.goods.bean.request.DataApiUpReq;
import com.jz.manage.moduls.entity.DataGoodsApiParams;
import com.jz.manage.moduls.service.DataGoodsService; import com.jz.manage.moduls.service.DataGoodsService;
import com.jz.manage.moduls.service.SysUserService; import com.jz.manage.moduls.service.SysUserService;
import com.jz.manage.moduls.service.TestUserService; import com.jz.manage.moduls.service.TestUserService;
...@@ -12,10 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -12,10 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class manageApplicationTests { public class manageApplicationTests {
...@@ -38,29 +31,29 @@ public class manageApplicationTests { ...@@ -38,29 +31,29 @@ public class manageApplicationTests {
@Test @Test
public void getUserRoleByAccount() throws Exception { public void getUserRoleByAccount() throws Exception {
DataApiUpReq dataApiUpReq = new DataApiUpReq(); //DataApiUpReq dataApiUpReq = new DataApiUpReq();
dataApiUpReq.setApiName("华为"); //dataApiUpReq.setApiName("华为");
dataApiUpReq.setRequestType("的撒旦"); //dataApiUpReq.setRequestType("的撒旦");
dataApiUpReq.setApiUrl("http://localhost/dataGoods/uploadApi"); //dataApiUpReq.setApiUrl("http://localhost/dataGoods/uploadApi");
dataApiUpReq.setReturnDataExample("dadasdadafaf"); //dataApiUpReq.setReturnDataExample("dadasdadafaf");
dataApiUpReq.setApiKey("5"); //dataApiUpReq.setApiKey("5");
dataApiUpReq.setEncryMode("MD5"); //dataApiUpReq.setEncryMode("MD5");
dataApiUpReq.setTransMode("http"); //dataApiUpReq.setTransMode("http");
dataApiUpReq.setApiFunction("fafafaffaf"); //dataApiUpReq.setApiFunction("fafafaffaf");
dataApiUpReq.setVersionNumber("1.0"); //dataApiUpReq.setVersionNumber("1.0");
dataApiUpReq.setPriceType("01"); //dataApiUpReq.setPriceType("01");
dataApiUpReq.setApiType("1"); //dataApiUpReq.setApiType("1");
dataApiUpReq.setApiDesc("fafsafafasff"); //dataApiUpReq.setApiDesc("fafsafafasff");
dataApiUpReq.setDataPicture("http://wx4.sinaimg.cn/bmiddle/006GJQvhly1gkx4ezwcr4j328g28g1kx.jpg"); //dataApiUpReq.setDataPicture("http://wx4.sinaimg.cn/bmiddle/006GJQvhly1gkx4ezwcr4j328g28g1kx.jpg");
dataApiUpReq.setYearType(BigDecimal.valueOf(1)); //dataApiUpReq.setYearType(BigDecimal.valueOf(1));
List<DataGoodsApiParams> paramsList = new ArrayList<>(); //List<DataGoodsApiParams> paramsList = new ArrayList<>();
for (DataGoodsApiParams params : paramsList) { //for (DataGoodsApiParams params : paramsList) {
params.setParamsDiff("01"); // params.setParamsDiff("01");
params.setParamsName("xiaomi"); // params.setParamsName("xiaomi");
params.setParamsType("哒哒哒"); // params.setParamsType("哒哒哒");
params.setCreTime(DateUtils.getToday()); // params.setCreTime(DateUtils.getToday());
} //}
dataApiUpReq.setParamsList(paramsList); //dataApiUpReq.setParamsList(paramsList);
dataGoodsService.uploadApi(dataApiUpReq); //dataGoodsService.uploadApi(dataApiUpReq);
} }
} }
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
<springfox-swagger2.version>2.9.2</springfox-swagger2.version> <springfox-swagger2.version>2.9.2</springfox-swagger2.version>
<swagger-bootstrap-ui.version>1.9.6</swagger-bootstrap-ui.version> <swagger-bootstrap-ui.version>1.9.6</swagger-bootstrap-ui.version>
<lombok.version>1.18.16</lombok.version> <lombok.version>1.18.16</lombok.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
...@@ -66,6 +67,22 @@ ...@@ -66,6 +67,22 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- fastjson --> <!-- fastjson -->
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
...@@ -220,58 +237,7 @@ ...@@ -220,58 +237,7 @@
<module>jz-dm-apigateway</module> <module>jz-dm-apigateway</module>
</modules> </modules>
<!-- <build>-->
<!-- <defaultGoal>compile</defaultGoal>-->
<!-- <finalName>${project.artifactId}</finalName>-->
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>src/main/resources</directory>-->
<!-- <filtering>true</filtering>-->
<!-- <excludes>-->
<!-- <exclude>disconf.properties</exclude>-->
<!-- </excludes>-->
<!-- </resource>-->
<!-- </resources>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <version>2.3.2</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>default-compile</id>-->
<!-- <phase>compile</phase>-->
<!-- <goals>-->
<!-- <goal>compile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-resources-plugin</artifactId>-->
<!-- <version>2.5</version>-->
<!-- <configuration>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-eclipse-plugin</artifactId>-->
<!-- <version>2.8</version>-->
<!-- <configuration>-->
<!-- <downloadSources>true</downloadSources>-->
<!-- <downloadJavadocs>false</downloadJavadocs>-->
<!-- <wtpversion>2.0</wtpversion>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<build> <build>
<finalName>${project.artifactId}</finalName> <finalName>${project.artifactId}</finalName>
<plugins> <plugins>
......
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