Commit a571f762 authored by zhangc's avatar zhangc

更新修改gateway代码

parent 2e756f4b
...@@ -16,7 +16,7 @@ import java.util.Map; ...@@ -16,7 +16,7 @@ import java.util.Map;
*/ */
public class OpenApiRequest { public class OpenApiRequest {
private String appId; private String appKey;
private String openApiParams; private String openApiParams;
...@@ -40,12 +40,12 @@ public class OpenApiRequest { ...@@ -40,12 +40,12 @@ public class OpenApiRequest {
} }
} }
public String getAppId() { public String getAppKey() {
return appId; return appKey;
} }
public void setAppId(String appId) { public void setAppKey(String appKey) {
this.appId = appId; this.appKey = appKey;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
......
...@@ -48,6 +48,7 @@ public class GatewayController { ...@@ -48,6 +48,7 @@ public class GatewayController {
RequestContext requestContext = RequestContext.getCurrentContext(); RequestContext requestContext = RequestContext.getCurrentContext();
requestContext.setRequest(httpServletRequest); requestContext.setRequest(httpServletRequest);
requestContext.setResponse(httpServletResponse); requestContext.setResponse(httpServletResponse);
System.out.println("经过了controller~~~~~~~~~~~~~~~~~~~~");
GatewayResponse gatewayResponse = gatewayService.invoke(gatewayRequest); GatewayResponse gatewayResponse = gatewayService.invoke(gatewayRequest);
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
convertResponse(result, gatewayResponse); convertResponse(result, gatewayResponse);
......
...@@ -20,16 +20,25 @@ import java.nio.charset.Charset; ...@@ -20,16 +20,25 @@ import java.nio.charset.Charset;
@Component @Component
public class CheckArgsFilter extends AbstractFilter { public class CheckArgsFilter extends AbstractFilter {
@Override
public int getOrder() {
return Constants.FILTER_ORDER_2;
}
@Override
public String getFilterName() {
return "CheckArgsFilter";
}
@Override @Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { FilterChain chain) {
// 校验参数非空 // 校验参数非空
if (StringUtil.isEmpty(request.getAppKey()) || StringUtil.isEmpty(request.getMethod()) if (StringUtil.isEmpty(request.getAppKey()) || StringUtil.isEmpty(request.getMethod())
|| StringUtil.isEmpty(request.getCharset()) || StringUtil.isEmpty(request.getSignType()) || StringUtil.isEmpty(request.getCharset()) || StringUtil.isEmpty(request.getSignType())
|| StringUtil.isEmpty(request.getSign()) || StringUtil.isEmpty(request.getTimestamp()) || StringUtil.isEmpty(request.getSign()) || StringUtil.isEmpty(request.getTimestamp())
|| StringUtil.isEmpty(request.getVersion()) || StringUtil.isEmpty(request.getVersion())
|| StringUtil.isEmpty(request.getParams())) { || StringUtil.isEmpty(request.getParams())) {
//无效参数
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT); throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);
} }
...@@ -39,27 +48,15 @@ public class CheckArgsFilter extends AbstractFilter { ...@@ -39,27 +48,15 @@ public class CheckArgsFilter extends AbstractFilter {
} }
try { try {
Format.valueOf(request.getFormat()); Format.valueOf(request.getFormat());//格式,目前仅支持JSON
Charset.forName(request.getCharset()); Charset.forName(request.getCharset());//请求使用的编码格式,如UTF-8,GBK,GB2312等
SignType.valueOf(request.getSignType()); SignType.valueOf(request.getSignType());//生成签名字符串所使用的签名算法类型
} catch (Exception ex) { } catch (Exception ex) {
//无效参数
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT); throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);
} }
chain.doFilter(request, response); 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; ...@@ -12,12 +12,16 @@ import javax.servlet.http.HttpServletRequest;
/** /**
* 检查提交数据大小 * 检查提交数据大小
* * @author key
*/ */
@Component("CheckPostSizeFilter") @Component("CheckPostSizeFilter")
public class CheckPostSizeFilter extends AbstractFilter { public class CheckPostSizeFilter extends AbstractFilter {
/**
* 过滤器名称
* @return
*/
@Override @Override
public String getFilterName() { public String getFilterName() {
return "CheckPostSizeFilter"; return "CheckPostSizeFilter";
...@@ -25,7 +29,7 @@ public class CheckPostSizeFilter extends AbstractFilter { ...@@ -25,7 +29,7 @@ public class CheckPostSizeFilter extends AbstractFilter {
/** /**
* @see org.springframework.core.Ordered#getOrder() * 过滤器执行顺序
*/ */
@Override @Override
public int getOrder() { public int getOrder() {
......
...@@ -15,42 +15,37 @@ import org.springframework.stereotype.Component; ...@@ -15,42 +15,37 @@ import org.springframework.stereotype.Component;
@Component @Component
public class CheckTimestampFilter extends AbstractFilter { public class CheckTimestampFilter extends AbstractFilter {
//@Value("${api.skipFilter}")
private boolean skipFilter;
/** /**
* 时间戳超时分钟,10分钟 * 时间戳超时分钟,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 @Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, FilterChain chain) { protected void internalDoFilter(GatewayRequest request, GatewayResponse response, FilterChain chain) {
if (!skipFilter) {
String timestamp = request.getTimestamp(); String timestamp = request.getTimestamp();
long time = 0; long time = 0;
try { try {
time = Long.valueOf(timestamp); time = Long.valueOf(timestamp);
} catch (Exception ex) { } catch (Exception ex) {
throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT); throw new GatewayException(GatewayResultCode.ILLEGAL_ARGUMENT);//无效参数
} }
if (System.currentTimeMillis() - time > max) {//无效时间戳 //if (System.currentTimeMillis() - time > max) {
throw new GatewayException(GatewayResultCode.ILLEGAL_TIMETEMP); // throw new GatewayException(GatewayResultCode.ILLEGAL_TIMETEMP);//无效时间戳
} //}
}
chain.doFilter(request, response); 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; ...@@ -7,7 +7,7 @@ import java.util.List;
/** /**
* 过滤链工厂 * 过滤链工厂
* * @author key
*/ */
@Component @Component
public class FilterChainFactory { public class FilterChainFactory {
......
...@@ -15,6 +15,7 @@ import java.util.List; ...@@ -15,6 +15,7 @@ import java.util.List;
/** /**
* 过滤链 * 过滤链
* *
* @author key
*/ */
public class FilterChainImpl implements FilterChain { public class FilterChainImpl implements FilterChain {
......
...@@ -17,6 +17,7 @@ import org.springframework.stereotype.Component; ...@@ -17,6 +17,7 @@ import org.springframework.stereotype.Component;
/** /**
* 网关路由过滤器 * 网关路由过滤器
* *
* @author key
*/ */
@Component @Component
public class InvokeRouteFilter extends AbstractFilter { public class InvokeRouteFilter extends AbstractFilter {
...@@ -27,6 +28,16 @@ public class InvokeRouteFilter extends AbstractFilter { ...@@ -27,6 +28,16 @@ public class InvokeRouteFilter extends AbstractFilter {
@Autowired @Autowired
DefaultOpenApiDispatcher defaultOpenApiDispatcher; DefaultOpenApiDispatcher defaultOpenApiDispatcher;
@Override
public int getOrder() {
return Constants.FILTER_ORDER_6;
}
@Override
public String getFilterName() {
return "InvokeRouteFilter";
}
@Override @Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { FilterChain chain) {
...@@ -44,11 +55,13 @@ public class InvokeRouteFilter extends AbstractFilter { ...@@ -44,11 +55,13 @@ public class InvokeRouteFilter extends AbstractFilter {
} }
DispatchContext context = new DispatchContext(); DispatchContext context = new DispatchContext();
context.setAppId(request.getAppKey()); context.setAppKey(request.getAppKey());//apiKey
context.setApplication(openApi.getApplication()); context.setApplication(openApi.getApplication());//应用
context.setOpenApiMethod(request.getMethod()); //context.setApplication(openApi.getApplication());//应用
context.setOpenApiParams(request.getParams()); context.setOpenApiMethod(request.getMethod()); //方法 例如:tradd.add
context.setOpenApiVersion(request.getVersion()); context.setOpenApiParams(request.getParams());//入参
context.setOpenApiVersion(request.getVersion()); //版本号
//扩展参数
for (GatewayRequest.Attribute attribute : request.getExtAttributes().values()) { for (GatewayRequest.Attribute attribute : request.getExtAttributes().values()) {
if (attribute != null && attribute.isPass) { if (attribute != null && attribute.isPass) {
context.setExtAttribute(attribute.name, attribute.value); context.setExtAttribute(attribute.name, attribute.value);
...@@ -67,17 +80,5 @@ public class InvokeRouteFilter extends AbstractFilter { ...@@ -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";
}
} }
...@@ -17,146 +17,146 @@ import org.springframework.util.CollectionUtils; ...@@ -17,146 +17,146 @@ import org.springframework.util.CollectionUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** ///**
* 签名过滤器 // * 签名过滤器
*/ // */
@Component //@Component
public class SignatureFilter extends AbstractFilter { //public class SignatureFilter extends AbstractFilter {
//
private static final Logger LOGGER = LoggerFactory.getLogger(SignatureFilter.class); // private static final Logger LOGGER = LoggerFactory.getLogger(SignatureFilter.class);
//
// @Autowired // // @Autowired
// private IApiWhiteService apiWhiteService; // // private IApiWhiteService apiWhiteService;
// @Value("${secret_private}") // // @Value("${secret_private}")
// private String secretPrivate; // // private String secretPrivate;
//
private String encryptData; // private String encryptData;
//
@Override // @Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, // protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { // FilterChain chain) {
try { // try {
chain.doFilter(request, response); // chain.doFilter(request, response);
} finally { // } finally {
String responseJson = null; // String responseJson = null;
String sign = StringUtil.EMPTY_STRING; // String sign = StringUtil.EMPTY_STRING;
String privatekey = null; // String privatekey = null;
if (StringUtil.isEmpty(request.getAppKey())) { // if (StringUtil.isEmpty(request.getAppKey())) {
return; // return;
} // }
//ApiWhite apiWhite = apiWhiteService.byMerchantId(request.getAppId()); // //ApiWhite apiWhite = apiWhiteService.byMerchantId(request.getAppId());
//if (apiWhite == null) { // //if (apiWhite == null) {
// return; // // return;
//} // //}
//是否提供密码 // //是否提供密码
//boolean providePublicKey = false; // //boolean providePublicKey = false;
//if ("0".equals(apiWhite.getProvideSecret())) { // //if ("0".equals(apiWhite.getProvideSecret())) {
// privatekey = apiWhite.getSecretKey(); // // privatekey = apiWhite.getSecretKey();
//} else if ("1".equals(apiWhite.getProvideSecret())) { // //} else if ("1".equals(apiWhite.getProvideSecret())) {
// privatekey = secretPrivate; // // privatekey = secretPrivate;
// providePublicKey = true; // // providePublicKey = true;
//} // //}
try { // try {
responseJson = JsonUtil.toJSONString(response.getResponse(), // responseJson = JsonUtil.toJSONString(response.getResponse(),
PropertyNamingStrategy.SnakeCase); // PropertyNamingStrategy.SnakeCase);
if (StringUtil.isNotEmpty(request.getSignType())) { // if (StringUtil.isNotEmpty(request.getSignType())) {
String charset = request.getCharset(); // String charset = request.getCharset();
if (StringUtil.isEmpty(charset)) { // if (StringUtil.isEmpty(charset)) {
charset = Constants.CHARSET_UTF8; // charset = Constants.CHARSET_UTF8;
} // }
//
//if (providePublicKey) { // //if (providePublicKey) {
// sign = SignatureUtil.sign(convertResult(response.getResponse(), response, apiWhite), privatekey, charset, SignType.valueOf(request.getSignType()), SignatureUtil.SIGN_REQUEST, providePublicKey); // // sign = SignatureUtil.sign(convertResult(response.getResponse(), response, apiWhite), privatekey, charset, SignType.valueOf(request.getSignType()), SignatureUtil.SIGN_REQUEST, providePublicKey);
//} else { // //} else {
// sign = SignatureUtil.sign(convertResult(response.getResponse(), response, apiWhite), privatekey, charset, SignType.valueOf(request.getSignType()), SignatureUtil.SIGN_REQUEST, providePublicKey); // // sign = SignatureUtil.sign(convertResult(response.getResponse(), response, apiWhite), privatekey, charset, SignType.valueOf(request.getSignType()), SignatureUtil.SIGN_REQUEST, providePublicKey);
//} // //}
} // }
} catch (JSONException e) { // } catch (JSONException e) {
LogUtil.error(LOGGER, e, // LogUtil.error(LOGGER, e,
"serializer response to json string error. response=" + response.getResponse()); // "serializer response to json string error. response=" + response.getResponse());
response.clearAttributes(); // response.clearAttributes();
response.setCode(OpenApiResultCode.RESPONSE_DATA_FORMAT_ERROR.getCode()); // response.setCode(OpenApiResultCode.RESPONSE_DATA_FORMAT_ERROR.getCode());
response.setMsg(OpenApiResultCode.RESPONSE_DATA_FORMAT_ERROR.getMsg()); // response.setMsg(OpenApiResultCode.RESPONSE_DATA_FORMAT_ERROR.getMsg());
// sign = signatureException(request, response, privatekey, providePublicKey); // // sign = signatureException(request, response, privatekey, providePublicKey);
} catch (SignatureException ex) { // } catch (SignatureException ex) {
LogUtil.error(LOGGER, ex, // LogUtil.error(LOGGER, ex,
"sign response error. response=" + response.getResponse()); // "sign response error. response=" + response.getResponse());
response.clearAttributes(); // response.clearAttributes();
response.setCode(ex.getResultCode().getCode()); // response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg()); // response.setMsg(ex.getResultCode().getMsg());
} catch (Throwable ex) { // } catch (Throwable ex) {
LogUtil.error(LOGGER, ex, // LogUtil.error(LOGGER, ex,
"signatureFilter doFilter error. response=" + response.getResponse()); // "signatureFilter doFilter error. response=" + response.getResponse());
response.clearAttributes(); // response.clearAttributes();
response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode()); // response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg()); // response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg());
// sign = signatureException(request, response, privatekey, providePublicKey); // // sign = signatureException(request, response, privatekey, providePublicKey);
} // }
response.setSign(sign); // response.setSign(sign);
//if (apiWhite.getIsEncrypt()) { // //if (apiWhite.getIsEncrypt()) {
// response.setEncryptData(encryptData); // // response.setEncryptData(encryptData);
//} // //}
} // }
} // }
//
private Map<String, Object> convertResult(Map<String, Object> attributes, GatewayResponse response) { // private Map<String, Object> convertResult(Map<String, Object> attributes, GatewayResponse response) {
Map<String, Object> mapResult = new HashMap<>(); // Map<String, Object> mapResult = new HashMap<>();
mapResult.put("code", response.getCode()); // mapResult.put("code", response.getCode());
mapResult.put("msg", response.getMsg()); // mapResult.put("msg", response.getMsg());
Object code = attributes.get("code"); // Object code = attributes.get("code");
Object msg = attributes.get("msg"); // Object msg = attributes.get("msg");
attributes.remove("code"); // attributes.remove("code");
attributes.remove("msg"); // attributes.remove("msg");
if (!CollectionUtils.isEmpty(attributes)) { // if (!CollectionUtils.isEmpty(attributes)) {
mapResult.put("data", JsonUtil.toJSONString(attributes, PropertyNamingStrategy.SnakeCase)); // mapResult.put("data", JsonUtil.toJSONString(attributes, PropertyNamingStrategy.SnakeCase));
//if (apiWhite.getIsEncrypt()) { // //if (apiWhite.getIsEncrypt()) {
// String string = (String) mapResult.get("data"); // // String string = (String) mapResult.get("data");
// try { // // try {
// RSAPrivateKey privateKey = RSAUtils.getPrivateKey(secretPrivate); // // RSAPrivateKey privateKey = RSAUtils.getPrivateKey(secretPrivate);
// String encrypt = RSAUtils.privateEncrypt(string, privateKey); // // String encrypt = RSAUtils.privateEncrypt(string, privateKey);
// mapResult.put("data", encrypt); // // mapResult.put("data", encrypt);
// encryptData = encrypt; // // encryptData = encrypt;
// } catch (Exception e) { // // } catch (Exception e) {
// e.printStackTrace(); // // e.printStackTrace();
// } // // }
//} // //}
} // }
attributes.put("code", code); // attributes.put("code", code);
attributes.put("msg", msg); // attributes.put("msg", msg);
return mapResult; // return mapResult;
} // }
//
private String signatureException(GatewayRequest request, GatewayResponse response, // private String signatureException(GatewayRequest request, GatewayResponse response,
String privateKey, boolean providePublicKey) { // String privateKey, boolean providePublicKey) {
String responseJson = null; // String responseJson = null;
String sign = StringUtil.EMPTY_STRING; // String sign = StringUtil.EMPTY_STRING;
try { // try {
responseJson = JsonUtil.toJSONString(response.getResponse(), // responseJson = JsonUtil.toJSONString(response.getResponse(),
PropertyNamingStrategy.SnakeCase); // PropertyNamingStrategy.SnakeCase);
} catch (JSONException e1) { // } catch (JSONException e1) {
LogUtil.error(LOGGER, e1, // LogUtil.error(LOGGER, e1,
"serializer response to json string error. response=" + response.getResponse()); // "serializer response to json string error. response=" + response.getResponse());
return sign; // return sign;
} // }
try { // try {
sign = SignatureUtil.sign(responseJson, privateKey, request.getCharset(), SignType.valueOf(request.getSignType()), SignatureUtil.SIGN_REQUEST, providePublicKey); // sign = SignatureUtil.sign(responseJson, privateKey, request.getCharset(), SignType.valueOf(request.getSignType()), SignatureUtil.SIGN_REQUEST, providePublicKey);
} catch (SignatureException ex) { // } catch (SignatureException ex) {
LogUtil.error(LOGGER, ex, "sign response error. response=" + response.getResponse()); // LogUtil.error(LOGGER, ex, "sign response error. response=" + response.getResponse());
} // }
return sign; // return sign;
} // }
//
/** // /**
* @see org.springframework.core.Ordered#getOrder() // * @see org.springframework.core.Ordered#getOrder()
*/ // */
@Override // @Override
public int getOrder() { // public int getOrder() {
return Constants.FILTER_ORDER_0; // return Constants.FILTER_ORDER_0;
} // }
//
@Override // @Override
public String getFilterName() { // public String getFilterName() {
return "SignatureFilter"; // return "SignatureFilter";
} // }
//
} //}
...@@ -22,74 +22,27 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -22,74 +22,27 @@ public class VerifySignFilter extends AbstractFilter {
//@Autowired //@Autowired
//private IApiWhiteService apiWhiteService; //private IApiWhiteService apiWhiteService;
//@Value("${api.skipFilter}")
private boolean skipFilter;
private final static String CHARSET = "UTF-8"; private final static String CHARSET = "UTF-8";
@Override
public int getOrder() {
return Constants.FILTER_ORDER_4;
}
@Override
public String getFilterName() {
return "VerifySignFilter";
}
@Override @Override
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { 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 openApi = new OpenApi();
openApi.setApplication("HDD_API_GATEWAY"); openApi.setApplication("JZ_API_GATEWAY");//应用
openApi.setRouteType(RouteType.SRPING); openApi.setRouteType(RouteType.SPRINGBOOT);//漏油类型
//openApi.setRouteType(RouteType.SRPING);//漏油类型
RequestContext.getCurrentContext().set("openApi", openApi); RequestContext.getCurrentContext().set("openApi", openApi);
chain.doFilter(request, response); 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";
}
} }
...@@ -34,7 +34,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -34,7 +34,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
/** /**
* 服务注入可参考: /openapi-demo/src/main/resources/META-INF/application-context.xml * 服务注入可参考: /openapi-demo/src/main/resources/META-INF/application-context.xml
* @param openApiServiceList * @param openApiServiceList
* <pre> * <pre>
* &lt;!-- 定义开放接口服务 --> * &lt;!-- 定义开放接口服务 -->
* &lt;bean id="addUserOpenApiService" class="com.xxx.openapi.demo.openapi.AddUserOpenApiService" /&gt; * &lt;bean id="addUserOpenApiService" class="com.xxx.openapi.demo.openapi.AddUserOpenApiService" /&gt;
...@@ -44,8 +44,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -44,8 +44,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
@Resource @Resource
public void setOpenApiServices(List<OpenApiService> openApiServiceList) { public void setOpenApiServices(List<OpenApiService> openApiServiceList) {
for (OpenApiService openApiService : openApiServiceList) { for (OpenApiService openApiService : openApiServiceList) {
String openApiServiceKey = getOpenApiServiceKey(openApiService.getOpenApiMethod(), String openApiServiceKey = getOpenApiServiceKey(openApiService.getOpenApiMethod(),openApiService.getOpenApiVersion());
openApiService.getOpenApiVersion());
openApiServices.put(openApiServiceKey, openApiService); openApiServices.put(openApiServiceKey, openApiService);
} }
} }
...@@ -69,7 +68,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -69,7 +68,7 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
try { try {
OpenApiRequest request = new OpenApiRequest(context.getOpenApiParams()); OpenApiRequest request = new OpenApiRequest(context.getOpenApiParams());
request.setAppId(context.getAppId()); request.setAppKey(context.getAppKey());
request.setExtAttributes(context.getExtAttributes()); request.setExtAttributes(context.getExtAttributes());
openApiService.doService(request, response); openApiService.doService(request, response);
} catch (Throwable ex) { } catch (Throwable ex) {
......
...@@ -6,14 +6,15 @@ import com.jz.dm.model.DispatchContext; ...@@ -6,14 +6,15 @@ import com.jz.dm.model.DispatchContext;
/** /**
* openapi分发器 * openapi分发器
* *
* @author key
*/ */
public interface OpenApiDispatcher { public interface OpenApiDispatcher {
/** /**
* openapi dispatch * openapi dispatch
* *
* @param context dispatch context * @param context dispatch context
* *
* @return * @return
*/ */
public String doDispatch(DispatchContext context); public String doDispatch(DispatchContext context);
......
...@@ -6,7 +6,7 @@ import com.jz.dm.common.util.OpenApiResponse; ...@@ -6,7 +6,7 @@ import com.jz.dm.common.util.OpenApiResponse;
/** /**
* openapi服务 * openapi服务
* * @author key
*/ */
public interface OpenApiService { 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 { ...@@ -24,6 +24,7 @@ public class GatewayServiceImpl implements GatewayService {
@Override @Override
public GatewayResponse invoke(GatewayRequest gatewayRequest) { public GatewayResponse invoke(GatewayRequest gatewayRequest) {
GatewayResponse gatewayResponse = new GatewayResponse(); GatewayResponse gatewayResponse = new GatewayResponse();
System.out.println("经过了网关服务~~~~~~~~~~~~~~~~~~~~");
FilterChain filterChain = filterChainFactory.getFilterChain(); FilterChain filterChain = filterChainFactory.getFilterChain();
filterChain.doFilter(gatewayRequest, gatewayResponse); filterChain.doFilter(gatewayRequest, gatewayResponse);
return gatewayResponse; return gatewayResponse;
......
...@@ -17,7 +17,7 @@ public class DispatchContext implements Serializable { ...@@ -17,7 +17,7 @@ public class DispatchContext implements Serializable {
/** /**
* 商户应用id * 商户应用id
*/ */
private String appId; private String appKey;
/** /**
* 应用 * 应用
...@@ -49,17 +49,17 @@ public class DispatchContext implements Serializable { ...@@ -49,17 +49,17 @@ public class DispatchContext implements Serializable {
* *
* @return property value of appId * @return property value of appId
*/ */
public String getAppId() { public String getAppKey() {
return appId; return appKey;
} }
/** /**
* Setter method for property <tt>appId</tt>. * 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) { public void setAppKey(String appKey) {
this.appId = appId; this.appKey = appKey;
} }
/** /**
......
...@@ -56,18 +56,6 @@ public class GatewayRequest implements Serializable { ...@@ -56,18 +56,6 @@ public class GatewayRequest implements Serializable {
*/ */
private String version; private String version;
///**
// * 回调通知地址
// */
//@ParamName("notify_url")
//private String notifyUrl;
///**
// * 返回地址
// */
//@ParamName("return_url")
//private String returnUrl;
/** /**
* 请求参数,JSON格式 * 请求参数,JSON格式
*/ */
...@@ -312,7 +300,7 @@ public class GatewayRequest implements Serializable { ...@@ -312,7 +300,7 @@ public class GatewayRequest implements Serializable {
*/ */
@Override @Override
public String toString() { 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 + ", charset=" + charset + ", signType=" + signType + ", sign=" + sign
+ ", timestamp=" + timestamp + ", version=" + version + ", timestamp=" + timestamp + ", version=" + version
+ ", params=" + params + ", extAttributes=" + ", params=" + params + ", extAttributes="
......
...@@ -54,13 +54,7 @@ public enum GatewayResultCode implements ResultCode { ...@@ -54,13 +54,7 @@ public enum GatewayResultCode implements ResultCode {
POST_DATA_TOO_LARGE("POST_DATA_TOO_LARGE", "提交数据过大"), POST_DATA_TOO_LARGE("POST_DATA_TOO_LARGE", "提交数据过大"),
/** 调用转发异常 */ /** 调用转发异常 */
DISPATCHER_EXCEPTION("DISPATCHER_EXCEPTION", "调用转发异常"), DISPATCHER_EXCEPTION("DISPATCHER_EXCEPTION", "调用转发异常");
/*账户不存在*/
ACCOUNT_EXIST("ACCOUNT_EXIST","账户信息不存在");
;
/** /**
* 初始化保存到map里方便根据code获取 * 初始化保存到map里方便根据code获取
......
...@@ -2,7 +2,15 @@ package com.jz.dm.model.enums; ...@@ -2,7 +2,15 @@ package com.jz.dm.model.enums;
/** /**
* 路由类型 * 路由类型
* @author key
*/ */
public enum RouteType { public enum RouteType {
SRPING /**
* 漏油类型
*/
SRPING,
/**
* 路由网关
*/
SPRINGBOOT;
} }
...@@ -3,22 +3,25 @@ package com.jz.dm.service; ...@@ -3,22 +3,25 @@ package com.jz.dm.service;
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.gateway.OpenApiService; import com.jz.dm.gateway.OpenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@Slf4j
public class AccountAddService implements OpenApiService { public class AccountAddService implements OpenApiService {
@Override @Override
public String getOpenApiMethod() { public String getOpenApiMethod() {
return "account.add"; return "query";
} }
@Override @Override
public String getOpenApiVersion() { public String getOpenApiVersion() {
return "1.0.0"; return "V1.0.0";
} }
@Override @Override
public void doService(OpenApiRequest request, OpenApiResponse response) { public void doService(OpenApiRequest request, OpenApiResponse response) {
System.out.println("请求过来了。。。。。"); System.out.println("请求过来了。。。。。");
System.out.println(request);
} }
} }
...@@ -9,6 +9,7 @@ import java.util.Map; ...@@ -9,6 +9,7 @@ import java.util.Map;
/** /**
* 参数名注解绑定 * 参数名注解绑定
* *
* @author key
*/ */
public class ParamNameAnnotationBinder extends ExtendedServletRequestDataBinder { public class ParamNameAnnotationBinder extends ExtendedServletRequestDataBinder {
......
...@@ -47,5 +47,3 @@ spring: ...@@ -47,5 +47,3 @@ spring:
# 日志用的filter:log4j # 日志用的filter:log4j
# 防御sql注入的filter:wall # 防御sql注入的filter:wall
filters: stat filters: stat
# ====================MybatisPlus====================
...@@ -19,6 +19,7 @@ spring: ...@@ -19,6 +19,7 @@ spring:
active: test #默认使用的配置文件 active: test #默认使用的配置文件
# ====================MybatisPlus====================
mybatis-plus: mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/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