news 2026/7/14 17:33:39

手写海康OpenApi签名规范,实现手动调用api(sdk:artemis-http-client)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
手写海康OpenApi签名规范,实现手动调用api(sdk:artemis-http-client)

1. 前言:

artemis-http-clientsdk 中提供获取门禁事件图片的方法,但实际图片访问地址为该响应的重定向地址
问题来了:虽然他提供了 sdk ,但没有办法通过 sdk 获取重定向的地址于是产生了本文,自己通过hutools的 httpUtil调用

2. 签名工具类

HikSignUtil 记得先引入一下 artemis-http-client

packagecom.lxsy.util;importcn.hutool.core.codec.Base64;importcn.hutool.crypto.digest.DigestUtil;importcn.hutool.crypto.digest.HMac;importcn.hutool.crypto.digest.HmacAlgorithm;importjava.nio.charset.StandardCharsets;importjava.time.ZoneOffset;importjava.time.ZonedDateTime;importjava.time.format.DateTimeFormatter;importjava.util.HashMap;importjava.util.Map;/** * 海康 OpenAPI 签名工具类 * 签名算法:HmacSHA256 + Base64 */publicclassHikSignUtil{privatestaticfinalDateTimeFormatterRFC_1123_FORMATTER=DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC);/** * 生成请求头(包含签名) * * @param appKey 应用Key * @param appSecret 应用Secret * @param url 请求的相对路径(如 /artemis/api/acs/v1/event/pictures) * @param body 请求体(JSON字符串) * @return 包含签名的请求头Map */publicstaticMap<String,String>buildHeaders(StringappKey,StringappSecret,Stringurl,Stringbody){// Content-MD5:请求体的MD5值,Base64编码StringcontentMd5=Base64.encode(DigestUtil.md5(body));// Date 头(RFC1123)Stringdate=RFC_1123_FORMATTER.format(ZonedDateTime.now(ZoneOffset.UTC));// 签名字符串拼接// httpHeaders = HTTP METHOD + "\n" + Accept + "\n" + Content-MD5 + "\n" + Content-Type + "\n" + Date + "\n"// customHeaders = "x-ca-key" + ":" + appKey + "\n"// signString = httpHeaders + customHeaders + urlStringBuildersignBuilder=newStringBuilder();signBuilder.append("POST").append("\n");// HTTP METHODsignBuilder.append("*/*").append("\n");// AcceptsignBuilder.append(contentMd5).append("\n");// Content-MD5signBuilder.append("application/json").append("\n");// Content-TypesignBuilder.append(date).append("\n");// DatesignBuilder.append("x-ca-key:").append(appKey).append("\n");// customHeaderssignBuilder.append(url);// urlStringsignString=signBuilder.toString();// 使用 HmacSHA256 算法计算签名HMachmac=newHMac(HmacAlgorithm.HmacSHA256,appSecret.getBytes(StandardCharsets.UTF_8));Stringsignature=Base64.encode(hmac.digest(signString));// 构建请求头Map<String,String>headers=newHashMap<>();headers.put("Accept","*/*");headers.put("Content-Type","application/json");headers.put("Content-MD5",contentMd5);headers.put("Date",date);headers.put("x-ca-key",appKey);headers.put("x-ca-signature",signature);headers.put("x-ca-signature-headers","x-ca-key");returnheaders;}}

3. 使用

示例1

protectedstaticfinalArtemisConfigartemisConfig=newArtemisConfig("192.XX.13.XX:443","123","123");/** * 使用自建 HttpClient 调用海康接口,便于排查网络/签名问题 */publicstaticPageResponseResult<EventInfoDto>getEventListWithHttpClient(EventsRequesteventsRequest)throwsException{if(eventsRequest==null){eventsRequest=newEventsRequest();}Stringurl="/artemis/api/acs/v2/door/events";StringfullUrl="https://"+artemisConfig.getHost()+url;Stringbody=JSON.toJSONString(eventsRequest);Map<String,String>headers=HikSignUtil.buildHeaders(artemisConfig.getAppKey(),artemisConfig.getAppSecret(),url,body);try(CloseableHttpClienthttpClient=createHttpClient(false)){HttpPosthttpPost=newHttpPost(fullUrl);headers.forEach(httpPost::setHeader);httpPost.setEntity(newStringEntity(body,ContentType.APPLICATION_JSON));try(CloseableHttpResponseresponse=httpClient.execute(httpPost)){intstatus=response.getStatusLine().getStatusCode();StringrespBody=response.getEntity()==null?null:EntityUtils.toString(response.getEntity());if(status==HttpStatus.SC_OK){returncheckResp(respBody,newTypeReference<PageResponseResult<EventInfoDto>>(){});}thrownewException("获取事件列表失败,状态码: "+status+", 响应: "+respBody);}}}

示例2

protectedstaticfinalArtemisConfigartemisConfig=newArtemisConfig("192.XX.13.XX:443","123","123");publicstaticStringpictures(StringsvrIndexCode,StringpicUri)throwsException{Stringurl="/artemis/api/acs/v1/event/pictures";StringfullUrl="https://"+artemisConfig.getHost()+url;// 构建请求体PicturesRequestrequest=newPicturesRequest(svrIndexCode,picUri);Stringbody=JSON.toJSONString(request);// 使用签名工具生成请求头Map<String,String>headers=HikSignUtil.buildHeaders(artemisConfig.getAppKey(),artemisConfig.getAppSecret(),url,body);try(CloseableHttpClienthttpClient=createHttpClient(false)){HttpPosthttpPost=newHttpPost(fullUrl);headers.forEach(httpPost::setHeader);httpPost.setEntity(newStringEntity(body,ContentType.APPLICATION_JSON));try(CloseableHttpResponseresponse=httpClient.execute(httpPost)){intstatus=response.getStatusLine().getStatusCode();if(status==HttpStatus.SC_MOVED_TEMPORARILY||status==HttpStatus.SC_MOVED_PERMANENTLY||status==HttpStatus.SC_SEE_OTHER||status==HttpStatus.SC_TEMPORARY_REDIRECT||status==HTTP_STATUS_PERMANENT_REDIRECT){HeaderlocationHeader=response.getFirstHeader("Location");if(locationHeader!=null&&StrUtil.isNotBlank(locationHeader.getValue())){returnlocationHeader.getValue();}thrownewException("302重定向但未获取到Location头");}StringrespBody=response.getEntity()==null?null:EntityUtils.toString(response.getEntity());thrownewException("获取图片失败,状态码: "+status+", 响应: "+respBody);}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/13 10:37:33

哔哩下载姬深度解析:全方位掌握B站视频高效下载技巧

在当今数字化内容爆炸的时代&#xff0c;B站用户经常面临优质视频无法离线保存的困境。哔哩下载姬作为专业的B站视频下载解决方案&#xff0c;彻底解决了视频保存的技术难题&#xff0c;支持从480P到8K的全画质下载&#xff0c;配备智能批量管理和精准链接解析功能&#xff0c;…

作者头像 李华
网站建设 2026/7/14 4:29:05

Solidity入门:从零开始编写第一个智能合约

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 在快马平台上生成一个适合初学者的Solidity智能合约教程项目。合约功能简单&#xff0c;如存储和读取一个字符串。提供详细的代码注释和部署步骤&#xff0c;帮助新手快速上手。使用…

作者头像 李华
网站建设 2026/7/14 2:24:02

10分钟构建verification failed:(0x1a)错误监控原型

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个轻量级错误监控原型&#xff0c;功能包括&#xff1a;1)实时日志捕获 2)verification failed:(0x1a)错误模式识别 3)错误分级(严重/警告/提示) 4)自动生成诊断报告 5)Teams…

作者头像 李华
网站建设 2026/7/14 16:29:26

用D盾快速构建安全检测原型系统

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个快速原型系统&#xff0c;演示D盾的核心功能。系统应允许用户上传代码片段&#xff0c;自动扫描并显示检测结果。支持自定义规则和简单的结果过滤功能。使用Kimi-K2模型生成…

作者头像 李华
网站建设 2026/7/14 0:35:04

5分钟快速验证:你的项目是否会有模块导入问题

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个项目预检工具&#xff0c;能够&#xff1a;1. 自动扫描项目中的模块使用情况 2. 识别潜在的导入兼容性问题 3. 生成可视化兼容性报告 4. 提供一键配置修复 5. 支持多种框架…

作者头像 李华
网站建设 2026/7/14 6:01:39

传统vs现代:ARM编译器问题解决效率对比

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个对比演示工具&#xff0c;左侧展示传统解决ARM编译器版本问题的步骤&#xff1a;手动检查版本、查阅文档、下载安装、配置环境变量、修改makefile等。右侧展示现代解决方案…

作者头像 李华