diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml index 92a3640e9..c2bce185b 100644 --- a/ruoyi-admin/src/main/resources/application-dev.yml +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -49,17 +49,17 @@ spring: driverClassName: com.mysql.cj.jdbc.Driver # jdbc 所有参数配置参考 https://lionli.blog.csdn.net/article/details/122018562 # rewriteBatchedStatements=true 批处理优化 大幅提升批量插入更新删除性能(对数据库有性能损耗 使用批量操作应考虑性能问题) - url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true + url: jdbc:mysql://localhost:3306/rl-vue-5?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true username: root - password: root + password: Zheng@123456 # 从库数据源 - slave: - lazy: true - type: ${spring.datasource.type} - driverClassName: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true - username: - password: +# slave: +# lazy: true +# type: ${spring.datasource.type} +# driverClassName: com.mysql.cj.jdbc.Driver +# url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true +# username: +# password: # oracle: # type: ${spring.datasource.type} # driverClassName: oracle.jdbc.OracleDriver diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 83fcb99c1..33e4d02b1 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -33,6 +33,7 @@ ruoyi-common-encrypt ruoyi-common-tenant ruoyi-common-websocket + ruoyi-common-hik ruoyi-common diff --git a/ruoyi-common/ruoyi-common-hik/pom.xml b/ruoyi-common/ruoyi-common-hik/pom.xml new file mode 100644 index 000000000..8f5b8c351 --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/pom.xml @@ -0,0 +1,65 @@ + + + com.ruoyi + ruoyi-common + ${revision} + ../pom.xml + + 4.0.0 + ruoyi-common-hik + + ruoyi-common-json 序列化模块 + + + + + + com.ruoyi + ruoyi-common-core + + + + org.apache.httpcomponents + httpclient + 4.5.14 + + + + org.apache.httpcomponents + httpclient-cache + 4.5.14 + + + + org.apache.httpcomponents + httpcore + 4.4.16 + + + + org.apache.httpcomponents + httpcore-nio + 4.4.16 + + + + org.apache.httpcomponents + httpasyncclient + 4.1.5 + + + org.apache.httpcomponents + httpasyncclient-cache + 4.1.5 + + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + + + + + diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/commom/HttpClientConfig.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/commom/HttpClientConfig.java new file mode 100644 index 000000000..5b01e6e8b --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/commom/HttpClientConfig.java @@ -0,0 +1,126 @@ +package com.ruoyi.hik.commom; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * HttpClientProperties + * + * @author xuzhou + * @version v1.0.0 + * @create 2021/7/22 16:35 + */ +@Component +@ConfigurationProperties(prefix = "http.client") +public class HttpClientConfig { + /** + * 返回从连接管理器请求连接时使用的超时时间(以毫秒为单位)。 + * 默认值: -1,为无限超时。 + */ + private int connectionRequestTimeout = 5000; + + /** + * 连接超时:连接一个url的连接等待时间 + * 确定建立连接之前的超时时间(以毫秒为单位)。 + * 默认值: -1,为无限超时。 + */ + private int connectTimeout = 5000; + + /** + * 读取数据超时:连上url,获取response的返回等待时间 + * 以毫秒为单位定义套接字超时,它是等待数据的超时,或者换句话说,两个连续数据包之间的最长不活动时间。 + * 默认值: -1,为无限超时。 + */ + private int socketTimeout = 5000; + + /** + * 客户端总并行链接最大数 + */ + private int maxTotal = 300; + + /** + * 客户端每个路由最高链接最大数 + */ + private int maxPreRoute = 5; + + /** + * 连接存活时长:秒 + */ + private long connectionTimeToLive = 60; + + /** + * 重试尝试最大次数 + * 默认为3 + */ + private int retryCount = 3; + + /** + * 非幂等请求是否可以重试 + * 默认不开启 + */ + private boolean requestSentRetryEnabled = false; + + public int getConnectionRequestTimeout() { + return connectionRequestTimeout; + } + + public void setConnectionRequestTimeout(int connectionRequestTimeout) { + this.connectionRequestTimeout = connectionRequestTimeout; + } + + public int getConnectTimeout() { + return connectTimeout; + } + + public void setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + } + + public int getSocketTimeout() { + return socketTimeout; + } + + public void setSocketTimeout(int socketTimeout) { + this.socketTimeout = socketTimeout; + } + + public int getMaxTotal() { + return maxTotal; + } + + public void setMaxTotal(int maxTotal) { + this.maxTotal = maxTotal; + } + + public int getMaxPreRoute() { + return maxPreRoute; + } + + public void setMaxPreRoute(int maxPreRoute) { + this.maxPreRoute = maxPreRoute; + } + + public long getConnectionTimeToLive() { + return connectionTimeToLive; + } + + public void setConnectionTimeToLive(long connectionTimeToLive) { + this.connectionTimeToLive = connectionTimeToLive; + } + + public int getRetryCount() { + return retryCount; + } + + public void setRetryCount(int retryCount) { + this.retryCount = retryCount; + } + + public boolean isRequestSentRetryEnabled() { + return requestSentRetryEnabled; + } + + public void setRequestSentRetryEnabled(boolean requestSentRetryEnabled) { + this.requestSentRetryEnabled = requestSentRetryEnabled; + } +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HTTPClientUtil.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HTTPClientUtil.java new file mode 100644 index 000000000..5a5dd3aab --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HTTPClientUtil.java @@ -0,0 +1,331 @@ +package com.ruoyi.hik.communicationCom; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.ruoyi.hik.commom.HttpClientConfig; +import com.ruoyi.hik.domain.HikEquipmentInfo; +import com.ruoyi.hik.domain.HikSecurity; +import com.ruoyi.hik.domain.HikSecurityUserCheck; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +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.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.TimeUnit; + +public class HTTPClientUtil { + + public static CopyOnWriteArraySet copyOnWriteArraySet = new CopyOnWriteArraySet(); + + //创建XmlMapper对象,用于实体与json和xml之间的相互转换 + private static final XmlMapper xmlMapper = new XmlMapper(); + + /** + * 配置类 + */ + private static final HttpClientConfig HTTP_CLIENT_CONFIG = new HttpClientConfig();; + + // 使用工厂类 HttpClients 进行创建 + // 1、默认配置创建 + private CloseableHttpClient httpClient; + private HikEquipmentInfo equipmentInfo; + + /** + * 请求配置 + */ + private static RequestConfig requestConfig; + + // 设置要访问的HttpHost,即是目标站点的HttpHost + private HttpHost target = null; + + public static HTTPClientUtil getHikEquipmentHTTPClient(HikEquipmentInfo equipment) { + for (HTTPClientUtil util : HTTPClientUtil.copyOnWriteArraySet) { + if (util.getEquipmentInfo().getEquipmentIp().equals(equipment.getEquipmentIp())) { + return util; + } + } + return null; + } + + public HikSecurityUserCheck doLogin(HikEquipmentInfo equipment) throws IOException { + requestConfig = RequestConfig.custom() + .setAuthenticationEnabled(true) + .setConnectTimeout(HTTP_CLIENT_CONFIG.getConnectTimeout()) + .setConnectionRequestTimeout(HTTP_CLIENT_CONFIG.getConnectionRequestTimeout()) + .setSocketTimeout(HTTP_CLIENT_CONFIG.getSocketTimeout()) + .build(); + this.setTarget( new HttpHost(equipment.getEquipmentIp(), 80)); + + // 2、使用 builder来创建,可以添加自定义配置 + // 自定义 connectionManager 连接管理器 + // 配置连接池关联 + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(HTTP_CLIENT_CONFIG.getMaxTotal()); + connectionManager.setDefaultMaxPerRoute(HTTP_CLIENT_CONFIG.getMaxPreRoute()); + + // 配置登录权限相关 + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(equipment.getUsername(), equipment.getPassword()); + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY,credentials); + + + // 初始化客户端 + httpClient = HttpClients.custom() + .setConnectionManager(connectionManager) + .setDefaultRequestConfig(requestConfig) + .setDefaultCredentialsProvider(credentialsProvider) + // 重试机制 + .setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_CONFIG.getRetryCount(), HTTP_CLIENT_CONFIG.isRequestSentRetryEnabled())) + // 开启后台线程清除过期的连接 + .evictExpiredConnections() + // 开启后台线程清除闲置的连接 + .evictIdleConnections(HTTP_CLIENT_CONFIG.getConnectionTimeToLive(), TimeUnit.SECONDS) + .build(); + + + /** + * + * + * + * 200 + * + * + * OK + * + * + * true + * + * + * true + * + * + * true + * + * + * 1 + * + * + * unlock + * + * + * 1 + * + * + * 1 + * + * + */ + String url = "/ISAPI/Security/userCheck"; + + HikSecurity security = xmlMapper.readValue(this.doGet(url), HikSecurity.class); + if(security.getUserCheck().getStatusValue() ==200 ){ + copyOnWriteArraySet.add(this); + this.setEquipmentInfo(equipment); + } + + return security.getUserCheck(); + + } + + public String doGet(String url) throws IOException { + String result = null; + HttpGet method = new HttpGet(url); + method.setConfig(requestConfig); + //response 对象 + HttpResponse response = this.httpClient.execute(this.getTarget(), method); + + int statusCode = response.getStatusLine().getStatusCode(); + System.out.println(response); + if (statusCode != 200) { + method.abort(); + // throw new ServiceException("请求设备失败 错误码:" + statusCode); + } + + HttpEntity entity = response.getEntity(); + + if (entity != null) { + result = EntityUtils.toString(entity, "utf-8"); + } + EntityUtils.consume(entity); + // Release the connection + // method.reset(); + + + return result; + } + + public HttpResponse doPut(String url, String inbound) throws Exception { + + HttpPut method = new HttpPut(url); + method.setConfig(requestConfig); + + // method.setRequestBody(inbound); + StringEntity stringEntity = new StringEntity(inbound); + method.setEntity(stringEntity); + + //response 对象 + HttpResponse response = null; + response = this.httpClient.execute(this.getTarget(), method); + // Release the connection + // method.reset(); + return response; + } + + public String doPost(String url, String inbound) throws Exception { + String result = null; + HttpPost method = new HttpPost(url); + method.setConfig(requestConfig); + StringEntity stringEntity = new StringEntity(inbound); + method.setEntity(stringEntity); + + //response 对象 + HttpResponse response = this.httpClient.execute(this.getTarget(), method); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) { + method.abort(); + // throw new ServiceException("请求设备失败 错误码:" + statusCode); + } + + HttpEntity entity = response.getEntity(); + + if (entity != null) { + result = EntityUtils.toString(entity, "utf-8"); + } + EntityUtils.consume(entity); + + // Release the connection + // method.reset(); + return result; + } + + public HttpResponse doDelete(String url) throws Exception { + + HttpDelete method = new HttpDelete(url); + method.setConfig(requestConfig); + + //response 对象 + HttpResponse response = null; + response = this.httpClient.execute(this.getTarget(), method); + // Release the connection + // method.reset(); + return response; + } + + //With the binary form POst method + public HttpResponse doPostwithBinaryData(String url, String json, String jsonName, String image, String imageName, String boundary) throws Exception { + + HttpPost method = new HttpPost(url); + method.setConfig(requestConfig); + + method.setHeader("Accept", "text/html, application/xhtml+xml"); + method.setHeader("Accept-Language", "zh-CN"); + method.setHeader("Content-Type","multipart/form-data; boundary=" + boundary); + method.setHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); + method.setHeader("Accept-Encoding","gzip, deflate"); + method.setHeader("Connection","Keep-Alive"); + method.setHeader("Cache-Control","no-cache"); + + String bodyParam = + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\""+jsonName+"\";\r\n" + + "Content-Type: text/json\r\n" + + "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n" + + json + "\r\n" + + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\""+imageName+"\";\r\n" + + "Content-Type: image/jpeg\r\n" + + "Content-Length: " + Integer.toString(image.length()) + "\r\n\r\n" + + image + + "\r\n--" + boundary + "--\r\n"; + + //method.setRequestBody(bodyParam); + StringEntity stringEntity = new StringEntity(bodyParam); + method.setEntity(stringEntity); + + + //response 对象 + HttpResponse response = null; + response = this.httpClient.execute(this.getTarget(), method); + // Release the connection + // method.reset(); + return response; + } + + public HttpResponse doPostStorageCloud(String url, String json,String faceimage,String boundary) throws Exception { + + HttpPost method = new HttpPost(url); + method.setConfig(requestConfig); + + method.setHeader("Accept", "text/html, application/xhtml+xml"); + method.setHeader("Accept-Language", "zh-CN"); + method.setHeader("Content-Type","multipart/form-data; boundary=" + boundary); + method.setHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); + method.setHeader("Accept-Encoding","gzip, deflate"); + method.setHeader("Connection","Keep-Alive"); + method.setHeader("Cache-Control","no-cache"); + + String bodyParam = + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"uploadStorageCloud\";\r\n" + + "Content-Type: text/json\r\n" + + "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n" + + json + "\r\n" + + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"imageData\";\r\n" + + "Content-Type: image/jpeg\r\n" + + "Content-Length: " + Integer.toString(faceimage.length()) + "\r\n\r\n" + + faceimage + + "\r\n--" + boundary + "--\r\n"; + + // method.setRequestBody(bodyParam); + StringEntity stringEntity = new StringEntity(bodyParam); + method.setEntity(stringEntity); + + //response 对象 + HttpResponse response = null; + response = this.httpClient.execute(this.getTarget(), method); + // Release the connection + // method.reset(); + return response; + } + + +// public CloseableHttpClient getHttpClient() { +// return httpClient; +// } +// +// public void setHttpClient(CloseableHttpClient httpClient) { +// this.httpClient = httpClient; +// } + + + public HikEquipmentInfo getEquipmentInfo() { + return equipmentInfo; + } + + public void setEquipmentInfo(HikEquipmentInfo equipmentInfo) { + this.equipmentInfo = equipmentInfo; + } + + public HttpHost getTarget() { + return target; + } + + public void setTarget(HttpHost target) { + this.target = target; + } + +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HttpClientUtils.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HttpClientUtils.java new file mode 100644 index 000000000..ee8ac326d --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HttpClientUtils.java @@ -0,0 +1,358 @@ +package com.ruoyi.hik.communicationCom; + +import com.ruoyi.hik.commom.HttpClientConfig; +import com.ruoyi.hik.domain.HttpResult; +import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.HttpHeaders; +import org.apache.http.NameValuePair; +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.client.methods.HttpRequestBase; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.InputStreamEntity; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicHeader; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * http连接工具 + * 不会对请求的结果做处理,用户可以访问 {@link HttpResult} + * 通过{@linkplain HttpResult#getStatus()}判断响应码code + * 通过{@linkplain HttpResult#getStringEntity()}获取响应实体字符串 + * + * @author xuzhou + * @version v1.0.0 + */ +public class HttpClientUtils { + + /** + * Http客户端 + */ + public static final CloseableHttpClient httpClient; + + /** + * 配置类 + */ + private static final HttpClientConfig HTTP_CLIENT_CONFIG; + + /** + * 编码方式 + */ + private static final String ENCODING = "utf-8"; + + /** + * 日志对象 + */ + // private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class); + + /** + * 请求配置 + */ + private static final RequestConfig request_config; + + static { + // 配置类 + HTTP_CLIENT_CONFIG = new HttpClientConfig(); + + // 配置请求参数,请求时常,连接市场,读取数据时长 + request_config = RequestConfig.custom() + .setAuthenticationEnabled(true) + .setConnectTimeout(HTTP_CLIENT_CONFIG.getConnectTimeout()) + .setConnectionRequestTimeout(HTTP_CLIENT_CONFIG.getConnectionRequestTimeout()) + .setSocketTimeout(HTTP_CLIENT_CONFIG.getSocketTimeout()) + .build(); + + // 配置连接池关联 + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(HTTP_CLIENT_CONFIG.getMaxTotal()); + connectionManager.setDefaultMaxPerRoute(HTTP_CLIENT_CONFIG.getMaxPreRoute()); + + // 初始化客户端 + httpClient = HttpClients.custom() + .setConnectionManager(connectionManager) + .setDefaultRequestConfig(request_config) + // 重试机制 + .setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_CONFIG.getRetryCount(), HTTP_CLIENT_CONFIG.isRequestSentRetryEnabled())) + // 开启后台线程清除过期的连接 + .evictExpiredConnections() + // 开启后台线程清除闲置的连接 + .evictIdleConnections(HTTP_CLIENT_CONFIG.getConnectionTimeToLive(), TimeUnit.SECONDS) + .build(); + } + + private HttpClientUtils() { + + } + + + /** + * GET请求 + * 1.支持不带参数的请求 + * 2.支持参数拼接在URl中的请求 + * + * @param url 请求地址 + * @return 返回值 + */ + public static HttpResult doGet(String url) { + return doGet(url, null, null); + } + + /** + * 带有参数的GET请求 + * + * @param url 请求地址 + * @param params 请求参数 + * @return 返回值 + */ + public static HttpResult doGet(String url, Map params) { + return doGet(url, params, null); + } + + /** + * Get 请求:指定请求头,请求参数 + * + * @param url 请求地址 + * @param headers 请求头参数 + * @param params 请求参数 + * @return HttpResult + */ + public static HttpResult doGet(String url, Map params, Map headers) { + +// log.info("Http GET 请求URL:{}", url); +// log.info("Http GET 请求参数:{}", JSONObject.toJSONString(params)); + + try { + // 创建访问对象地址 + URIBuilder uriBuilder = new URIBuilder(url); + if (params != null && !params.isEmpty()) { + // 构建在URL中的请求参数 + Set> entrySet = params.entrySet(); + for (Map.Entry entry : entrySet) { + uriBuilder.addParameter((String) entry.getKey(), String.valueOf(entry.getValue())); + } + } + + HttpGet httpGet = new HttpGet(uriBuilder.build().toString()); + + // 封装请求头 + packageHeader(headers, httpGet); + + return execute(httpGet); + } catch (URISyntaxException e) { + // log.error("Get请求构建URL失败", e); + } + return null; + } + + /** + * 执行POST请求 + * + * @param url 请求地址 + * @return 返回值 + */ + public static HttpResult doPost(String url) { + return doPost(url, null, null); + } + + /** + * 执行POST请求:有参数 + * + * @param url 请求地址 + * @param params 请求参数 + * @return 返回值 + */ + public static HttpResult doPost(String url, Map params) { + return doPost(url, params, null); + } + + /** + * 执行POST请求 + * + * @param url 请求地址 + * @param headers 请求头 + * @param params 请求参数 + * @return 返回值 + */ + public static HttpResult doPost(String url, Map params, Map headers) { + +// log.info("Http POST 请求URL:{}", url); +// log.info("Http POST 请求参数:{}", JSONObject.toJSONString(params)); + + // 创建http POST请求 + HttpPost httpPost = new HttpPost(url); + + try { + // 封装请求头 + packageHeader(headers, httpPost); + + // 封装请求参数 + packageParam(params, httpPost); + return execute(httpPost); + + } catch (UnsupportedEncodingException e) { + // log.error("POST请求参数编码异常", e); + } + + return null; + } + + /** + * http post json数据 + * + * @param url 请求地址 + * @param json 请求参数 + * @return 返回值 + */ + public static HttpResult doPostJson(String url, String json) { + return doPostJson(url, json, null); + } + + + /** + * http post json数据 + * + * @param url 请求地址 + * @param json 请求参数 + * @param headers 请求头 + * @return 返回值 + */ + public static HttpResult doPostJson(String url, String json, Map headers) { +// log.info("Http post json请求URL:{}", url); +// log.info("Http post json请求参数:{}", json); + // 创建http POST请求 + HttpPost httpPost = new HttpPost(url); + + httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())); + httpPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain;charset=utf-8")); + + // 封装请求头 + packageHeader(headers, httpPost); + if (json != null) { + // 构造一个JSON请求的实体 + StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON); + // 将请求实体设置到httpPost对象中 + httpPost.setEntity(stringEntity); + } + return execute(httpPost); + } + + /** + * http post stream请求 + * + * @param url 请求地址 + * @param in 输入流 + * @return 返回数据 + */ + public static HttpResult doPostInputStream(String url, InputStream in) { + // 创建http POST请求 + HttpPost httpPost = new HttpPost(url); + httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString())); + httpPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain;charset=utf-8")); + + if (in != null) { + httpPost.setEntity(new InputStreamEntity(in)); + } + + return execute(httpPost); + } + + /** + * http post text请求 + * + * @param url 请求地址 + * @param text 文本内容 + * @return 返回数据 + */ + public static HttpResult doPostWrite(String url, String text) { + // 创建http POST请求 + HttpPost httpPost = new HttpPost(url); + httpPost.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString())); + httpPost.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain;charset=utf-8")); + +// if (StringUtils.isNotBlank(text)) { +// StringEntity stringEntity = new StringEntity(text, ContentType.TEXT_PLAIN); +// httpPost.setEntity(stringEntity); +// } + return execute(httpPost); + } + + /** + * 执行HTTP请求 + * + * @param request {@link HttpRequestBase} 请求 + * @return {@link HttpResult} 请求结果 + */ + public static HttpResult execute(HttpRequestBase request) { + // 执行http请求 + try (CloseableHttpResponse response = httpClient.execute(request)) { + // 构建返回实体 + return new HttpResult(response.getStatusLine().getStatusCode(), + EntityUtils.toString(response.getEntity())); + } catch (Exception e) { + // log.error("http 请求异常", e); + } + + return null; + } + + /** + * 将请求参数处理为 NameValuePair + * + * @param params 请求参数Map + * @return List + */ + public static List convertParams2NVPS(Map params) { + if (!params.isEmpty()) { + List parameters = new ArrayList<>(); + params.forEach((key, value) -> parameters.add(new BasicNameValuePair(key, String.valueOf(value)))); + return parameters; + } + return Collections.emptyList(); + } + + /** + * 封装请求头 + * + * @param headers 请求头参数列表 + * @param httpMethod 请求方式 + */ + public static void packageHeader(Map headers, HttpRequestBase httpMethod) { +// if (MapUtils.isNotEmpty(headers)) { +// Set> entrySet = headers.entrySet(); +// for (Map.Entry entry : entrySet) { +// // 设置请求头到 HttpRequestBase 对象中 +// httpMethod.setHeader(entry.getKey(), entry.getValue()); +// } +// } + } + + /** + * 封装请求参数 + * + * @param params 请求参数 + * @param httpMethod 请求方式 + * @throws UnsupportedEncodingException 不支持字符编码异常 + */ + private static void packageParam(Map params, HttpEntityEnclosingRequest httpMethod) + throws UnsupportedEncodingException { + +// if (MapUtils.isNotEmpty(params)) { +// List nameValuePairs = convertParams2NVPS(params); +// httpMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, ENCODING)); +// } + } +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HttpsClientUtil.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HttpsClientUtil.java new file mode 100644 index 000000000..684120427 --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/communicationCom/HttpsClientUtil.java @@ -0,0 +1,464 @@ +package com.ruoyi.hik.communicationCom; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; + +//import org.apache.commons.httpclient.HttpStatus; +//import org.apache.commons.httpclient.methods.PostMethod; +//import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContextBuilder; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.conn.ssl.X509HostnameVerifier; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +public class HttpsClientUtil { + public HttpsClientUtil() throws Exception + { + + } + + public static String strIP=""; + public static int iPort = 0; + public static boolean bHttpsEnabled=false; + + public static void httpsClientInit(String IP, String user, String Password) + { + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + Credentials credentials = new UsernamePasswordCredentials(user, Password); + credentialsProvider.setCredentials(new AuthScope(IP, 443), credentials); + HttpsClientUtil.httpsClient=HttpClients.custom().setSSLSocketFactory(HttpsClientUtil.createSSLConnSocketFactory()).setDefaultCredentialsProvider(credentialsProvider).build(); + } + + public static SSLConnectionSocketFactory createSSLConnSocketFactory() { + + SSLConnectionSocketFactory sslsf = null; + try { + + TrustStrategy trustStrategy=new TrustStrategy(){ + + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return true; + } + }; + + SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build(); + + X509HostnameVerifier x509HostnameVerifier=new X509HostnameVerifier(){ + + public boolean verify(String arg0, SSLSession arg1) { + return true; + } + + public void verify(String host, SSLSocket ssl) throws IOException { + } + + + public void verify(String host, X509Certificate cert) throws SSLException { + } + + public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { + } + }; + + sslsf = new SSLConnectionSocketFactory(sslContext, x509HostnameVerifier); + } catch (GeneralSecurityException e) { + e.printStackTrace(); + } + return sslsf; + } + + + public static CloseableHttpClient httpsClient=null; + + public static String httpsGet(String url) + { + String Ret=""; + try { + CloseableHttpResponse response = null; + HttpGet httpGet = new HttpGet(url); + + response = httpsClient.execute(httpGet); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + } + + public static String httpsPut(String url, String inboundInfo) + { + String Ret=""; + try { + CloseableHttpResponse response = null; + HttpPut httpPut = new HttpPut(url); + + HttpEntity inboundInfoEntity = new StringEntity(inboundInfo, "UTF-8"); + httpPut.setEntity(inboundInfoEntity); + + response = httpsClient.execute(httpPut); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + } + + public static String httpsPost(String url, String inboundInfo) + { + String Ret=""; + try { + CloseableHttpResponse response = null; + HttpPost httpPost = new HttpPost(url); + + HttpEntity inboundInfoEntity = new StringEntity(inboundInfo, "UTF-8"); + httpPost.setEntity(inboundInfoEntity); + + response = httpsClient.execute(httpPost); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + } + + public static String httpsDelete(String url) + { + String Ret=""; + try { + CloseableHttpResponse response = null; + HttpDelete httpDelete = new HttpDelete(url); + + response = httpsClient.execute(httpDelete); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + } + +public static String doPostStorageCloud(String url, String json,String faceimage,String boundary) throws Exception { + + String Ret=""; + try{ + CloseableHttpResponse response = null; + HttpPost method = new HttpPost(url); + method.addHeader("Accept", "text/html, application/xhtml+xml"); + method.addHeader("Accept-Language", "zh-CN"); + method.addHeader("Content-Type","multipart/form-data; boundary=" + boundary); + method.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); + method.addHeader("Accept-Encoding","gzip, deflate"); + method.addHeader("Connection","Keep-Alive"); + method.addHeader("Cache-Control","no-cache"); + // byte szTemp + String bodyParam = + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"uploadStorageCloud\";\r\n" + + "Content-Type: text/json\r\n" + + "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n" + + json + "\r\n" + + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"imageData\";\r\n" + + "Content-Type: image/jpeg\r\n" + + "Content-Length: " + Integer.toString(faceimage.length()) + "\r\n\r\n" + + faceimage + + "\r\n--" + boundary + "--\r\n"; + + + //HttpEntity inboundInfoEntity = new StringEntity(bodyParam, "UTF-8"); + HttpEntity inboundInfoEntity = new StringEntity(bodyParam); + String strTemp = inboundInfoEntity.toString(); + + String strTemppp = strTemp; + + method.setEntity(inboundInfoEntity); + + response = httpsClient.execute(method); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + // Release the connection + method.releaseConnection(); + + }catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + + } +public static String doModFacePicRecord(String url, String json,String faceimage,String boundary) throws Exception { + + String Ret=""; + try{ + CloseableHttpResponse response = null; + HttpPost method = new HttpPost(url); + method.addHeader("Accept", "text/html, application/xhtml+xml"); + method.addHeader("Accept-Language", "zh-CN"); + method.addHeader("Content-Type","multipart/form-data; boundary=" + boundary); + method.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); + method.addHeader("Accept-Encoding","gzip, deflate"); + method.addHeader("Connection","Keep-Alive"); + method.addHeader("Cache-Control","no-cache"); + + String bodyParam = + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"FaceDataRecord\";\r\n" + + "Content-Type: text/json\r\n" + + "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n" + + json + "\r\n" + + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"FaceImage\";\r\n" + + "Content-Type: image/jpeg\r\n" + + "Content-Length: " + Integer.toString(faceimage.length()) + "\r\n\r\n" + + faceimage + + "\r\n--" + boundary + "--\r\n"; + + + HttpEntity inboundInfoEntity = new StringEntity(bodyParam, "UTF-8"); + String strTemp = inboundInfoEntity.toString(); + method.setEntity(inboundInfoEntity); + + response = httpsClient.execute(method); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + // Release the connection + method.releaseConnection(); + + }catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + + } +public static String doPutWithType(String url, String inbound,String charset,String Type) throws Exception { + + + + String Ret=""; + try { + CloseableHttpResponse response = null; + HttpPut httpPut = new HttpPut(url); + + httpPut.addHeader("Content-Type", Type); + HttpEntity inboundInfoEntity = new StringEntity(inbound, "UTF-8"); + httpPut.setEntity(inboundInfoEntity); + + response = httpsClient.execute(httpPut); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; +} + +public static String doPostwithBinaryData(String url, String json, String jsonName, String image, String imageName, String boundary) throws Exception { + + String Ret=""; + try{ + CloseableHttpResponse response = null; + HttpPost method = new HttpPost(url); + method.addHeader("Accept", "text/html, application/xhtml+xml"); + method.addHeader("Accept-Language", "zh-CN"); + method.addHeader("Content-Type","multipart/form-data; boundary=" + boundary); + method.addHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); + method.addHeader("Accept-Encoding","gzip, deflate"); + method.addHeader("Connection","Keep-Alive"); + method.addHeader("Cache-Control","no-cache"); + // byte szTemp + String bodyParam = + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\""+jsonName+"\";\r\n" + + "Content-Type: text/json\r\n" + + "Content-Length: " + Integer.toString(json.length()) + "\r\n\r\n" + + json + "\r\n" + + "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\""+imageName+"\";\r\n" + + "Content-Type: image/jpeg\r\n" + + "Content-Length: " + Integer.toString(image.length()) + "\r\n\r\n" + + image + + "\r\n--" + boundary + "--\r\n"; + + + //HttpEntity inboundInfoEntity = new StringEntity(bodyParam, "UTF-8"); + HttpEntity inboundInfoEntity = new StringEntity(bodyParam); + String strTemp = inboundInfoEntity.toString(); + + String strTemppp = strTemp; + + method.setEntity(inboundInfoEntity); + + response = httpsClient.execute(method); + + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != HttpStatus.SC_OK) + { + Ret = "error "+statusCode; + } + + HttpEntity entity = response.getEntity(); + if (entity == null) + { + Ret = "error response is null"; + } + + Ret = EntityUtils.toString(entity, "utf-8"); + // Release the connection + method.releaseConnection(); + + }catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return Ret; + + } + +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikEquipmentInfo.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikEquipmentInfo.java new file mode 100644 index 000000000..2c42076ee --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikEquipmentInfo.java @@ -0,0 +1,58 @@ +package com.ruoyi.hik.domain; + +import lombok.Data; + +/** + * 海康设备对象 hk_equipment + * + * @author zz + * @date 2022-12-24 + */ +@Data +public class HikEquipmentInfo { + + private static final long serialVersionUID=1L; + + /** + * 设备id + */ + private Long hkEquipmentId; + /** + * 设备类型0采集器1通道2门禁3考勤机 + */ + private String equipmentType; + + /** + * 设备账号 + */ + private String username; + /** + * 设备密码 + */ + private String password; + /** + * 设备IP + */ + private String equipmentIp; + /** + * 设备端口号 + */ + private Short equipmentPort; + + + /** + * 用户句柄:登录后的操作句柄 + */ + public int lUserID = -1; + /** + * 布防句柄:布防后的操作句柄 + */ + public int lAlarmHandle = -1; + + /** + * 监听句柄:监听后的操作句柄 + */ + public int lListenHandle = -1; + + +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikSecurity.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikSecurity.java new file mode 100644 index 000000000..c16525585 --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikSecurity.java @@ -0,0 +1,10 @@ +package com.ruoyi.hik.domain; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import lombok.Data; + +@Data +public class HikSecurity { + private HikSecurityUserCheck userCheck; + +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikSecurityUserCheck.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikSecurityUserCheck.java new file mode 100644 index 000000000..aa648e4e1 --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HikSecurityUserCheck.java @@ -0,0 +1,25 @@ +package com.ruoyi.hik.domain; + +import lombok.Data; + +@Data +public class HikSecurityUserCheck { + // 200 + private Integer statusValue; + // OK + private String statusString; + // true + private Boolean isDefaultPassword; + // true + private Boolean isRiskPassword; + // true + private Boolean isActivated; + // 1 + private Integer residualValidity; + // unlock + private String lockStatus; + // 1 + private Integer unlockTime; + // 1 + private Integer retryLoginTime; +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HttpResult.java b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HttpResult.java new file mode 100644 index 000000000..00a412b5b --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/java/com/ruoyi/hik/domain/HttpResult.java @@ -0,0 +1,57 @@ +package com.ruoyi.hik.domain; + +/** + * http请求返回对象 + * + * @author xuzhou + * @version 1.0.0 + */ +public class HttpResult { + + /** + * 状态码 + */ + private Integer status; + /** + * 返回数据 + */ + private String stringEntity; + + public HttpResult() { + } + + /** + * http请求返回对象 + * + * @param status 返回状态 + * @param stringEntity 返回数据 + */ + public HttpResult(Integer status, String stringEntity) { + this.status = status; + this.stringEntity = stringEntity; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public String getStringEntity() { + return stringEntity; + } + + public void setStringEntity(String stringEntity) { + this.stringEntity = stringEntity; + } + + @Override + public String toString() { + return "HttpResult{" + + "status=" + status + + ", stringEntity='" + stringEntity + '\'' + + '}'; + } +} diff --git a/ruoyi-common/ruoyi-common-hik/src/main/resources/application.properties b/ruoyi-common/ruoyi-common-hik/src/main/resources/application.properties new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/ruoyi-common/ruoyi-common-hik/src/main/resources/application.properties @@ -0,0 +1 @@ +