diff --git a/mallinkPublicApi/src/main/java/com/iformall/utils/AppTest.java b/mallinkPublicApi/src/main/java/com/iformall/utils/AppTest.java index b07d83efc..7a24b3bb4 100644 --- a/mallinkPublicApi/src/main/java/com/iformall/utils/AppTest.java +++ b/mallinkPublicApi/src/main/java/com/iformall/utils/AppTest.java @@ -107,7 +107,7 @@ public class AppTest { /** * 创建交易信息 接口 */ - String s = HttpUtil.doPost("https://openapi.malls.iformall.com/api/thirdCircle/order", headMap, JSON.toJSONString(paramMap)); +// String s = HttpUtil.doPost("https://openapi.malls.iformall.com/api/thirdCircle/order", headMap, JSON.toJSONString(paramMap)); // String s = HttpUtil.doPost("http://localhost:7070/public/api/thirdCircle/order", headMap, JSON.toJSONString(paramMap)); @@ -122,7 +122,7 @@ public class AppTest { // String s = HttpUtil.doPost("https://openapitest.malls.iformall.com/api/thirdCircle/pointDeduction", headMap, JSON.toJSONString(paramMap)); - System.out.println(s); +// System.out.println(s); } diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/SysMonitorSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/SysMonitorSchedule.java index 3028b3be6..eba87058d 100644 --- a/mallinkSchedule/src/main/java/com/iformall/schedule/SysMonitorSchedule.java +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/SysMonitorSchedule.java @@ -31,54 +31,54 @@ public class SysMonitorSchedule { @Autowired private SysMonitorMapper sysMonitorMapper; - @Scheduled(cron = "0 */5 * * * *?") // 每隔5分钟检查一下各个网址 - public void checkUrls() { - if(isFmMonitor) { - SysMonitor q = new SysMonitor(); - q.setEnable(EnumSysMonitorStatus.ENABLE.getCode()); - List sysMonitors = sysMonitorMapper.findList(q); - - for (SysMonitor sysMonitor : sysMonitors) { - checkSysMonitor(sysMonitor); - } - } - } - - private void checkSysMonitor(SysMonitor sysMonitor) { - boolean isNormal = true; - for(int i=0;i<3;i++) { - try { - String result = HttpUtil.doGet(sysMonitor.getUrl()); - if (StringUtils.isBlank(result)) { - isNormal = false; - } else { - if (result.length() < 60) { - logger.info(result); - } else { - logger.info(result.substring(0, 50)); - } - } - } catch (RuntimeException e) { - logger.error(e.getMessage()); - isNormal = false; - } - if(isNormal) { - break; - } - } - - if(!isNormal) { - String[] receivers = fmMonitorEmails.split(","); - - String content = sysMonitor.getErrMsg(); - content = content.replace("{name}", sysMonitor.getName()); - content = content.replace("{url}", sysMonitor.getUrl()); - - //发送邮件 - logger.error(sysMonitor.getName() + "访问异常"); - mailService.sendSimpleMail(receivers, sysMonitor.getName() + "访问异常", content); - } - } +// @Scheduled(cron = "0 */5 * * * *?") // 每隔5分钟检查一下各个网址 +// public void checkUrls() { +// if(isFmMonitor) { +// SysMonitor q = new SysMonitor(); +// q.setEnable(EnumSysMonitorStatus.ENABLE.getCode()); +// List sysMonitors = sysMonitorMapper.findList(q); +// +// for (SysMonitor sysMonitor : sysMonitors) { +// checkSysMonitor(sysMonitor); +// } +// } +// } +// +// private void checkSysMonitor(SysMonitor sysMonitor) { +// boolean isNormal = true; +// for(int i=0;i<3;i++) { +// try { +// String result = HttpUtil.doGet(sysMonitor.getUrl()); +// if (StringUtils.isBlank(result)) { +// isNormal = false; +// } else { +// if (result.length() < 60) { +// logger.info(result); +// } else { +// logger.info(result.substring(0, 50)); +// } +// } +// } catch (RuntimeException e) { +// logger.error(e.getMessage()); +// isNormal = false; +// } +// if(isNormal) { +// break; +// } +// } +// +// if(!isNormal) { +// String[] receivers = fmMonitorEmails.split(","); +// +// String content = sysMonitor.getErrMsg(); +// content = content.replace("{name}", sysMonitor.getName()); +// content = content.replace("{url}", sysMonitor.getUrl()); +// +// //发送邮件 +// logger.error(sysMonitor.getName() + "访问异常"); +// mailService.sendSimpleMail(receivers, sysMonitor.getName() + "访问异常", content); +// } +// } } \ No newline at end of file diff --git a/mallinkService/src/main/java/com/iformall/service/park/impl/dahua/DaHuaUtil.java b/mallinkService/src/main/java/com/iformall/service/park/impl/dahua/DaHuaUtil.java index 138db8ac3..0c293ac64 100644 --- a/mallinkService/src/main/java/com/iformall/service/park/impl/dahua/DaHuaUtil.java +++ b/mallinkService/src/main/java/com/iformall/service/park/impl/dahua/DaHuaUtil.java @@ -42,7 +42,8 @@ public class DaHuaUtil { params.put("cipher", cipher); params.put("timestamp", System.currentTimeMillis()); try { - return HttpUtil.doPost(URL, JSONObject.toJSONString(params)); + //return HttpUtil.doPost(URL, JSONObject.toJSONString(params)); + return null; } catch (Exception e) { return null; } diff --git a/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java b/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java index 6b5c32194..3f2ec75ad 100644 --- a/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java +++ b/mallinkService/src/main/java/com/iformall/utils/HttpUtil.java @@ -23,6 +23,10 @@ import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.github.binarywang.wxpay.service.WxPayService; + +import cn.binarywang.wx.miniapp.api.WxMaService; + import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; @@ -31,9 +35,15 @@ import java.io.*; import java.net.URI; import java.net.URL; import java.nio.charset.Charset; +import java.security.KeyManagementException; import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; /** @@ -49,32 +59,32 @@ public class HttpUtil { private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"; - /** - * get请求 - * @return - */ - public static String doGet(String url) { - try { - CloseableHttpClient client = HttpClients.createDefault(); - //发送get请求 - HttpGet request = new HttpGet(url); - HttpResponse response = client.execute(request); - - /**请求发送成功,并得到响应**/ - if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { - /**读取服务器返回过来的json字符串数据**/ - String strResult = EntityUtils.toString(response.getEntity()); - - return strResult; - } - } - catch (IOException e) { - logger.error(e.getMessage()); - throw new RuntimeException(e); - } - - return null; - } +// /** +// * get请求 +// * @return +// */ +// public static String doGet(String url) { +// try { +// CloseableHttpClient client = HttpClients.createDefault(); +// //发送get请求 +// HttpGet request = new HttpGet(url); +// HttpResponse response = client.execute(request); +// +// /**请求发送成功,并得到响应**/ +// if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { +// /**读取服务器返回过来的json字符串数据**/ +// String strResult = EntityUtils.toString(response.getEntity()); +// +// return strResult; +// } +// } +// catch (IOException e) { +// logger.error(e.getMessage()); +// throw new RuntimeException(e); +// } +// +// return null; +// } /** * get请求 @@ -166,71 +176,71 @@ public class HttpUtil { return null; } - /** - * post请求(用于key-value格式的参数) - * @param url - * @param params - * @return - */ - public static String doPost(String url,Map headMap, Map params){ - - // 定义HttpClient - CloseableHttpClient client = HttpClients.createDefault(); - - BufferedReader in = null; - try { - - // 实例化HTTP方法 - HttpPost request = new HttpPost(); - request.setURI(new URI(url)); - if(headMap != null){ - Set set = headMap.keySet(); - for(String key: set){ - request.addHeader(key,headMap.get(key)); - } - } - - //设置参数 - List nvps = new ArrayList(); - for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { - String name = (String) iter.next(); - String value = String.valueOf(params.get(name)); - nvps.add(new BasicNameValuePair(name, value)); - - //System.out.println(name +"-"+value); - } - request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8)); - - HttpResponse response = client.execute(request); - int code = response.getStatusLine().getStatusCode(); - if(code == 200){ //请求成功 - in = new BufferedReader(new InputStreamReader(response.getEntity() - .getContent(),"utf-8")); - StringBuilder sb = new StringBuilder(""); - String line = ""; - String NL = System.getProperty("line.separator"); - while ((line = in.readLine()) != null) { - sb.append(line + NL); - } - - in.close(); - - client.close(); - - return sb.toString(); - } - else{ // - logger.info("状态码:" + code); - client.close(); - } - } - catch(Exception e){ - logger.error(e.getMessage()); - return null; - } - - return null; - } +// /** +// * post请求(用于key-value格式的参数) +// * @param url +// * @param params +// * @return +// */ +// public static String doPost(String url,Map headMap, Map params){ +// +// // 定义HttpClient +// CloseableHttpClient client = HttpClients.createDefault(); +// +// BufferedReader in = null; +// try { +// +// // 实例化HTTP方法 +// HttpPost request = new HttpPost(); +// request.setURI(new URI(url)); +// if(headMap != null){ +// Set set = headMap.keySet(); +// for(String key: set){ +// request.addHeader(key,headMap.get(key)); +// } +// } +// +// //设置参数 +// List nvps = new ArrayList(); +// for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { +// String name = (String) iter.next(); +// String value = String.valueOf(params.get(name)); +// nvps.add(new BasicNameValuePair(name, value)); +// +// //System.out.println(name +"-"+value); +// } +// request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8)); +// +// HttpResponse response = client.execute(request); +// int code = response.getStatusLine().getStatusCode(); +// if(code == 200){ //请求成功 +// in = new BufferedReader(new InputStreamReader(response.getEntity() +// .getContent(),"utf-8")); +// StringBuilder sb = new StringBuilder(""); +// String line = ""; +// String NL = System.getProperty("line.separator"); +// while ((line = in.readLine()) != null) { +// sb.append(line + NL); +// } +// +// in.close(); +// +// client.close(); +// +// return sb.toString(); +// } +// else{ // +// logger.info("状态码:" + code); +// client.close(); +// } +// } +// catch(Exception e){ +// logger.error(e.getMessage()); +// return null; +// } +// +// return null; +// } /** * post请求(用于key-value格式的参数,wiwide) @@ -366,111 +376,111 @@ public class HttpUtil { return null; } - /** - * post请求(用于请求json格式的参数) - * @param url - * @param params - * @return - */ - public static String doPost(String url, String params) throws Exception { - - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(url);// 创建httpPost - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Content-Type", "application/json"); - String charSet = "UTF-8"; - StringEntity entity = new StringEntity(params, charSet); - httpPost.setEntity(entity); - CloseableHttpResponse response = null; - - try { - response = httpclient.execute(httpPost); - StatusLine status = response.getStatusLine(); - int state = status.getStatusCode(); - if (state == HttpStatus.SC_OK) { - HttpEntity responseEntity = response.getEntity(); - String jsonString = EntityUtils.toString(responseEntity); - return jsonString; - } - else{ - logger.info("请求返回:"+state+"("+url+")"); - } - } - finally { - if (response != null) { - try { - response.close(); - } catch (IOException e) { - logger.error(e.getMessage()); - } - } - try { - httpclient.close(); - } catch (IOException e) { - logger.error(e.getMessage()); - } - } - return null; - } - - /** - * post请求(用于请求json格式的参数) - * @param url - * @param params - * @return - */ - public static String doPost(String url, Map headMap, String params){ - - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(url);// 创建httpPost - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Content-Type", "application/json"); - - if(headMap != null){ - Set set = headMap.keySet(); - for(String key: set){ - httpPost.addHeader(key,headMap.get(key)); - } - } - - String charSet = "UTF-8"; - StringEntity entity = new StringEntity(params, charSet); - httpPost.setEntity(entity); - CloseableHttpResponse response = null; - - try { - response = httpclient.execute(httpPost); - StatusLine status = response.getStatusLine(); - int state = status.getStatusCode(); - if (state == HttpStatus.SC_OK) { - HttpEntity responseEntity = response.getEntity(); - String jsonString = EntityUtils.toString(responseEntity); - return jsonString; - } - else{ - logger.info("请求返回:"+state+"("+url+")"); - } - } - catch(Exception e){ - logger.error(e.getMessage()); - return null; - } - finally { - if (response != null) { - try { - response.close(); - } catch (IOException e) { - logger.error(e.getMessage()); - } - } - try { - httpclient.close(); - } catch (IOException e) { - logger.error(e.getMessage()); - } - } - return null; - } +// /** +// * post请求(用于请求json格式的参数) +// * @param url +// * @param params +// * @return +// */ +// public static String doPost(String url, String params) throws Exception { +// +// CloseableHttpClient httpclient = HttpClients.createDefault(); +// HttpPost httpPost = new HttpPost(url);// 创建httpPost +// httpPost.setHeader("Accept", "application/json"); +// httpPost.setHeader("Content-Type", "application/json"); +// String charSet = "UTF-8"; +// StringEntity entity = new StringEntity(params, charSet); +// httpPost.setEntity(entity); +// CloseableHttpResponse response = null; +// +// try { +// response = httpclient.execute(httpPost); +// StatusLine status = response.getStatusLine(); +// int state = status.getStatusCode(); +// if (state == HttpStatus.SC_OK) { +// HttpEntity responseEntity = response.getEntity(); +// String jsonString = EntityUtils.toString(responseEntity); +// return jsonString; +// } +// else{ +// logger.info("请求返回:"+state+"("+url+")"); +// } +// } +// finally { +// if (response != null) { +// try { +// response.close(); +// } catch (IOException e) { +// logger.error(e.getMessage()); +// } +// } +// try { +// httpclient.close(); +// } catch (IOException e) { +// logger.error(e.getMessage()); +// } +// } +// return null; +// } + +// /** +// * post请求(用于请求json格式的参数) +// * @param url +// * @param params +// * @return +// */ +// public static String doPost(String url, Map headMap, String params){ +// +// CloseableHttpClient httpclient = HttpClients.createDefault(); +// HttpPost httpPost = new HttpPost(url);// 创建httpPost +// httpPost.setHeader("Accept", "application/json"); +// httpPost.setHeader("Content-Type", "application/json"); +// +// if(headMap != null){ +// Set set = headMap.keySet(); +// for(String key: set){ +// httpPost.addHeader(key,headMap.get(key)); +// } +// } +// +// String charSet = "UTF-8"; +// StringEntity entity = new StringEntity(params, charSet); +// httpPost.setEntity(entity); +// CloseableHttpResponse response = null; +// +// try { +// response = httpclient.execute(httpPost); +// StatusLine status = response.getStatusLine(); +// int state = status.getStatusCode(); +// if (state == HttpStatus.SC_OK) { +// HttpEntity responseEntity = response.getEntity(); +// String jsonString = EntityUtils.toString(responseEntity); +// return jsonString; +// } +// else{ +// logger.info("请求返回:"+state+"("+url+")"); +// } +// } +// catch(Exception e){ +// logger.error(e.getMessage()); +// return null; +// } +// finally { +// if (response != null) { +// try { +// response.close(); +// } catch (IOException e) { +// logger.error(e.getMessage()); +// } +// } +// try { +// httpclient.close(); +// } catch (IOException e) { +// logger.error(e.getMessage()); +// } +// } +// return null; +// } public static String payPost(String url, String params) { @@ -489,9 +499,11 @@ public class HttpUtil { } } + private static OkHttpClient OkHttpClient = new OkHttpClient(); + private static String exec(okhttp3.Request request) { try { - okhttp3.Response response = new OkHttpClient().newCall(request).execute(); + okhttp3.Response response = OkHttpClient.newCall(request).execute(); if (!response.isSuccessful()) throw new RuntimeException("Unexpected code " + response); return response.body().string(); @@ -501,20 +513,36 @@ public class HttpUtil { } } + private static Map sslContextMap = new ConcurrentHashMap(); + private static SSLContext getSSLContext(String certPath, String certPass) throws KeyStoreException, NoSuchAlgorithmException, + CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException, KeyManagementException { + String key = certPath+certPass; + SSLContext sslContext = sslContextMap.get(key); + if ( null == sslContext ) { + synchronized("sslContextLock"+key) { + sslContext = sslContextMap.get(key); + if (null == sslContext) { + KeyStore clientStore = KeyStore.getInstance("PKCS12"); + clientStore.load(new FileInputStream(certPath), certPass.toCharArray()); + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(clientStore, certPass.toCharArray()); + KeyManager[] kms = kmf.getKeyManagers(); + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kms, null, new SecureRandom()); + sslContextMap.put(key, sslContext); + } + } + } + return sslContext; + } + public static String payPostSSL(String url, String data, String certPath, String certPass) { HttpsURLConnection conn = null; OutputStream out = null; InputStream inputStream = null; BufferedReader reader = null; try { - KeyStore clientStore = KeyStore.getInstance("PKCS12"); - clientStore.load(new FileInputStream(certPath), certPass.toCharArray()); - KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); - kmf.init(clientStore, certPass.toCharArray()); - KeyManager[] kms = kmf.getKeyManagers(); - SSLContext sslContext = SSLContext.getInstance("TLS"); - - sslContext.init(kms, null, new SecureRandom()); + SSLContext sslContext = getSSLContext(certPath, certPass); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); URL _url = new URL(url); conn = (HttpsURLConnection) _url.openConnection(); @@ -554,61 +582,61 @@ public class HttpUtil { } } - private static String ClientCustomSSL(String url, String p12file, String mch_id, String xmlStr) throws Exception{ - KeyStore keyStore = KeyStore.getInstance("PKCS12"); - FileInputStream instream = new FileInputStream(new File(p12file)); - try { - keyStore.load(instream, mch_id.toCharArray()); - } finally { - instream.close(); - } - - // Trust own CA and all self-signed certs - SSLContext sslcontext = SSLContexts.custom() - .loadKeyMaterial(keyStore, mch_id.toCharArray()) - .build(); - // Allow TLSv1 protocol only - SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( - sslcontext, - new String[] { "TLSv1" }, - null, - SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); - CloseableHttpClient httpclient = HttpClients.custom() - .setSSLSocketFactory(sslsf) - .build(); - try { - HttpPost httpPost = new HttpPost(url); - StringEntity entityStr = new StringEntity(xmlStr); - entityStr.setContentType("text/xml"); - //logger.info("entityStr--------------"+entityStr); - httpPost.setEntity(entityStr); - - CloseableHttpResponse response = httpclient.execute(httpPost); - try { - HttpEntity entity = response.getEntity(); - - //System.out.println("----------------------------------------"); - //System.out.println(response.getStatusLine()); - if (entity != null) { - //System.out.println("Response content length: " + entity.getContentLength()); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); - StringBuilder sb = new StringBuilder(); - String line = null; - while ((line = bufferedReader.readLine()) != null) { - sb.append(line).append("\n"); - } - return sb.toString(); - } - EntityUtils.consume(entity); - } finally { - response.close(); - } - } finally { - httpclient.close(); - return null; - } - - } +// private static String ClientCustomSSL(String url, String p12file, String mch_id, String xmlStr) throws Exception{ +// KeyStore keyStore = KeyStore.getInstance("PKCS12"); +// FileInputStream instream = new FileInputStream(new File(p12file)); +// try { +// keyStore.load(instream, mch_id.toCharArray()); +// } finally { +// instream.close(); +// } +// +// // Trust own CA and all self-signed certs +// SSLContext sslcontext = SSLContexts.custom() +// .loadKeyMaterial(keyStore, mch_id.toCharArray()) +// .build(); +// // Allow TLSv1 protocol only +// SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( +// sslcontext, +// new String[] { "TLSv1" }, +// null, +// SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); +// CloseableHttpClient httpclient = HttpClients.custom() +// .setSSLSocketFactory(sslsf) +// .build(); +// try { +// HttpPost httpPost = new HttpPost(url); +// StringEntity entityStr = new StringEntity(xmlStr); +// entityStr.setContentType("text/xml"); +// //logger.info("entityStr--------------"+entityStr); +// httpPost.setEntity(entityStr); +// +// CloseableHttpResponse response = httpclient.execute(httpPost); +// try { +// HttpEntity entity = response.getEntity(); +// +// //System.out.println("----------------------------------------"); +// //System.out.println(response.getStatusLine()); +// if (entity != null) { +// //System.out.println("Response content length: " + entity.getContentLength()); +// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); +// StringBuilder sb = new StringBuilder(); +// String line = null; +// while ((line = bufferedReader.readLine()) != null) { +// sb.append(line).append("\n"); +// } +// return sb.toString(); +// } +// EntityUtils.consume(entity); +// } finally { +// response.close(); +// } +// } finally { +// httpclient.close(); +// return null; +// } +// +// } }