zoukankan      html  css  js  c++  java
  • 发送验证码

    controller

     1   /**
     2      * 发送短信验证码
     3      * @param phoneNumber
     4      * @return
     5      * @throws Exception
     6      */
     7     @GetMapping(value = "sendSmsCode")
     8     public Object sendSmsCode(String phoneNumber,HttpServletRequest request) throws Exception{
     9         if(StringUtils.isEmpty(phoneNumber)){
    10             return RestResponse.buildError(ReturnCode.parame_is_null);
    11         }
    12         if(!phoneNumber.matches(ConstantUtil.REGEX_MOBILE)){
    13             return RestResponse.buildError(ReturnCode.pLength_is_error);
    14         }
    15         String source = request.getHeader("source");
    16         //获取验证码
    17         String smsCode = CodeUtil.getSmsCode();
    18         log.info("短信验证码:"+smsCode);
    19         //存储验证码
    20         GuavaCacheUtil.GLOBAL_CACHE_SMS_CODE.put(ConstantUtil.appendStr(ConstantUtil.sms_code_pre,phoneNumber,source),smsCode);
    21         //存获取验证码的次数
    22         String result = commonService.sendSmsCode(phoneNumber,String.format(MyTokenCache.smsTempMap.get(ConstantUtil.sms_context).toString(),smsCode));
    23         if(StringUtils.isEmpty(result)){
    24             return RestResponse.buildError(ReturnCode.sms_code_send_fail);
    25         }else {
    26             Map jsonMap = JsonUtils.string2Obj(result,Map.class);
    27             String status = jsonMap.get("ReturnStatus").toString();
    28             if(status.equals("Success")){
    29                 return RestResponse.buildSuccess();
    30             }else {
    31                 return RestResponse.buildError(ReturnCode.sms_code_send_fail);
    32             }
    33         }
    34     }

    impl

     1 public String sendSmsCode(String phoneNumber, String context) throws Exception {
     2         //调用短信接口
     3         String method = "POST";
     4         Map<String, String> headers = new HashMap<String, String>();
     5         //最后在header中的格式(中间是英文空格)为Authorization:APPCODE xxx
     6         headers.put("Authorization", "APPCODE " + app_code);
     7         Map<String, String> querys = new HashMap<String, String>();
     8         //测试可用默认短信模板,测试模板为专用模板不可修改,如需自定义短信内容或改动任意字符,请联系旺旺或QQ726980650进行申请
     9         querys.put("content", context);
    10         querys.put("mobile", phoneNumber);
    11         Map<String, String> bodys = new HashMap<String, String>();
    12         HttpResponse response = HttpUtil.doPost(sms_code_url, method, headers, querys, bodys);
    13         String result = EntityUtils.toString(response.getEntity());
    14         if (result.equals("")) {
    15             log.error(response.toString());
    16             if (StringUtils.isEmpty(result)) {
    17                 log.error("验证短信发送次数已经用完,请联系管理员购买");
    18             }
    19         } else {
    20             log.info(result);
    21         }
    22         return result;
    23     }
    HttpUtil 
      1 package com.zdkj.umt.util.http;
      2 
      3 
      4 import com.aliyun.oss.common.utils.StringUtils;
      5 import org.apache.http.HttpResponse;
      6 import org.apache.http.NameValuePair;
      7 import org.apache.http.client.HttpClient;
      8 import org.apache.http.client.entity.UrlEncodedFormEntity;
      9 import org.apache.http.client.methods.HttpDelete;
     10 import org.apache.http.client.methods.HttpGet;
     11 import org.apache.http.client.methods.HttpPost;
     12 import org.apache.http.client.methods.HttpPut;
     13 import org.apache.http.conn.ClientConnectionManager;
     14 import org.apache.http.conn.scheme.Scheme;
     15 import org.apache.http.conn.scheme.SchemeRegistry;
     16 import org.apache.http.conn.ssl.SSLSocketFactory;
     17 import org.apache.http.entity.ByteArrayEntity;
     18 import org.apache.http.entity.StringEntity;
     19 import org.apache.http.impl.client.DefaultHttpClient;
     20 import org.apache.http.message.BasicNameValuePair;
     21 
     22 import javax.net.ssl.SSLContext;
     23 import javax.net.ssl.TrustManager;
     24 import javax.net.ssl.X509TrustManager;
     25 import java.io.UnsupportedEncodingException;
     26 import java.net.URLEncoder;
     27 import java.security.KeyManagementException;
     28 import java.security.NoSuchAlgorithmException;
     29 import java.security.cert.X509Certificate;
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 import java.util.Map;
     33 
     34 /**
     35  * @author liuyanjun
     36  */
     37 public class HttpUtil {
     38 
     39     /**
     40      * get
     41      *
     42      * @param host
     43      * @param path
     44      * @param method
     45      * @param headers
     46      * @param querys
     47      * @return
     48      * @throws Exception
     49      */
     50     public static HttpResponse doGet(String host, String method,
     51                                      Map<String, String> headers,
     52                                      Map<String, String> querys)
     53             throws Exception {
     54         HttpClient httpClient = wrapClient(host);
     55 
     56         HttpGet request = new HttpGet(buildUrl(host, querys));
     57         for (Map.Entry<String, String> e : headers.entrySet()) {
     58             request.addHeader(e.getKey(), e.getValue());
     59         }
     60 
     61         return httpClient.execute(request);
     62     }
     63 
     64     /**
     65      * post form
     66      *
     67      * @param host
     68      * @param method
     69      * @param headers
     70      * @param querys
     71      * @param bodys
     72      * @return
     73      * @throws Exception
     74      */
     75     public static HttpResponse doPost(String host, String method,
     76                                       Map<String, String> headers,
     77                                       Map<String, String> querys,
     78                                       Map<String, String> bodys)
     79             throws Exception {
     80         HttpClient httpClient = wrapClient(host);
     81 
     82         HttpPost request = new HttpPost(buildUrl(host, querys));
     83         for (Map.Entry<String, String> e : headers.entrySet()) {
     84             request.addHeader(e.getKey(), e.getValue());
     85         }
     86 
     87         if (bodys != null) {
     88             List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
     89 
     90             for (String key : bodys.keySet()) {
     91                 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
     92             }
     93             UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
     94             formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
     95             request.setEntity(formEntity);
     96         }
     97 
     98         return httpClient.execute(request);
     99     }
    100 
    101     /**
    102      * Post String
    103      *
    104      * @param host
    105      * @param path
    106      * @param method
    107      * @param headers
    108      * @param querys
    109      * @param body
    110      * @return
    111      * @throws Exception
    112      */
    113     public static HttpResponse doPost(String host,  String method,
    114                                       Map<String, String> headers,
    115                                       Map<String, String> querys,
    116                                       String body)
    117             throws Exception {
    118         HttpClient httpClient = wrapClient(host);
    119 
    120         HttpPost request = new HttpPost(buildUrl(host, querys));
    121         for (Map.Entry<String, String> e : headers.entrySet()) {
    122             request.addHeader(e.getKey(), e.getValue());
    123         }
    124 
    125         if (!StringUtils.isNullOrEmpty(body)) {
    126             request.setEntity(new StringEntity(body, "utf-8"));
    127         }
    128 
    129         return httpClient.execute(request);
    130     }
    131 
    132     /**
    133      * Post stream
    134      *
    135      * @param host
    136      * @param path
    137      * @param method
    138      * @param headers
    139      * @param querys
    140      * @param body
    141      * @return
    142      * @throws Exception
    143      */
    144     public static HttpResponse doPost(String host,String method,
    145                                       Map<String, String> headers,
    146                                       Map<String, String> querys,
    147                                       byte[] body)
    148             throws Exception {
    149         HttpClient httpClient = wrapClient(host);
    150 
    151         HttpPost request = new HttpPost(buildUrl(host, querys));
    152         for (Map.Entry<String, String> e : headers.entrySet()) {
    153             request.addHeader(e.getKey(), e.getValue());
    154         }
    155 
    156         if (body != null) {
    157             request.setEntity(new ByteArrayEntity(body));
    158         }
    159 
    160         return httpClient.execute(request);
    161     }
    162 
    163     /**
    164      * Put String
    165      * @param host
    166      * @param path
    167      * @param method
    168      * @param headers
    169      * @param querys
    170      * @param body
    171      * @return
    172      * @throws Exception
    173      */
    174     public static HttpResponse doPut(String host,  String method,
    175                                      Map<String, String> headers,
    176                                      Map<String, String> querys,
    177                                      String body)
    178             throws Exception {
    179         HttpClient httpClient = wrapClient(host);
    180 
    181         HttpPut request = new HttpPut(buildUrl(host, querys));
    182         for (Map.Entry<String, String> e : headers.entrySet()) {
    183             request.addHeader(e.getKey(), e.getValue());
    184         }
    185 
    186         if (!StringUtils.isNullOrEmpty(body)) {
    187             request.setEntity(new StringEntity(body, "utf-8"));
    188         }
    189 
    190         return httpClient.execute(request);
    191     }
    192 
    193     /**
    194      * Put stream
    195      * @param host
    196      * @param path
    197      * @param method
    198      * @param headers
    199      * @param querys
    200      * @param body
    201      * @return
    202      * @throws Exception
    203      */
    204     public static HttpResponse doPut(String host,  String method,
    205                                      Map<String, String> headers,
    206                                      Map<String, String> querys,
    207                                      byte[] body)
    208             throws Exception {
    209         HttpClient httpClient = wrapClient(host);
    210 
    211         HttpPut request = new HttpPut(buildUrl(host, querys));
    212         for (Map.Entry<String, String> e : headers.entrySet()) {
    213             request.addHeader(e.getKey(), e.getValue());
    214         }
    215 
    216         if (body != null) {
    217             request.setEntity(new ByteArrayEntity(body));
    218         }
    219 
    220         return httpClient.execute(request);
    221     }
    222 
    223     /**
    224      * Delete
    225      *
    226      * @param host
    227      * @param path
    228      * @param method
    229      * @param headers
    230      * @param querys
    231      * @return
    232      * @throws Exception
    233      */
    234     public static HttpResponse doDelete(String host, String path, String method,
    235                                         Map<String, String> headers,
    236                                         Map<String, String> querys)
    237             throws Exception {
    238         HttpClient httpClient = wrapClient(host);
    239 
    240         HttpDelete request = new HttpDelete(buildUrl(host, querys));
    241         for (Map.Entry<String, String> e : headers.entrySet()) {
    242             request.addHeader(e.getKey(), e.getValue());
    243         }
    244 
    245         return httpClient.execute(request);
    246     }
    247 
    248     private static String buildUrl(String host, Map<String, String> querys) throws UnsupportedEncodingException {
    249         StringBuilder sbUrl = new StringBuilder();
    250         sbUrl.append(host);
    251         if (null != querys) {
    252             StringBuilder sbQuery = new StringBuilder();
    253             for (Map.Entry<String, String> query : querys.entrySet()) {
    254                 if (0 < sbQuery.length()) {
    255                     sbQuery.append("&");
    256                 }
    257                 if (StringUtils.isNullOrEmpty(query.getKey()) && !StringUtils.isNullOrEmpty(query.getValue())) {
    258                     sbQuery.append(query.getValue());
    259                 }
    260                 if (!StringUtils.isNullOrEmpty(query.getKey())) {
    261                     sbQuery.append(query.getKey());
    262                     if (!StringUtils.isNullOrEmpty(query.getValue())) {
    263                         sbQuery.append("=");
    264                         sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
    265                     }
    266                 }
    267             }
    268             if (0 < sbQuery.length()) {
    269                 sbUrl.append("?").append(sbQuery);
    270             }
    271         }
    272 
    273         return sbUrl.toString();
    274     }
    275 
    276     private static HttpClient wrapClient(String host) {
    277         HttpClient httpClient = new DefaultHttpClient();
    278         if (host.startsWith("https://")) {
    279             sslClient(httpClient);
    280         }
    281 
    282         return httpClient;
    283     }
    284 
    285     private static void sslClient(HttpClient httpClient) {
    286         try {
    287             SSLContext ctx = SSLContext.getInstance("TLS");
    288             X509TrustManager tm = new X509TrustManager() {
    289                 public X509Certificate[] getAcceptedIssuers() {
    290                     return null;
    291                 }
    292                 public void checkClientTrusted(X509Certificate[] xcs, String str) {
    293 
    294                 }
    295                 public void checkServerTrusted(X509Certificate[] xcs, String str) {
    296 
    297                 }
    298             };
    299             ctx.init(null, new TrustManager[] { tm }, null);
    300             SSLSocketFactory ssf = new SSLSocketFactory(ctx);
    301             ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    302             ClientConnectionManager ccm = httpClient.getConnectionManager();
    303             SchemeRegistry registry = ccm.getSchemeRegistry();
    304             registry.register(new Scheme("https", 443, ssf));
    305         } catch (KeyManagementException ex) {
    306             throw new RuntimeException(ex);
    307         } catch (NoSuchAlgorithmException ex) {
    308             throw new RuntimeException(ex);
    309         }
    310     }
    311 }
  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/huanglp/p/12901710.html
Copyright © 2011-2022 走看看