From c9cd6d5c52ab9c036db36c7db7b9e121f25f9038 Mon Sep 17 00:00:00 2001 From: Sum1Dream <418471657@qq.com> Date: Mon, 25 Oct 2021 09:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/cweb/config/AuthConfig.java | 1 + .../cweb/controller/HighTestController.java | 2 +- .../cweb/controller/SmsContentController.java | 159 +++++++++++++++++- .../com/cweb/controller/testController.java | 14 +- .../src/main/resources/pre/application.yml | 2 +- 5 files changed, 162 insertions(+), 16 deletions(-) diff --git a/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java b/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java index e7040d78..9cf5ce84 100644 --- a/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java +++ b/hai-cweb/src/main/java/com/cweb/config/AuthConfig.java @@ -124,6 +124,7 @@ public class AuthConfig implements WebMvcConfigurer { .excludePathPatterns("/cmsContent/*") .excludePathPatterns("/sendSms/*") .excludePathPatterns("/test/*") + .excludePathPatterns("/sms/*") ; } diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java index c4179a84..c4bfb56c 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighTestController.java @@ -187,7 +187,7 @@ public class HighTestController { @ApiOperation(value = "推送团油订单") public ResponseData pushTuanYouOrder() { try { - HighOrder order = highOrderService.getOrderByOrderNo("HF2021100910141059900"); + HighOrder order = highOrderService.getOrderByOrderNo("HF2021102112141171406"); for (HighChildOrder highChildOrder : order.getHighChildOrderList()) { HighMerchantStoreModel store = highMerchantStoreService.getMerchantStoreById(highChildOrder.getGoodsId()); // 推送团油订单 diff --git a/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java b/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java index 2f304084..9f3fec68 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java @@ -1,6 +1,7 @@ package com.cweb.controller; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.databind.ObjectMapper; import com.hai.common.Base64Util; import com.hai.common.security.VerifyCode; import com.hai.common.security.VerifyCodeStorage; @@ -8,6 +9,7 @@ import com.hai.common.utils.DateUtil; import com.hai.common.utils.HttpsUtils; import com.hai.common.utils.RedisUtil; import com.hai.common.utils.ResponseMsgUtil; +import com.hai.model.MtSmsMessageModel; import com.hai.model.ResponseData; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -20,9 +22,13 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; -import java.io.UnsupportedEncodingException; +import javax.net.ssl.*; +import java.io.*; +import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.*; @Controller @@ -34,6 +40,17 @@ public class SmsContentController { private static Logger log = LoggerFactory.getLogger(CmsContentController.class); + /** + * 设置不验证主机 + */ + private static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + + @Resource private RedisUtil redisUtil; @@ -72,6 +89,146 @@ public class SmsContentController { } } + @RequestMapping(value = "/sendSmsCodeHw", method = RequestMethod.GET) + @ApiOperation(value = "发送短信验证码") + @ResponseBody + public ResponseData sendSmsCodeHw(@RequestParam(value = "phone", required = true) String phone) { + try { + VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); + String smsCode; + if (verifyCode != null){ + smsCode = verifyCode.getObject(); + }else{ + // 生成随机6位验证码 + smsCode = String.valueOf(new Random().nextInt(899999) + 100000); + } + // ip:port根据实际情况填写 + String url = "https://139.9.32.119:18312/common/sms/sendTemplateMessage"; + String msisdn = phone; + String smsTemplateId = "SMS_21092300061"; + Map templateParas = new HashMap(); + templateParas.put("code", smsCode); + String account = "760887"; //实际账号 + String password = "Z.o'&mO%7_?5M,Br"; //实际密码 + + // If the request body does not contain the signature name, set signature to null. + Map body = buildRequestBody(msisdn, smsTemplateId, templateParas, account, password); + if (null == body || body.isEmpty()) { + System.out.println("body is null."); + return ResponseMsgUtil.success(""); + } + + + HttpsURLConnection connection = null; + InputStream is = null; + BufferedReader br = null; + trustAllHttpsCertificates(); + try { + URL realUrl = new URL(url); + connection = (HttpsURLConnection) realUrl.openConnection(); + connection.setHostnameVerifier(DO_NOT_VERIFY); + connection.setDoInput(true); // 设置可输入 + connection.setDoOutput(true); // 设置该连接是可以输出的 + connection.setRequestMethod("POST"); // 设置请求方式 + connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); + // connection.connect(); + ObjectMapper objectMapper = new ObjectMapper(); + PrintWriter pw = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8")); + pw.write(objectMapper.writeValueAsString(body)); + pw.flush(); + pw.close(); + + + br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); + int status = connection.getResponseCode(); + if (200 == status) { // 200 + is = connection.getInputStream(); + } else { // 400/401 + is = connection.getErrorStream(); + } + br = new BufferedReader(new InputStreamReader(is, "UTF-8")); + String line = null; + StringBuilder result = new StringBuilder(); + while ((line = br.readLine()) != null) { // 读取数据 + result.append(line + "\n"); + } + connection.disconnect(); + System.out.println(result.toString()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + + + if (null != is) { + is.close(); + } + if (null != br) { + br.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return ResponseMsgUtil.success("发生成功"); + } catch (Exception e) { + log.error("CmsContentController --> sendSmsCode() error!", e); + return ResponseMsgUtil.exception(e); + } + } + + + // msisdn, smsTemplateId, paramValues, countryID + public static Map buildRequestBody(String msisdn, String smsTemplateId, + Map paramValues, String accout, String passward) { + if (null == msisdn || null == smsTemplateId || null == accout || null == passward) { + System.out.println( + "buildRequestBody(): mobiles, templateId or templateParas or account or password is null."); + return null; + } + + + Map map = new HashMap(); + List requestLists = new ArrayList(); + MtSmsMessageModel mtSmsMessage = new MtSmsMessageModel(); + List mobiles = new ArrayList(); + mobiles.add(msisdn); + mtSmsMessage.setMobiles(mobiles); + mtSmsMessage.setTemplateId(smsTemplateId); + mtSmsMessage.setTemplateParas(paramValues); + mtSmsMessage.setSignature("【嗨森逛】"); + requestLists.add(mtSmsMessage); + map.put("account", accout); + map.put("password", passward); + map.put("requestLists", requestLists); + return map; + } + + + static void trustAllHttpsCertificates() throws Exception { + TrustManager[] trustAllCerts = new TrustManager[] { + new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return; + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + return; + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + } + }; + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, null); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + } + @RequestMapping(value = "/getSmsCode", method = RequestMethod.GET) @ApiOperation(value = "发送短信验证码") @ResponseBody diff --git a/hai-cweb/src/main/java/com/cweb/controller/testController.java b/hai-cweb/src/main/java/com/cweb/controller/testController.java index 9387d1f9..e0d3743a 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/testController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/testController.java @@ -34,26 +34,16 @@ import javax.net.ssl.X509TrustManager; @Controller @Api(value = "华为云短信发送") -@RequestMapping(value = "/test") +@RequestMapping(value = "/test1") public class testController { /** * 设置不验证主机 */ - - private static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { - - @Override public boolean verify(String hostname, SSLSession session) { - - return true; - - } - - }; @RequestMapping(value = "/sendSmsCode", method = RequestMethod.GET) @@ -168,13 +158,11 @@ public class testController { return; } - @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { return; } - @Override public X509Certificate[] getAcceptedIssuers() { return null; diff --git a/hai-cweb/src/main/resources/pre/application.yml b/hai-cweb/src/main/resources/pre/application.yml index 1012ea5c..f425a243 100644 --- a/hai-cweb/src/main/resources/pre/application.yml +++ b/hai-cweb/src/main/resources/pre/application.yml @@ -9,7 +9,7 @@ debug: false #datasource数据源设置 spring: datasource: - url: jdbc:mysql://139.159.177.244:3306/hsg_pre?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false + url: jdbc:mysql://122.9.135.148:3306/hsg?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: HF123456. type: com.alibaba.druid.pool.DruidDataSource