webpos调用加密方式

@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 + "&timestamp=" + timestamp + "&method=" + method + "&sign=" + sign + "&param=" + 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");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值