Commit a571f762 authored by zhangc's avatar zhangc

更新修改gateway代码

parent 2e756f4b
......@@ -16,7 +16,7 @@ import java.util.Map;
*/
public class OpenApiRequest {
private String appId;
private String appKey;
private String openApiParams;
......@@ -40,12 +40,12 @@ public class OpenApiRequest {
}
}
public String getAppId() {
return appId;
public String getAppKey() {
return appKey;
}
public void setAppId(String appId) {
this.appId = appId;
public void setAppKey(String appKey) {
this.appKey = appKey;
}
@SuppressWarnings("unchecked")
......
......@@ -48,6 +48,7 @@ public class GatewayController {
RequestContext requestContext = RequestContext.getCurrentContext();
requestContext.setRequest(httpServletRequest);
requestContext.setResponse(httpServletResponse);
System.out.println("经过了controller~~~~~~~~~~~~~~~~~~~~");
GatewayResponse gatewayResponse = gatewayService.invoke(gatewayRequest);
JSONObject result = new JSONObject();
convertResponse(result, gatewayResponse);
......
......@@ -20,16 +20,25 @@ import java.nio.charset.Charset;
@Component
public class CheckArgsFilter extends AbstractFilter {
@Override
public int getOrder() {
return Constants.FILTER_ORDER_2;
}
@Override
public String getFilterName() {
return "CheckArgsFilter";
}
@Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) {
// 校验参数非空
if (StringUtil.isEmpty(request.getAppKey()) || StringUtil.isEmpty(request.getMethod())
|| StringUtil.isEmpty(request.getCharset()) || StringUtil.isEmpty(request.getSignType())
|| StringUtil.isEmpty(request.getSign()) || StringUtil.isEmpty(request.getTimestamp())
|| StringUtil.isEmpty(request.getVersion())
|| StringUtil.isEmpty(request.getParams())) {
//无效参数
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);
}
......@@ -39,27 +48,15 @@ public class CheckArgsFilter extends AbstractFilter {
}
try {
Format.valueOf(request.getFormat());
Charset.forName(request.getCharset());
SignType.valueOf(request.getSignType());
Format.valueOf(request.getFormat());//格式,目前仅支持JSON
Charset.forName(request.getCharset());//请求使用的编码格式,如UTF-8,GBK,GB2312等
SignType.valueOf(request.getSignType());//生成签名字符串所使用的签名算法类型
} catch (Exception ex) {
//无效参数
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);
}
chain.doFilter(request, response);
}
/**
* @see org.springframework.core.Ordered#getOrder()
*/
@Override
public int getOrder() {
return Constants.FILTER_ORDER_2;
}
@Override
public String getFilterName() {
return "CheckArgsFilter";
}
}
......@@ -12,12 +12,16 @@ import javax.servlet.http.HttpServletRequest;
/**
* 检查提交数据大小
*
* @author key
*/
@Component("CheckPostSizeFilter")
public class CheckPostSizeFilter extends AbstractFilter {
/**
* 过滤器名称
* @return
*/
@Override
public String getFilterName() {
return "CheckPostSizeFilter";
......@@ -25,7 +29,7 @@ public class CheckPostSizeFilter extends AbstractFilter {
/**
* @see org.springframework.core.Ordered#getOrder()
* 过滤器执行顺序
*/
@Override
public int getOrder() {
......
......@@ -15,42 +15,37 @@ import org.springframework.stereotype.Component;
@Component
public class CheckTimestampFilter extends AbstractFilter {
//@Value("${api.skipFilter}")
private boolean skipFilter;
/**
* 时间戳超时分钟,10分钟
*/
private static final long max = 1000 * 60 * 10; //10分钟
//private static final long max = 1000 * 60 * 10; //10分钟
private static final long max = 1000 * 60 * 60*24; //10分钟
@Override
public int getOrder() {
return Constants.FILTER_ORDER_3;
}
@Override
public String getFilterName() {
return "CheckTimestampFilter";
}
@Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, FilterChain chain) {
if (!skipFilter) {
String timestamp = request.getTimestamp();
long time = 0;
try {
time = Long.valueOf(timestamp);
} catch (Exception ex) {
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);
}
if (System.currentTimeMillis() - time > max) {//无效时间戳
throw new GatewayException(GatewayResultCode.ILLEGAL_TIMETEMP);
}
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);//无效参数
}
//if (System.currentTimeMillis() - time > max) {
// throw new GatewayException(GatewayResultCode.ILLEGAL_TIMETEMP);//无效时间戳
//}
chain.doFilter(request, response);
}
/**
* @see org.springframework.core.Ordered#getOrder()
*/
@Override
public int getOrder() {
return Constants.FILTER_ORDER_5;
}
@Override
public String getFilterName() {
return "CheckTimestampFilter";
}
}
......@@ -7,7 +7,7 @@ import java.util.List;
/**
* 过滤链工厂
*
* @author key
*/
@Component
public class FilterChainFactory {
......
......@@ -15,6 +15,7 @@ import java.util.List;
/**
* 过滤链
*
* @author key
*/
public class FilterChainImpl implements FilterChain {
......
......@@ -17,6 +17,7 @@ import org.springframework.stereotype.Component;
/**
* 网关路由过滤器
*
* @author key
*/
@Component
public class InvokeRouteFilter extends AbstractFilter {
......@@ -27,6 +28,16 @@ public class InvokeRouteFilter extends AbstractFilter {
@Autowired
DefaultOpenApiDispatcher defaultOpenApiDispatcher;
@Override
public int getOrder() {
return Constants.FILTER_ORDER_6;
}
@Override
public String getFilterName() {
return "InvokeRouteFilter";
}
@Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) {
......@@ -44,11 +55,13 @@ public class InvokeRouteFilter extends AbstractFilter {
}
DispatchContext context = new DispatchContext();
context.setAppId(request.getAppKey());
context.setApplication(openApi.getApplication());
context.setOpenApiMethod(request.getMethod());
context.setOpenApiParams(request.getParams());
context.setOpenApiVersion(request.getVersion());
context.setAppKey(request.getAppKey());//apiKey
context.setApplication(openApi.getApplication());//应用
//context.setApplication(openApi.getApplication());//应用
context.setOpenApiMethod(request.getMethod()); //方法 例如:tradd.add
context.setOpenApiParams(request.getParams());//入参
context.setOpenApiVersion(request.getVersion()); //版本号
//扩展参数
for (GatewayRequest.Attribute attribute : request.getExtAttributes().values()) {
if (attribute != null && attribute.isPass) {
context.setExtAttribute(attribute.name, attribute.value);
......@@ -67,17 +80,5 @@ public class InvokeRouteFilter extends AbstractFilter {
}
}
/**
* @see org.springframework.core.Ordered#getOrder()
*/
@Override
public int getOrder() {
return Constants.FILTER_ORDER_6;
}
@Override
public String getFilterName() {
return "InvokeRouteFilter";
}
}
......@@ -22,74 +22,27 @@ public class VerifySignFilter extends AbstractFilter {
//@Autowired
//private IApiWhiteService apiWhiteService;
//@Value("${api.skipFilter}")
private boolean skipFilter;
private final static String CHARSET = "UTF-8";
@Override
public int getOrder() {
return Constants.FILTER_ORDER_4;
}
@Override
public String getFilterName() {
return "VerifySignFilter";
}
@Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) {
HddHashMap params = new HddHashMap();
params.put("app_id", request.getAppKey());
params.put("method", request.getMethod());
params.put("charset", request.getCharset());
params.put("sign", request.getSign());
params.put("sign_type", request.getSignType());
params.put("timestamp", request.getTimestamp());
params.put("version", request.getVersion());
params.put("params", request.getParams());
//ApiWhite apiWhite = apiWhiteService.byMerchantId(request.getAppId());
//if (apiWhite != null) {
// String publicKey = apiWhite.getSecretKey();
//
// if (!skipFilter) {
// boolean verify = SignatureUtil.verify(params, publicKey, request.getCharset(), SignType.valueOf(request.getSignType()));
// if (!verify) {
// throw new SignatureException(OpenApiResultCode.SIGN_VERIFY_ERROR);
// }
//
// //参数解密
// if (apiWhite.getIsEncrypt()) {
// String decryptParams = decryptParams(request.getParams(), publicKey);
// request.setParams(decryptParams);
// }
// }
//}
// 后期再扩展
OpenApi openApi = new OpenApi();
openApi.setApplication("HDD_API_GATEWAY");
openApi.setRouteType(RouteType.SRPING);
openApi.setApplication("JZ_API_GATEWAY");//应用
openApi.setRouteType(RouteType.SPRINGBOOT);//漏油类型
//openApi.setRouteType(RouteType.SRPING);//漏油类型
RequestContext.getCurrentContext().set("openApi", openApi);
chain.doFilter(request, response);
}
private String decryptParams(String params, String publicKey) {
log.info("解密前请求参数,{}", params);
try {
RSAPublicKey key = RSAUtils.getPublicKey(publicKey);
String decrypt = RSAUtils.publicDecrypt(params, key);
log.info("解密后请求参数,{}", decrypt);
return decrypt;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @see org.springframework.core.Ordered#getOrder()
*/
@Override
public int getOrder() {
return Constants.FILTER_ORDER_4;
}
@Override
public String getFilterName() {
return "VerifySignFilter";
}
}
......@@ -44,8 +44,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
@Resource
public void setOpenApiServices(List<OpenApiService> openApiServiceList) {
for (OpenApiService openApiService : openApiServiceList) {
String openApiServiceKey = getOpenApiServiceKey(openApiService.getOpenApiMethod(),
openApiService.getOpenApiVersion());
String openApiServiceKey = getOpenApiServiceKey(openApiService.getOpenApiMethod(),openApiService.getOpenApiVersion());
openApiServices.put(openApiServiceKey, openApiService);
}
}
......@@ -69,7 +68,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
try {
OpenApiRequest request = new OpenApiRequest(context.getOpenApiParams());
request.setAppId(context.getAppId());
request.setAppKey(context.getAppKey());
request.setExtAttributes(context.getExtAttributes());
openApiService.doService(request, response);
} catch (Throwable ex) {
......
......@@ -6,6 +6,7 @@ import com.jz.dm.model.DispatchContext;
/**
* openapi分发器
*
* @author key
*/
public interface OpenApiDispatcher {
......
......@@ -6,7 +6,7 @@ import com.jz.dm.common.util.OpenApiResponse;
/**
* openapi服务
*
* @author key
*/
public interface OpenApiService {
......
/**
* Copyright (c) 2011-2019 All Rights Reserved.
*/
package com.jz.dm.gateway.impl;
/**
* API白名单表 服务实现类
*
* @author zengxx
* @version $Id: ApiWhiteServiceImpl.java 2020-04-02 $
*/
//@Service("apiWhiteService")
//public class ApiWhiteServiceImpl implements IApiWhiteService {
//@Resource
//private ApiWhiteMapper apiWhiteMapper;
//@Resource
//private MeasureRecordMapper measureRecordMapper;
//
//@Override
//public ApiWhite byMerchantId(String merchantId) throws TtyException {
// return apiWhiteMapper.byMerchantId(merchantId);
//}
//
//@Override
//@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
//public long adjustAmt(String merchantId, long amt) {
// if (amt == 0) {
// return amt;
// }
// BigDecimal adjustAmt = new BigDecimal(amt).divide(new BigDecimal(100));
// ApiWhite apiWhite = apiWhiteMapper.byMerchantId(merchantId);
// long result = apiWhiteMapper.adjustAmt(merchantId, adjustAmt);
// if (result != 1) {
// return 0;
// } else {
// MeasureRecord measure = new MeasureRecord();
// //已用金额
// BigDecimal sumUseAmt = apiWhiteMapper.sumUseAmt(merchantId);
// if (sumUseAmt == null) {
// sumUseAmt = BigDecimal.ZERO;
// }
// if (amt > 0) {
// measure.setChangType("0");
// measure.setUseAmount(sumUseAmt.subtract(adjustAmt));
// } else {
// measure.setChangType("1");
// measure.setUseAmount(sumUseAmt.add(adjustAmt.abs()));
// }
// measure.setChangeAmount(adjustAmt.abs());
// measure.setAfterAmount(apiWhite.getUsableAmount().add(adjustAmt));
//
// measure.setMerchantId(apiWhite.getMerchantId());
// measure.setName(apiWhite.getName());
// measure.setBeforeAmount(apiWhite.getUsableAmount());
// measure.setChangDate(LocalDateTime.now());
// measure.setCreateUser("API");
// measure.setCreateDate(LocalDateTime.now());
// //插入流水信息
// if (measureRecordMapper.insert(measure) > 0) {
// return 1;
// }
// }
// return 0;
//}
//@Override
//@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
//public void invoke3bcCount(String merchantId) {
// apiWhiteMapper.invoke3bcCount(merchantId);
//}
//}
......@@ -24,6 +24,7 @@ public class GatewayServiceImpl implements GatewayService {
@Override
public GatewayResponse invoke(GatewayRequest gatewayRequest) {
GatewayResponse gatewayResponse = new GatewayResponse();
System.out.println("经过了网关服务~~~~~~~~~~~~~~~~~~~~");
FilterChain filterChain = filterChainFactory.getFilterChain();
filterChain.doFilter(gatewayRequest, gatewayResponse);
return gatewayResponse;
......
......@@ -17,7 +17,7 @@ public class DispatchContext implements Serializable {
/**
* 商户应用id
*/
private String appId;
private String appKey;
/**
* 应用
......@@ -49,17 +49,17 @@ public class DispatchContext implements Serializable {
*
* @return property value of appId
*/
public String getAppId() {
return appId;
public String getAppKey() {
return appKey;
}
/**
* Setter method for property <tt>appId</tt>.
*
* @param appId value to be assigned to property appId
* @param appKey value to be assigned to property appId
*/
public void setAppId(String appId) {
this.appId = appId;
public void setAppKey(String appKey) {
this.appKey = appKey;
}
/**
......
......@@ -56,18 +56,6 @@ public class GatewayRequest implements Serializable {
*/
private String version;
///**
// * 回调通知地址
// */
//@ParamName("notify_url")
//private String notifyUrl;
///**
// * 返回地址
// */
//@ParamName("return_url")
//private String returnUrl;
/**
* 请求参数,JSON格式
*/
......@@ -312,7 +300,7 @@ public class GatewayRequest implements Serializable {
*/
@Override
public String toString() {
return "GatewayRequest [appId=" + appKey + ", method=" + method + ", format=" + format
return "GatewayRequest [appKey=" + appKey + ", method=" + method + ", format=" + format
+ ", charset=" + charset + ", signType=" + signType + ", sign=" + sign
+ ", timestamp=" + timestamp + ", version=" + version
+ ", params=" + params + ", extAttributes="
......
......@@ -54,13 +54,7 @@ public enum GatewayResultCode implements ResultCode {
POST_DATA_TOO_LARGE("POST_DATA_TOO_LARGE", "提交数据过大"),
/** 调用转发异常 */
DISPATCHER_EXCEPTION("DISPATCHER_EXCEPTION", "调用转发异常"),
/*账户不存在*/
ACCOUNT_EXIST("ACCOUNT_EXIST","账户信息不存在");
;
DISPATCHER_EXCEPTION("DISPATCHER_EXCEPTION", "调用转发异常");
/**
* 初始化保存到map里方便根据code获取
......
......@@ -2,7 +2,15 @@ package com.jz.dm.model.enums;
/**
* 路由类型
* @author key
*/
public enum RouteType {
SRPING
/**
* 漏油类型
*/
SRPING,
/**
* 路由网关
*/
SPRINGBOOT;
}
......@@ -3,22 +3,25 @@ package com.jz.dm.service;
import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.gateway.OpenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class AccountAddService implements OpenApiService {
@Override
public String getOpenApiMethod() {
return "account.add";
return "query";
}
@Override
public String getOpenApiVersion() {
return "1.0.0";
return "V1.0.0";
}
@Override
public void doService(OpenApiRequest request, OpenApiResponse response) {
System.out.println("请求过来了。。。。。");
System.out.println(request);
}
}
......@@ -9,6 +9,7 @@ import java.util.Map;
/**
* 参数名注解绑定
*
* @author key
*/
public class ParamNameAnnotationBinder extends ExtendedServletRequestDataBinder {
......
......@@ -47,5 +47,3 @@ spring:
# 日志用的filter:log4j
# 防御sql注入的filter:wall
filters: stat
# ====================MybatisPlus====================
......@@ -19,6 +19,7 @@ spring:
active: test #默认使用的配置文件
# ====================MybatisPlus====================
mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
......
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