http通过数字签名加密方式进行post请求

我们有时候需要跟别的系统进行数据的对接。例如,别人提供数据接口,我们需要通过数字加密方式进行post的请求获取数据,这篇文章针对这个展开。其中,数据的方式是json格式,希望对你们有所帮助。直接上代码:
日志打印:

private static Logger log = Logger.getLogger(ReformTools.class);

post请求:

public static String getDataFromReformByPost(String json) throws IOException, CloneNotSupportedException {

        String url = "";
     
        //进行消息摘要
        url = MessageDigest(url);

        CloseableHttpClient client = HttpClients.createDefault();

        // 创建HttpPost对象
        HttpPost httpPost = new HttpPost(url);

        // 设置HTTP POST请求参数用json装填参数
        StringEntity stringEntity = new StringEntity(json, "utf-8");
        // 设置请求编码
        stringEntity.setContentEncoding("utf-8");
        // 设置请求类型
        stringEntity.setContentType("application/json");

        CloseableHttpResponse httpResponse = null;
        try {

            // 设置httpPost请求参数,设置参数到请求对象中
            httpPost.setEntity(stringEntity);

            httpResponse = client.execute(httpPost);
            String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 使用getEntity方法活得返回结果
                return result;
                
            } else {
                log.error("sendRequest error: " + url + ",response:" + result);
                return result;
            }
        } catch (Exception e) {
            log.error("sendRequest error: " + url);
            log.error(e.getMessage(), e);
        } finally {
            if (client != null) {
                client.close();
            }
            if (httpResponse != null) {
                httpResponse.close();
            }
            if (httpPost != null) {
                httpPost.clone();
            }
        }
        return null;
    }

加密:

 	public static String MessageDigest(String url) throws UnsupportedEncodingException {
        String clientSystemId = "test";
        Long timestamp = System.currentTimeMillis();
        Double nonce = Math.random();

        //字典序排序
        String[] arr = {clientSystemId, timestamp.toString(), nonce.toString()};
        Arrays.sort(arr);
        //按排序后的前后顺序连成串
        StringBuffer buf = new StringBuffer();
        buf.append(arr[0]).append(arr[1]).append(arr[2]);
        String befStr = buf.toString();
        //SHA1加密
        String mySignature = DigestUtils.shaHex(befStr.getBytes());

        String parameter = "?signature=" + urlParameterEncode(mySignature) +
                "×tamp=" + urlParameterEncode(timestamp.toString()) +
                "&nonce=" + urlParameterEncode(nonce.toString()) +
                "&clientSystemId=" + urlParameterEncode(clientSystemId);

        url += parameter;
        return url;
    }

参数编码(UTF-8方式):

  public static String urlParameterEncode(String urlParameter) throws UnsupportedEncodingException {
        return java.net.URLEncoder.encode(urlParameter, "UTF-8");
    }

到此,完毕!

你可能感兴趣的:(http,java,加密,java,http,加密,url编码)