|
|
@@ -0,0 +1,340 @@
|
|
|
+package com.studenthotel.yushi;
|
|
|
+
|
|
|
+
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.ParseException;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.*;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+public class BaseClass {
|
|
|
+
|
|
|
+ //NVR基本信息
|
|
|
+ //private static String nvrip = "192.168.0.101";//nvr 地址
|
|
|
+ private static String nvrip = "172.22.45.25";//nvr 地址
|
|
|
+
|
|
|
+ private static String nvrport = "80";//nvr端口
|
|
|
+ private static String nvruname = "admin";//登录用户名
|
|
|
+ private static String nvrpwd = "ncjtxy@2021";//登录密码
|
|
|
+ //登录NVR使用的url,三方所有接口调用都需要用到
|
|
|
+ private static String urlString = "/LAPI/V1.0/System/Security/Login";
|
|
|
+
|
|
|
+
|
|
|
+ //报文头相关字段默认值,目前这些值都是写死的,可不用关注;
|
|
|
+ private static String UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
|
|
|
+ private static String ContentType = "application/json";
|
|
|
+ private static String AcceptEncoding = "gzip, deflate";
|
|
|
+ private static String AcceptLanguage = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3";
|
|
|
+ private static String Connection = "Close";
|
|
|
+
|
|
|
+
|
|
|
+ public static String getNvrUrl() {
|
|
|
+ return "http://" + nvrip + ":" + nvrport;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getNvrName() {
|
|
|
+ return nvruname;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getNvrPwd() {
|
|
|
+ return nvrpwd;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getLoginUrl() {
|
|
|
+ return "http://" + nvrip + ":" + nvrport + urlString;
|
|
|
+ }
|
|
|
+
|
|
|
+ //拼装报文头,目前LAPI调用,报文头除鉴权信息外,其他都是写死的
|
|
|
+ public static Map getHeadInfo() {
|
|
|
+ Map<String, String> headinfo = new HashMap<String, String>();
|
|
|
+ headinfo.put("User-Agent", UserAgent);
|
|
|
+ headinfo.put("Content-Type", ContentType);
|
|
|
+ headinfo.put("Accept-Encoding", AcceptEncoding);
|
|
|
+ headinfo.put("Accept-Language", AcceptLanguage);
|
|
|
+ headinfo.put("Host", nvrip);
|
|
|
+ headinfo.put("Connection", Connection);
|
|
|
+
|
|
|
+ //三方登录时无需加cookie字段,只有web登录时,需要带
|
|
|
+ //headinfo.put("Cookie", "len=0; WebLoginHandle=10081124");
|
|
|
+
|
|
|
+ return headinfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ //put方法,不带鉴权信息时Authorization参数使用“”(空字符串),不带body时jsonBody使用“”(空字符串)
|
|
|
+ public static CloseableHttpResponse doPut(CloseableHttpClient httpClient, String url, String Authorization, String jsonBody) {
|
|
|
+ //获取报文头,目前LAPI调用,报文头除鉴权信息外,其他都是写死的
|
|
|
+ Map<String, String> headinfo = getHeadInfo();
|
|
|
+
|
|
|
+ //如果有鉴权信息则放入报文头中
|
|
|
+ if (Authorization.indexOf("response") > 0) {
|
|
|
+ headinfo.put("Authorization", Authorization);
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse httpResponse = null;
|
|
|
+
|
|
|
+ HttpPut httpPut = new HttpPut(url);
|
|
|
+ for (String s : headinfo.keySet()) {
|
|
|
+ httpPut.addHeader(s, headinfo.get(s));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!jsonBody.equals("")) {
|
|
|
+ //给httppost对象设置json字符串参数
|
|
|
+ StringEntity httpEntity = new StringEntity(jsonBody, "utf-8");
|
|
|
+
|
|
|
+ //传参
|
|
|
+ httpPut.setEntity(httpEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(6000)// 请求超时时间
|
|
|
+ .setSocketTimeout(6000)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ // 为httpPut实例设置配置
|
|
|
+ httpPut.setConfig(requestConfig);
|
|
|
+ // 执行Put请求得到返回对象
|
|
|
+ httpResponse = httpClient.execute(httpPut);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return httpResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ //post方法,Authorization必填
|
|
|
+ public static CloseableHttpResponse doPost(CloseableHttpClient httpClient, String url, String Authorization, String json) {
|
|
|
+ //获取报文头,目前LAPI调用,报文头除鉴权信息外,其他都是写死的
|
|
|
+ Map<String, String> headinfo = getHeadInfo();
|
|
|
+
|
|
|
+ //如果有鉴权信息则放入报文头中
|
|
|
+ if (Authorization.indexOf("response") > 0) {
|
|
|
+ headinfo.put("Authorization", Authorization);
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse httpResponse = null;
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ for (String s : headinfo.keySet()) {
|
|
|
+ httpPost.addHeader(s, headinfo.get(s));
|
|
|
+ }
|
|
|
+
|
|
|
+// // 创建cookie store的本地实例
|
|
|
+// BasicCookieStore basicCookieStore = new BasicCookieStore();
|
|
|
+// Cookie c = new Cookie("","");
|
|
|
+// //设置cookie的最大生存时间为零
|
|
|
+// c.setMaxAge(0);//项目所有目录均有效,这句很关键,否则不敢保证删除
|
|
|
+// basicCookieStore.addCookie((org.apache.http.cookie.Cookie) c);
|
|
|
+// HttpClient httpclient = HttpClientBuilder.create().setDefaultCookieStore(basicCookieStore).build();
|
|
|
+
|
|
|
+
|
|
|
+ //给httppost对象设置json字符串参数
|
|
|
+ StringEntity httpEntity = new StringEntity(json, "utf-8");
|
|
|
+
|
|
|
+ //传参
|
|
|
+ httpPost.setEntity(httpEntity);
|
|
|
+
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(6000)// 请求超时时间
|
|
|
+ .setSocketTimeout(6000)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ // 为httpPost实例设置配置
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+ // 执行Post请求得到返回对象
|
|
|
+ httpResponse = httpClient.execute(httpPost);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return httpResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ //get方法,Authorization必填
|
|
|
+ public static CloseableHttpResponse doGet(CloseableHttpClient httpClient, String url, String Authorization) {
|
|
|
+ //获取报文头,目前LAPI调用,报文头除鉴权信息外,其他都是写死的
|
|
|
+ Map<String, String> headinfo = getHeadInfo();
|
|
|
+
|
|
|
+ //如果有鉴权信息则放入报文头中
|
|
|
+ if (Authorization.indexOf("response") > 0) {
|
|
|
+ headinfo.put("Authorization", Authorization);
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse httpResponse = null;
|
|
|
+
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ for (String s : headinfo.keySet()) {
|
|
|
+ httpGet.addHeader(s, headinfo.get(s));
|
|
|
+ }
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(6000)// 请求超时时间
|
|
|
+ .setSocketTimeout(6000)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ // 为httpGet实例设置配置
|
|
|
+ httpGet.setConfig(requestConfig);
|
|
|
+ // 执行Get请求得到返回对象
|
|
|
+ httpResponse = httpClient.execute(httpGet);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return httpResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static CloseableHttpResponse doDelete(CloseableHttpClient httpClient, String url, String Authorization, String jsonBody) {
|
|
|
+ //获取报文头,目前LAPI调用,报文头除鉴权信息外,其他都是写死的
|
|
|
+ Map<String, String> headinfo = getHeadInfo();
|
|
|
+
|
|
|
+ //如果有鉴权信息则放入报文头中
|
|
|
+ if (Authorization.indexOf("response") > 0) {
|
|
|
+ headinfo.put("Authorization", Authorization);
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpResponse httpResponse = null;
|
|
|
+
|
|
|
+ HttpDelete httpDelete = new HttpDelete(url);
|
|
|
+ for (String s : headinfo.keySet()) {
|
|
|
+ httpDelete.addHeader(s, headinfo.get(s));
|
|
|
+ }
|
|
|
+
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)// 连接主机服务超时时间
|
|
|
+ .setConnectionRequestTimeout(6000)// 请求超时时间
|
|
|
+ .setSocketTimeout(6000)// 数据读取超时时间
|
|
|
+ .build();
|
|
|
+ try {
|
|
|
+ // 为httpPut实例设置配置
|
|
|
+ httpDelete.setConfig(requestConfig);
|
|
|
+ // 执行Put请求得到返回对象
|
|
|
+ httpResponse = httpClient.execute(httpDelete);
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return httpResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //计算cnonce值,cnonce用于鉴权
|
|
|
+ public static String getCnonce() {
|
|
|
+ double d = Math.random();
|
|
|
+// double d1 = new Date().getTime() / 1000;
|
|
|
+ double d1 = System.currentTimeMillis()/ 1000;
|
|
|
+ double x = d * d1;
|
|
|
+ DecimalFormat df = new DecimalFormat("#");//四舍五入取整
|
|
|
+ return df.format(x);
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算登录报文头中鉴权信息中的response值
|
|
|
+ public static String getAuthRespInfo(String uname, String upwd, String url, String nonce, String cnonce, String reqmode) {
|
|
|
+
|
|
|
+ //计算HA1
|
|
|
+ String str1 = uname + ":" + "NVRDVR" + ":" + upwd;
|
|
|
+ String ha1 = DigestUtils.md5DigestAsHex(str1.getBytes()).toLowerCase();
|
|
|
+ //String ha1 = md5.toMd5(str1).toLowerCase();
|
|
|
+ //计算HA2
|
|
|
+ String str2 = reqmode + ":" + url;
|
|
|
+ String ha2 = DigestUtils.md5DigestAsHex(str2.getBytes()).toLowerCase();
|
|
|
+ //String ha2 = md5.toMd5(str2).toLowerCase();
|
|
|
+ //计算HA3
|
|
|
+ String str3 = ha1 + ":" + nonce + ":" + "00000001" + ":" + cnonce + ":" + "auth" + ":" + ha2;
|
|
|
+ String ha3 = DigestUtils.md5DigestAsHex(str3.getBytes()).toLowerCase();
|
|
|
+ //String ha3 = md5.toMd5(str3).toLowerCase();
|
|
|
+
|
|
|
+ //拼接报文头中的鉴权信息
|
|
|
+ String Authorization = String.format("Digest username=\"%s\", realm=\"NVRDVR\", qop=auth,nonce=\"%s\""
|
|
|
+ + ",algorithm=MD5,cnonce=\"%s\", nc=00000001,uri=\"%s\", response=\"%s\"", uname, nonce, cnonce, url, ha3);
|
|
|
+ return Authorization;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //正则表达式提取字符串
|
|
|
+ public static String getMatch(String regex, String source) {
|
|
|
+ String rel = "";
|
|
|
+ Pattern pattern = Pattern.compile(regex);
|
|
|
+ Matcher matcher = pattern.matcher(source);
|
|
|
+ while (matcher.find()) {
|
|
|
+ rel = matcher.group(1);
|
|
|
+ }
|
|
|
+ return rel;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取response中header中字段的值,例如getHeaderinfo(resp,"WWW-Authenticate")从一个resp的报文头中获取WWW-Authenticate的值
|
|
|
+ public static String getHeaderinfo(HttpResponse resp, String keyword) {
|
|
|
+ return resp.getFirstHeader(keyword).getValue();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //运行,用于测试类正确行用,可以不用关注。
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String cururl = getNvrUrl() + urlString;
|
|
|
+ CloseableHttpClient httpClient = HttpClients.custom().build();
|
|
|
+
|
|
|
+ //不带鉴权信息发送请求,获取nonce
|
|
|
+ CloseableHttpResponse resp = doPut(httpClient, cururl, "", "");
|
|
|
+
|
|
|
+ //从返回中获取nonce值
|
|
|
+ String nonce = getMatch("nonce=\"(.*)\",stale", getHeaderinfo(resp, "WWW-Authenticate"));
|
|
|
+
|
|
|
+ //生成cnonce
|
|
|
+ String cnonce = getCnonce();
|
|
|
+
|
|
|
+ //生成报文头中Authorization字段的值
|
|
|
+ String curAuthorization = getAuthRespInfo(nvruname, nvrpwd, urlString, nonce, cnonce, "PUT");
|
|
|
+ System.out.println("curAuthorization = " + curAuthorization);
|
|
|
+
|
|
|
+ //带鉴权信息再次调用接口
|
|
|
+ CloseableHttpResponse resp2 = doPut(httpClient, cururl, curAuthorization, "");
|
|
|
+ System.out.println("resp2 = " + resp2);
|
|
|
+ HttpEntity entity = resp2.getEntity();
|
|
|
+
|
|
|
+ try {
|
|
|
+ String responString = EntityUtils.toString(entity);
|
|
|
+ //body中返回信息为Succeed,code为0即为登录成功
|
|
|
+ System.out.println(responString);
|
|
|
+ } catch (ParseException | IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ // 关闭相关资源
|
|
|
+ if (null != resp) {
|
|
|
+ try {
|
|
|
+ resp.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null != resp2) {
|
|
|
+ try {
|
|
|
+ resp2.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null != httpClient) {
|
|
|
+ try {
|
|
|
+ httpClient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|