diff --git a/src/main/java/cn/com/tenlion/usercenter/utils/SMCHttpUtil.java b/src/main/java/cn/com/tenlion/usercenter/utils/SMCHttpUtil.java index a015143..c7a49e7 100644 --- a/src/main/java/cn/com/tenlion/usercenter/utils/SMCHttpUtil.java +++ b/src/main/java/cn/com/tenlion/usercenter/utils/SMCHttpUtil.java @@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import ink.wgink.common.manager.env.EnvManager; import ink.wgink.login.base.manager.ConfigManager; +import org.apache.commons.lang3.StringUtils; import javax.net.ssl.*; import java.io.*; @@ -34,7 +35,9 @@ import java.util.Map.Entry; */ public class SMCHttpUtil { - /** 请求超时时间 */ + /** + * 请求超时时间 + */ private static Integer TIMEOUT = 3000; private static final String CHECK_MEETING_URL = "huaweiSmcCheckMeetingUrl"; private static final String CHECK_MEETING_NAME_URL = "huaweiSmcCheckMeetingNameUrl"; @@ -50,6 +53,9 @@ public class SMCHttpUtil { HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String urlHostName, SSLSession session) { + if (StringUtils.isBlank(urlHostName)) { + return false; + } return true; } }); @@ -60,7 +66,7 @@ public class SMCHttpUtil { private static void trustAllHttpsCertificates() throws NoSuchAlgorithmException, KeyManagementException { TrustManager[] trustAllCerts = new TrustManager[1]; trustAllCerts[0] = new TrustAllManager(); - SSLContext sc = SSLContext.getInstance("SSL"); + SSLContext sc = SSLContext.getInstance("TLSv1.2"); sc.init(null, trustAllCerts, null); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } @@ -71,33 +77,41 @@ public class SMCHttpUtil { } public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { + if (certs == null && StringUtils.isBlank(authType)) { + throw new CertificateException("异常"); + } } public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { + if (certs == null && StringUtils.isBlank(authType)) { + throw new CertificateException("异常"); + } } } /** * 删除会议室 + * * @param meetingId 会议室ID * @return * @throws Exception */ public void deleteMeeting(String meetingId) throws Exception { - List> list = new ArrayList>(); - Map param = new HashMap(); + List> list = new ArrayList>(); + Map param = new HashMap(); param.put("id", meetingId); list.add(param); String json = JSONObject.toJSONString(list); String result = doJson(EnvManager.getInstance().getValue(DELETE_MEETING_URL), "DELETE", json); - if(result.contains("error")) { + if (result.contains("error")) { throw new Exception("删除会议室失败"); } } /** * 创建会议室 - * @param name 名称 (例 : 集宁区-张三丰) + * + * @param name 名称 (例 : 集宁区-张三丰) * @param username 账号 * @return * @throws Exception @@ -107,7 +121,7 @@ public class SMCHttpUtil { params.put("name", username); // 判断是否已存在 Boolean exists = checkMeetingExists(username); - if(exists) { + if (exists) { throw new Exception("该账号已存在会议室"); } // 判断是否已存在 @@ -130,7 +144,7 @@ public class SMCHttpUtil { String json = JSONObject.toJSONString(create); try { String result = doJson(EnvManager.getInstance().getValue(CREATE_MEETING_URL), "POST", json); - if(result.contains("MEETINGROOM_NAME_EXIST")) { + if (result.contains("MEETINGROOM_NAME_EXIST")) { throw new Exception("该名称已存在会议室"); } SMCMeetingDTO dto = new SMCMeetingDTO(); @@ -139,13 +153,14 @@ public class SMCHttpUtil { dto.setNumber(number); dto.setUsername(username); return dto; - }catch (Exception e) { + } catch (Exception e) { throw e; } } /** * 获取随机的会议终端号码 + * * @return */ private String getMeetingNumber() throws Exception { @@ -153,7 +168,7 @@ public class SMCHttpUtil { // params.put("zoneid", "06e26a21-36b9-47e9-aabd-944eb7a33e23"); params.put("number", 1); // 数量 String result = doPost(EnvManager.getInstance().getValue(GET_MEETING_NUMBER_URL), params, null); - if(result.contains("error")) { + if (result.contains("error")) { throw new Exception("获取随机的会议终端号失败"); } JSONArray jsonArray = JSONObject.parseArray(result); @@ -162,6 +177,7 @@ public class SMCHttpUtil { /** * 验证会议室是否已经存在 + * * @return */ public Boolean checkMeetingExists(String username) throws Exception { @@ -175,6 +191,7 @@ public class SMCHttpUtil { /** * 验证会议室是否已经存在 + * * @return */ public Boolean checkMeetingNameExists(String name) throws Exception { @@ -213,12 +230,13 @@ public class SMCHttpUtil { /** * jsonPost请求 - * @Description: TODO - * @param @param postUrl - * @param @param json + * + * @param @param postUrl + * @param @param json * @param @return * @return String * @throws + * @Description: TODO * @author 崔宝铖 * @date 2019年6月19日 */ @@ -236,9 +254,9 @@ public class SMCHttpUtil { httpUrlConnection.setRequestMethod(type); httpUrlConnection.setRequestProperty("X-HW-ID", "697db861-bc1b-4e31-bc75-969e1ab5a9a5"); httpUrlConnection.setRequestProperty("X-HW-APPKEY", "H!UkcRIWb/lI1034/w5T3h%c5..4#8.+L-085%g!3Kq$w.vg3w$QD0!%i#nqE=83"); - httpUrlConnection.setRequestProperty("Connection","keep-Alive"); + httpUrlConnection.setRequestProperty("Connection", "keep-Alive"); //设置发送文件类型 - httpUrlConnection.setRequestProperty("Content-Type","application/json"); + httpUrlConnection.setRequestProperty("Content-Type", "application/json"); // 输入 输出 都打开 httpUrlConnection.setDoOutput(true); httpUrlConnection.setDoInput(true); @@ -249,24 +267,28 @@ public class SMCHttpUtil { out.write(json.getBytes()); out.flush(); // 读取数据 - br = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(),"utf-8")); + br = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(), "utf-8")); String line = null; - while (null != (line=br.readLine())){ + while (null != (line = br.readLine())) { response += line; } // 获得URL的响应状态码 status = new Integer(httpUrlConnection.getResponseCode()).toString(); } catch (Exception e) { e.printStackTrace(); - }finally { + } finally { try { - if (out != null) { out.close();} - if (br != null) {br.close();} + if (out != null) { + out.close(); + } + if (br != null) { + br.close(); + } } catch (Exception ex) { ex.printStackTrace(); } } - if(!status.startsWith("2")) { + if (!status.startsWith("2")) { System.out.println(response); throw new Exception("请求结果状态不是200"); } @@ -275,20 +297,21 @@ public class SMCHttpUtil { /** * Post请求 - * @Description: TODO - * @param @param postUrl - * @param @param param + * + * @param @param postUrl + * @param @param param * @param @return * @return String * @throws IOException * @throws + * @Description: TODO * @author 崔宝铖 * @date 2019年6月19日 */ - private static String doPost(String postUrl, Map param, JSONObject jsonObject) throws Exception { + private static String doPost(String postUrl, Map param, JSONObject jsonObject) throws Exception { // 封装发送的请求参数 StringBuffer buffer = new StringBuffer(); - if(param != null) { + if (param != null) { buffer.append("?"); int x = 0; for (Entry map : param.entrySet()) { @@ -317,14 +340,14 @@ public class SMCHttpUtil { String status = "";// 响应状态 PrintWriter out = null; BufferedReader in = null; - try{ + try { // 获取URLConnection对象对应的输出流 out = new PrintWriter(httpUrlConnection.getOutputStream()); // 发送请求参数 - if(buffer.toString().length() > 0) { + if (buffer.toString().length() > 0) { out.write(buffer.toString()); } - if(jsonObject != null) { + if (jsonObject != null) { String json = java.net.URLEncoder.encode(jsonObject.toString(), "utf-8"); out.write(json); } @@ -339,17 +362,21 @@ public class SMCHttpUtil { } // 获得URL的响应状态码 status = new Integer(httpUrlConnection.getResponseCode()).toString(); - }catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); - }finally { + } finally { try { - if (out != null) { out.close();} - if (in != null) {in.close();} + if (out != null) { + out.close(); + } + if (in != null) { + in.close(); + } } catch (Exception ex) { ex.printStackTrace(); } } - if(!status.startsWith("2")) { + if (!status.startsWith("2")) { System.out.println(response); throw new Exception("请求结果状态不是200"); } @@ -358,37 +385,39 @@ public class SMCHttpUtil { /** * Get请求 - * @Description: TODO - * @param @param getUrl 请求地址 - * @param @param param 请求参数 + * + * @param @param getUrl 请求地址 + * @param @param param 请求参数 * @param @return * @param @throws Exception * @return RetBody * @throws + * @Description: TODO * @author 崔宝铖 * @date 2019年6月19日 */ - private static String doGet(String getUrl, Map param, Map property) throws Exception { + private static String doGet(String getUrl, Map param, Map property) throws Exception { return doInitToken(getUrl, param, property); } /** * 初始化Token + * * @param getUrl * @param param * @param property * @return * @throws Exception */ - private static String doInitToken(String getUrl, Map param, Map property) throws Exception { + private static String doInitToken(String getUrl, Map param, Map property) throws Exception { // 封装发送的请求参数 StringBuffer buffer = new StringBuffer(); - if(param != null) { + if (param != null) { buffer.append("?"); int x = 0; - for(Entry map : param.entrySet()) { + for (Entry map : param.entrySet()) { buffer.append(map.getKey()).append("=").append(map.getValue().toString()); - if(x != param.size()-1) { + if (x != param.size() - 1) { buffer.append("&"); } x++; @@ -396,7 +425,7 @@ public class SMCHttpUtil { } String urlPath = getUrl + buffer.toString(); URL url = new URL(urlPath); - URLConnection urlConnection = url.openConnection(); + URLConnection urlConnection = url.openConnection(); HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; httpUrlConnection.setConnectTimeout(TIMEOUT); // 设置请求头属性参数 @@ -408,7 +437,7 @@ public class SMCHttpUtil { httpUrlConnection.setRequestProperty("charset", "UTF-8"); httpUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpUrlConnection.setRequestProperty("accept", "application/json"); - if(property != null) { + if (property != null) { for (Entry map : property.entrySet()) { httpUrlConnection.setRequestProperty(map.getKey(), map.getValue()); } @@ -417,7 +446,7 @@ public class SMCHttpUtil { String status = "";// 响应状态6 PrintWriter out = null; BufferedReader in = null; - try{ + try { // httpUrlConnection.connect(); // 定义BufferedReader输入流来读取URL的响应数据 in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(), "UTF-8")); @@ -427,17 +456,21 @@ public class SMCHttpUtil { } // 获得URL的响应状态码 status = new Integer(httpUrlConnection.getResponseCode()).toString(); - }catch(Exception e) { + } catch (Exception e) { e.printStackTrace(); - }finally { + } finally { try { - if (out != null) { out.close();} - if (in != null) {in.close();} + if (out != null) { + out.close(); + } + if (in != null) { + in.close(); + } } catch (Exception ex) { ex.printStackTrace(); } } - if(!status.startsWith("2")) { + if (!status.startsWith("2")) { System.out.println(response); throw new Exception("请求结果状态不是200"); } diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index ed18afd..c299383 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -156,7 +156,7 @@ logging: file: name: /project/logs/usercenter-logs.log level: - root: error + root: debug org.springframework.data.mongodb: debug org.springframework.boot.autoconfigure.security.servlet: debug ink.wgink: debug