@RequestMapping("/service")
public @ResponseBody String rest(HttpServletRequest request) {
if (logger.isInfoEnabled()) {
logger.info("pos service start with method = " + request.getParameter("method"));
}
// 先验证签名
String secretKey = ScmUtils.getScmkeyValue("posConfig", "secretKey");
String ivParameter = ScmUtils.getScmkeyValue("posConfig", "ivParameter");
String app_key = "";
String sign = "";
String param = "";
String method = "";
String timestamp = "";
try {
app_key = request.getParameter("app_key");
sign = request.getParameter("sign");
method = request.getParameter("method");
timestamp = request.getParameter("timestamp");
String requestStr = URLDecoder.decode(request.getQueryString(), "utf-8");
String[] requestStrs = requestStr.split("&");
for (int i = 0; i < requestStrs.length; i++) {
String[] con = requestStrs[i].split("=");
if ("param".equals(con[0])) {
param = con[1];
}
}
} catch (Exception e) {
return createResponse("000001", "缺少系统参数", null, secretKey, ivParameter);
}
String content = param;
String encryptionIson = ScmUtils.getScmkeyValue("posConfig", "encryption_ison");
if (encryptionIson.equals("true")) {
content = AesUtil.decrypt(param, secretKey, ivParameter);
logger.info("业务数据解密后的结果为:" + content);
}
String appkey = ScmUtils.getScmkeyValue("posConfig", "app_key");
if (StringUtils.isNoneBlank(appkey) && !appkey.equals(app_key)) {
return createResponse("000001", "appkey不正确", null, secretKey, ivParameter);
}
String app_secret = ScmUtils.getScmkeyValue("posConfig", "app_secret");
// logger.info("获取到的秘钥为:"+app_secret);
String signIson = ScmUtils.getScmkeyValue("posConfig", "sign_ison");
// logger.info("获取签名开关:"+signIson);
logger.info("接收到的必填参数为:app_key=" + app_key + "×tamp=" + timestamp + "&method=" + method + "&sign=" + sign + "¶m=" + param);
String signStr = app_secret + "app_key#" + app_key + "method#" + method + "param#" + content + "timestamp#" + timestamp + app_secret;
// logger.info("待签名的字符串:"+signStr);
String md5SignStr = MD5Util.Md5(signStr);
// logger.info("签名的结果为:"+md5SignStr);
if (signIson.equals("true") && !md5SignStr.equals(sign)) {
logger.info("签名不正确");
return createResponse("000001", "签名不正确", null, secretKey, ivParameter);
}
Object retObj = null;
String retCode = "999999";
String retDesc = "未调用到接口";
try {
Response<?> response = new Response<>();
switch (method) {
case "suning.sdipf.createOrder":
logger.info("进入创建订单API");
response = orderService.createOrder(gson.fromJson(content, CreateOrderDTO.class));
retCode = response.getResponseCode();
retDesc = response.getResponseMessage();
retObj = response.getResponseObject();
break;
return createResponse(retCode, retDesc, retObj, secretKey, ivParameter);
} catch (Exception e) {
logger.error("未知异常", e);
return createResponse("000001", "未知异常", null, secretKey, ivParameter);
}
}
private String createResponse(String resCode, String resDesc, Object resObj, String secretKey, String ivParameter) {
PosApiResponseVo posApiResponseVo = new PosApiResponseVo();
posApiResponseVo.setRetcode(resCode);
posApiResponseVo.setRetinfo(resDesc);
if (null != resObj) {
logger.info("resObj", resObj.toString());
posApiResponseVo.setData(resObj);
}
String response = gson.toJson(posApiResponseVo);
String content = response;
logger.info("返回的结果为:" + content);
String encryptionIson = ScmUtils.getScmkeyValue("posConfig", "encryption_ison");
if (encryptionIson.equals("true")) {
content = AesUtil.encrypt(response, secretKey, ivParameter);
}
return UrlEncoded.encodeString(content, "utf-8");
}
webpos调用加密方式
最新推荐文章于 2024-04-21 10:39:26 发布