package com.ruoyi.common.signature; import com.alibaba.fastjson2.JSON; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.Calendar; /** * @email 1603559716@qq.com * @author: zf * @date: 2023/9/8 * @time: 14:52 */ public class SignatureUtils { private static final Logger logger = LoggerFactory.getLogger(SignatureUtils.class); // 接口服务地址 private static final String restSever = "https://inspurtestcx.saws.org.cn/sjjh"; // 应用标识 private static final String appKey = "hj92qe"; // 加密算法 private static final String signMethod = "SHA-256"; // 身份系统签发给应用对接的密钥 private static final String appPwd = "dxep6j"; /** * 获取数据接口 * @param getQueryParam * @return */ public static Object getObject(String getQueryParam,String url){ // 时间戳 Long ts = Calendar.getInstance().getTime().getTime(); // 随机数 String once = RandomStringUtils.randomAlphanumeric(32); // 接口header 中的公共参数 String commonParamUrl = String.format("appKey=%s" + "&" + "ts=%s" + "&" + "once=%s" + "&" + "signMethod=%s", appKey, ts, once, signMethod); String getFullUrl = restSever + url + "?" + getQueryParam; HttpGet httpGet = new HttpGet(getFullUrl); // get 请求查询参数, 用在 URL 上的, 这里若是通过 ID 查询的, 接口中 ID 是作为路径存在的, 所以需要将 ID 组成 String getAllParamUrl = commonParamUrl + "&" + getQueryParam; // 创建 HttpClient 对象 CloseableHttpClient httpclient = (CloseableHttpClient) SkipHttpsUtils.wrapClient(); // 对参数签名, 并放入请求 header 中的 signData 参数中 try { // 签名数据 String signData = TokenUtils.getSignature(appPwd, getAllParamUrl); // 添加 header 参数 appCode、 timestamp、 signatureNonce、 signature httpGet.addHeader("appKey", appKey); httpGet.addHeader("ts", ts.toString()); httpGet.addHeader("once", once); httpGet.addHeader("signMethod", signMethod); System.out.println("once:" + once); httpGet.addHeader("signData", signData); System.out.println("headers:" + httpGet.getAllHeaders()); String urlStr = httpGet.getURI().toString(); // 公共参数 URL System.out.println("commonParamter:" + urlStr); if (StringUtils.endsWith(urlStr, "/")) { urlStr = StringUtils.removeEnd(urlStr, "/"); } httpGet.setURI(new URI(urlStr)); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000) .setConnectionRequestTimeout(6000).setSocketTimeout(6000).build(); httpGet.setConfig(requestConfig); System.out.println("urlStr in request:" + httpGet.getURI().toString()); // 执行请求 CloseableHttpResponse response = httpclient.execute(httpGet); // 取响应的结果 int statusCode = response.getStatusLine().getStatusCode(); // 打印响应结果 if (statusCode == HttpStatus.SC_OK) { String resp = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println("status:" + statusCode); Object object = JSON.parse(resp); System.out.println("result:" + resp); return object; } else { System.out.println(statusCode); } } catch (URISyntaxException e) { logger.error("签名失败: ", e); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { /** * GET 查询接口演示代码 */ String startTime = "2018-05-25 00:00:00"; String endTime = "2023-06-01 21:00:00"; try { startTime = URLEncoder.encode(startTime, "UTF-8"); endTime = URLEncoder.encode(endTime, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getCause()); } String getQueryParam = "startTime=" + startTime + "&endTime=" + endTime; System.out.println(getQueryParam); Object obj = getObject(getQueryParam,"/api/v1/exam/plan/enroll/download"); System.out.println(obj); } }