diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/HmacSignUtil.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/HmacSignUtil.java new file mode 100644 index 000000000..b40894146 --- /dev/null +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/HmacSignUtil.java @@ -0,0 +1,190 @@ +package com.jero.modules.system.util; + +import java.net.MalformedURLException; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.*; + + +/** + * @Author: liyawei + * @Description: 签名工具类 + * @Date: Created in 14:49 2022/2/25 + */ +public class HmacSignUtil { + + //测试main方法 + public static void main(String[] args) throws MalformedURLException, NoSuchAlgorithmException, InvalidKeyException{ + String appSecret= "CDf2D9404C6ac1B0f7c3e3845ae0282a"; + String method ="GET"; + String path = "/people/v1/employee/detail"; + String queryStr = "app_id=100679&hash_type=sha256&domain_account=dujuan.cao"; + Map header = new HashMap<>(); + + System.out.println(getSign(appSecret,method,path,queryStr,header)); + } + + /** + * 获取签名 + * @param appSecret + * @param path + * @param method + * @param queryStr + * @param header + * @return + * @throws NoSuchAlgorithmException + * @throws InvalidKeyException + * @throws MalformedURLException + */ + public static String getSign(String appSecret, String method, String path, String queryStr, Map header) throws NoSuchAlgorithmException, InvalidKeyException, MalformedURLException { + String signString = Splicing(method.toUpperCase() + , path + , getSortQuerysString(queryStr) + , appSecret + , getAccessToken(header)); + + System.out.println(signString); + String sign = SHA(signString, "SHA-256"); + return sign; + } + + /** + * 获取Header中Access Token参数(如果存在) + * @param header + * @return + */ + private static String getAccessToken(Map header){ + //TODO + return ""; + } + + /** + * 将参数排序后重新拼接 + * @param queryStr + * @return 已排序和重新拼装的queryString + */ + private static String getSortQuerysString(String queryStr){ + if(queryStr == null || queryStr.equals("")){ + return ""; + } + Map querys = new HashMap<>(); + String[] arr = queryStr.split("&"); + for(String item : arr){ + if(item == null || item.equals("")){ + continue; + } + String[] kvs = item.split("="); + if(kvs.length > 1){ + querys.put(kvs[0], kvs[1]); + } else { + querys.put(kvs[0], ""); + } + } + + StringBuilder content = new StringBuilder(); + List keys = new ArrayList(querys.keySet()); + Collections.sort(keys); + + for(int i = 0; i < keys.size(); ++i) { + String key = keys.get(i); + String value = String.valueOf(querys.get(key)); + content.append(i == 0 ? "" : "&").append(key).append("="); + if(value != null && !value.equals("")){ + content.append(value); + } + } + + return content.toString(); + } + + /** + * 获取签名时间标签 获取精确到秒的时间戳 + * @return 签名时间标签字符串 + */ + public static String getSecondTimestamp(Date date){ + if (null == date) { + return null; + } + String timestamp = String.valueOf(date.getTime()/1000); + System.out.println(date.getTime()/1000); + return timestamp; + } + + /** + * 拼接字符串 + * @param method 方法 + * @param path + * @param queryString 请求uri中的参数串 + * @param appSecert + * @param accessToken 需要签名的请求头拼接 + * @return 待签名字符串 + */ + private static String Splicing(String method, String path, String queryString, String appSecert, String accessToken){ + //签名⽤的字符串根据如下规则拼接⽽成 + // ${httpMethod}+${PATH}+'?'+'${key}=${value}&${key}=${value}&${key}=${value}'+${appSecret}+${access_token} + StringBuilder sb = new StringBuilder(); + sb.append(method.toUpperCase()); + sb.append(path); + sb.append("?"); + // 如果请求数据为url + if (queryString == null){ + queryString = ""; + } + sb.append(queryString); + sb.append(appSecert); + if (accessToken != null && !accessToken.equals("")){ + sb.append(accessToken); + } + // 拼接字符串 + return sb.toString(); + } + + /** + * 字符串 SHA 加密 + * + * @param strText + * @param strType + * @return + */ + private static String SHA(String strText, String strType) + { + // 返回值 + String strResult = null; + + // 是否是有效字符串 + if (strText != null && strText.length() > 0) + { + try + { + // SHA 加密开始 + // 创建加密对象 并传入加密类型 + MessageDigest messageDigest = MessageDigest.getInstance(strType); + // 传入要加密的字符串 + messageDigest.update(strText.getBytes()); + // 得到 byte 类型结果 + byte byteBuffer[] = messageDigest.digest(); + + // 将 byte 转换为 string + StringBuffer strHexString = new StringBuffer(); + // 遍历 byte buffer + for (int i = 0; i < byteBuffer.length; i++) + { + String hex = Integer.toHexString(0xff & byteBuffer[i]); + if (hex.length() == 1) + { + strHexString.append('0'); + } + strHexString.append(hex); + } + // 得到返回結果 + strResult = strHexString.toString(); + } + catch (NoSuchAlgorithmException e) + { + e.printStackTrace(); + } + } + return strResult; + } +} \ No newline at end of file diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/HttpRequestUtil.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/HttpRequestUtil.java new file mode 100644 index 000000000..984f2c14e --- /dev/null +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/HttpRequestUtil.java @@ -0,0 +1,184 @@ +package com.jero.modules.system.util; + +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +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.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.zip.GZIPInputStream; + +/** + * @Author: liyawei + * @Description: + * @Date: Created in 9:40 2022/2/24 + */ +public class HttpRequestUtil { + //main方法测试 + public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException { + String appId = "100679"; + String appSecret = "CDf2D9404C6ac1B0f7c3e3845ae0282a"; + // 获取签名 + String method ="GET"; + String path = "/people/v1/employee/detail"; + String queryStr = "app_id=100679&hash_type=sha256&domain_account=dujuan.cao"; + Map header = new HashMap<>(); + String timestamp = HmacSignUtil.getSecondTimestamp(new Date()); + queryStr += "×tamp=" + timestamp; + String sign = HmacSignUtil.getSign(appSecret,method,path,queryStr,header); + String url = "http://napoleon-fab-test.nioint.com"; + url += path + "?"; + url += queryStr; + url += "&sign=" + sign; + Map headerMap = new HashMap<>(); + String response = getResponseOfGET(url, headerMap); + + /* String url = ""; + Map headerMap = HmacSignUtil.createSignHeader(appId, appSecret, url, "post"); + String body = JSONObject.toJSONString(null); + String response = getResponseOfPOST(url, headerMap, body);*/ + System.out.println(response); + } + + public static String getResponseOfPOST(String url, Map headerMap,String body) throws IOException { + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + CloseableHttpResponse httpResponse; + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(20000000).setSocketTimeout(200000000).build(); + HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); + httpPost.setEntity(new StringEntity(body)); + if (headerMap != null) { + Iterator var38 = headerMap.entrySet().iterator(); + + while (var38.hasNext()) { + Map.Entry entry = (Map.Entry) var38.next(); + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + } +// httpPost.addHeader("Content-Type", "application/json"); + + httpResponse = httpClient.execute(httpPost); + // 校验是否正确 + final int statusCode = httpResponse.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) { + System.out.println("Error authenticating to Force.com: " + statusCode); + return null; + } + + String getResult = null; + try { + getResult = getStringFromResponseUzip(httpResponse); + } catch (Exception ioException) { + return null; + // Handle system IO exception + } + System.out.println(getResult); + return getResult; + } + + public static String getResponseOfGET(String url, Map headerMap) throws IOException { + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + CloseableHttpResponse httpResponse; + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(20000000).setSocketTimeout(200000000).build(); + HttpGet httpGet = new HttpGet(url); + httpGet.setConfig(requestConfig); + if (headerMap != null) { + Iterator var38 = headerMap.entrySet().iterator(); + + while (var38.hasNext()) { + Map.Entry entry = (Map.Entry) var38.next(); + httpGet.addHeader(entry.getKey(), entry.getValue()); + } + } +// httpGet.addHeader("Content-Type", "application/json"); + + httpResponse = httpClient.execute(httpGet); + // 校验是否正确 + final int statusCode = httpResponse.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) { + System.out.println("Error authenticating to Force.com: " + statusCode); + return null; + } + + String getResult = null; + try { + getResult = getStringFromResponseUzip(httpResponse); + } catch (Exception ioException) { + return null; + // Handle system IO exception + } + System.out.println(getResult); + return getResult; + } + /** + * 对请求结果进行转码 + * + * @param response + * @return + * @throws Exception + */ + public static String getStringFromResponseUzip(final HttpResponse response) throws IOException { + if (response == null) { + return null; + } + String responseText = ""; + //流不能关闭,关了就报错 + final InputStream in = response.getEntity().getContent(); + final Header[] headers = response.getHeaders("Content-Encoding"); + for (final Header h : headers) { + System.out.println(h.getValue()); + if (h.getValue().indexOf("gzip") > -1) { + //For GZip response + try (final GZIPInputStream gzin = new GZIPInputStream(in); + final InputStreamReader isr = new InputStreamReader(gzin, "UTF-8")) { + responseText = getStringFromStream(isr); + //responseText = URLDecoder.decode(responseText, "utf-8"); + } catch (final IOException exception) { + exception.printStackTrace(); + } + return responseText; + } + } + + responseText = EntityUtils.toString(response.getEntity(), "utf-8"); + return responseText; + } + + /** + * 文本转换 + * + * @param isr + * @return + */ + public static String getStringFromStream(final InputStreamReader isr) { + final StringBuilder sb = new StringBuilder(); + try (BufferedReader br = new BufferedReader(isr)) { + String tmp; + while ((tmp = br.readLine()) != null) { + sb.append(tmp); + sb.append("\r\n"); + } + } catch (IOException e) { + //读取异常,返回null + return null; + } + return sb.toString(); + } + +} diff --git a/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml b/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml index 7dbe0c01b..04d0038e5 100644 --- a/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml +++ b/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml @@ -324,4 +324,16 @@ eureka: ##是否从Eureka Server上获取注册信息,默认为true fetch-registry: false service-url: - defaultZone: http://127.0.0.1:8123/eureka/ \ No newline at end of file + defaultZone: http://127.0.0.1:8123/eureka/ +## people 2.0 +people: + appId: 100679 + # 访问域名 CN TEST版 + host: http://napoleon-fab-test.nioint.com + # 访问域名 CN PROD版 + # host: http://napoleon.nioint.com + + # appSecret CN TEST版 + secret: CDf2D9404C6ac1B0f7c3e3845ae0282a + # appSecret CN PROD版 + # secret: 7C3F03170E3ea489df04Ce8DEC7Df4f7