|
@@ -0,0 +1,198 @@
|
|
|
|
+/*
|
|
|
|
+ * Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
+ * contributor license agreements. See the NOTICE file distributed with
|
|
|
|
+ * this work for additional information regarding copyright ownership.
|
|
|
|
+ * The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
+ * (the "License"); you may not use this file except in compliance with
|
|
|
|
+ * the License. You may obtain a copy of the License at
|
|
|
|
+ *
|
|
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
+ *
|
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
|
+ * limitations under the License.
|
|
|
|
+ */
|
|
|
|
+package com.sooka.sponest.event.utils;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.HexUtil;
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+
|
|
|
|
+import javax.crypto.Mac;
|
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: demo.
|
|
|
|
+ * @Author: newland
|
|
|
|
+ * 发生订购鉴权失败时请先仔细阅读一下内容:
|
|
|
|
+ * 首先可以让运管这边推送订购关系,由你们提供接收接口,运管可以进行订购关系推送
|
|
|
|
+ * 然后由于新接入的平台,没有订单关系,所以目前运管这边采取的是推送其他平台的订单
|
|
|
|
+ * 在这样的情况下,AK,SK,仍然是使用你们自己的,但是 appCode 则需要使用该订单本身的 appCode,不然网关这边鉴权过不去
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+public class GWHttpUtilsTest65 {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 新网关
|
|
|
|
+ */
|
|
|
|
+ static String HOST_IP = "https://sl-neimenggu.chinatowercom.cn:9997";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * AK 由运管这边提供
|
|
|
|
+ */
|
|
|
|
+ static String AK = "012626d75ab147858ff7fdf9ea44b74d";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * SK 由运管这边提供
|
|
|
|
+ */
|
|
|
|
+ static String SK = "dce85f55cad140498936e7832e6528ad";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 行业编码 由运管这边提供
|
|
|
|
+ * 当发送订购鉴权失败时,请阅读最上面的解释,然后可以向运管那边提供订单号获得该订单正确的 appCode
|
|
|
|
+ * 当调试完成后,即可换成自己的 appCode
|
|
|
|
+ */
|
|
|
|
+ static String appCode = "700002";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 订购客户编码,用于订购鉴权,需要一一对应
|
|
|
|
+ */
|
|
|
|
+ static String customer = "124012301";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 订单号,用于订购鉴权,需要一一对应
|
|
|
|
+ */
|
|
|
|
+// static String orderCode = "PO124012301202504047918802915328";
|
|
|
|
+// static String orderCode = "PO124012301202503303260509147136";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 用户编码,暂可以填随意值
|
|
|
|
+ */
|
|
|
|
+ static String userCode = "0a97fb806ab24ca8bc788dc6ace3d4c2";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 访问方法路径
|
|
|
|
+ * /devices/{deviceCode}/channels/{channelCode}/getVideoRealtimeUrl
|
|
|
|
+ * 注: 这个路径在下面要参与到 sign加密,路径前面不要加服务名,比如 boot-out
|
|
|
|
+ * 例子:路径中的{deviceCode}、{channelCode},用于订购鉴权,需要一一对应
|
|
|
|
+ */
|
|
|
|
+ static String servicePath = "/file/obsFiles/getObsEncryptUrls";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String encrypt(String path, String orderCode) {
|
|
|
|
+ // 请求参数
|
|
|
|
+ String json = "{\n" +
|
|
|
|
+ " \"fileUrls\": [\n\"" +
|
|
|
|
+// " \"/15000000005/ThirdAlarm/pic/ZGTT-1717140001_d9b78c489b0f4263b2accee140f12d83a8e66c4bc69c4d70b929762f033c63d5_02_001.jpg\",\n" +
|
|
|
|
+ path +
|
|
|
|
+ "\" ]\n" +
|
|
|
|
+ "}";
|
|
|
|
+ return callServer(json, orderCode);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 服务调用
|
|
|
|
+ */
|
|
|
|
+ public static String callServer(String json, String orderCode) {
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ // 这里的 url是发送请求拼接url
|
|
|
|
+ String url = HOST_IP + servicePath;
|
|
|
|
+ String msgId = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
+ String requestTime = dateFormat.format(new Date());
|
|
|
|
+ System.out.println(requestTime);
|
|
|
|
+
|
|
|
|
+ // 下面两步是通过 AK,SK,servicePath 等参数生产 sign
|
|
|
|
+ // 注:加密方式尽量参考本例中的加密
|
|
|
|
+ // 大部分报错 signature value is error! 都是加密失败造成的,请仔细检查 AK, SK, path 是否正确
|
|
|
|
+ String signContext = getSignContext(servicePath, AK, msgId, requestTime, customer, userCode);
|
|
|
|
+ String sign = getGenerateSign(SK, signContext);
|
|
|
|
+ System.out.println("=========sign=====:" + sign);
|
|
|
|
+
|
|
|
|
+ // ********模拟请求头参数,以下参数为通用必填参数************
|
|
|
|
+ Map<String, String> headerMap = new HashMap<>();
|
|
|
|
+ headerMap.put("X-AppKey", AK);
|
|
|
|
+ headerMap.put("X-Signature", sign);
|
|
|
|
+ headerMap.put("X-MsgId", msgId);
|
|
|
|
+ headerMap.put("X-RequestTime", requestTime);
|
|
|
|
+ headerMap.put("X-CustomerCode", customer);
|
|
|
|
+ headerMap.put("X-UserCode", userCode);
|
|
|
|
+ headerMap.put("X-AppCode", appCode);
|
|
|
|
+ headerMap.put("X-OrderCode", orderCode);
|
|
|
|
+ HttpResponse response = postObject(url, json, headerMap);
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(response.body());
|
|
|
|
+ return jsonObject.getJSONArray("data").getJSONObject(0).getString("encryptUrl");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 组装 post调用的header和请求体 .
|
|
|
|
+ *
|
|
|
|
+ * @param appAddress 发送地址 .
|
|
|
|
+ * @param params 参数 .
|
|
|
|
+ * @return postObject.
|
|
|
|
+ */
|
|
|
|
+ public static HttpResponse postObject(String appAddress, String params, Map<String, String> headerMap) {
|
|
|
|
+ HttpResponse result = null;
|
|
|
|
+ try {
|
|
|
|
+ result = HttpRequest.post(appAddress)
|
|
|
|
+ .headerMap(headerMap, true)
|
|
|
|
+ .body(params)
|
|
|
|
+ .timeout(30000)
|
|
|
|
+ .execute();
|
|
|
|
+ log.info("\n请求地址{},\n返回状态:{},\n返回信息{}", appAddress, result.getStatus(), result.body());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("地址{},参数{},发送异常:{}", appAddress, params, e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 签名
|
|
|
|
+ *
|
|
|
|
+ * @param secret
|
|
|
|
+ * @param signContext
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getGenerateSign(final String secret, final String signContext) {
|
|
|
|
+ String sign = null;
|
|
|
|
+ try {
|
|
|
|
+ Mac hmacSha256 = Mac.getInstance("HmacSHA1");
|
|
|
|
+ byte[] keyBytes = secret.getBytes();
|
|
|
|
+ hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA1"));
|
|
|
|
+ sign = HexUtil.encodeHexStr(hmacSha256.doFinal(signContext.getBytes()));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return sign;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 签名字符串生成规则.
|
|
|
|
+ *
|
|
|
|
+ * @param appkey .
|
|
|
|
+ * @return 待签名字符串
|
|
|
|
+ */
|
|
|
|
+ private static String getSignContext(String path, String appkey, String msgId, String requestTime,
|
|
|
|
+ String customerCode, String userCode) {
|
|
|
|
+ StringBuilder append = new StringBuilder(path)
|
|
|
|
+ .append(appkey)
|
|
|
|
+ .append(msgId)
|
|
|
|
+ .append(requestTime);
|
|
|
|
+ if (customerCode != null) {
|
|
|
|
+ append.append(customerCode);
|
|
|
|
+ }
|
|
|
|
+ if (userCode != null) {
|
|
|
|
+ append.append(userCode);
|
|
|
|
+ }
|
|
|
|
+ System.out.println(append);
|
|
|
|
+ return append.toString().toUpperCase();
|
|
|
|
+ }
|
|
|
|
+}
|