栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

短信验证码实现(京东万象第三方接口)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

短信验证码实现(京东万象第三方接口)

在网页上我们经常见到有短信验证的功能,他是如何实现的呢?

需要的技术:
  1. springboot后端
  2. HttpClientUtils工具类
    该类是帮助连接其它网站的一个工具类,现在已经出到了第三个版本(后面会放源码)
  3. json解析等依赖,xml解析依赖
前期准备
  1. 京东万象网址:https://wx.jdcloud.com/market/datas/5/10306
    这里我选择使用106短信接口,每个人有8次免费使用的机会
  2. 首先查看一下这个短信的参数要求

    已经十分明确,主要需要你的appkey(每人都不一样,京东万象提供的)
    手机号:前端传递
    短信内容:后端生成随机数并字符串拼接
  3. 再看返回参数:

可以看到主要有两部分成,一部分是京东万象的回馈code,另一部分是106接口的返回值,106接口的返回值是xml类型,京东万象的返回值是json类型
返回值实例:

{
    "code": "10000",
    "charge": false,
    "remain": 0,
    "msg": "查询成功",
    "result": "n Successn okn -1111611n 101609164n 1"
}

就是这个样子,前一部分为json,后一部分为xml
好,现在已经弄明白了都有啥,那就开始

项目开始 pom.xml导入依赖

        
            org.apache.httpcomponents
            httpclient
            4.5.3
        

        
            com.alibaba
            fastjson
            1.2.36
        
        
        
            org.apache.commons
            commons-lang3
        

导入HttpClientUtils.java

package com.springboot.utils;

import com.alibaba.fastjson .JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.*;


public class HttpClientUtils {

    
    private static final String ENCODING = "UTF-8";

    
    private static final Integer CONNECT_TIMEOUT = 6000;

    
    private static final Integer SOCKET_TIMEOUT = 6000;


    
    public static String doGet(String url) throws Exception {
        return doGet(url, null, null);
    }

    
    public static String doGet(String url, Map params) throws Exception {
        return doGet(url, null, params);
    }

    
    public static String doGet(String url, Map headers, Map params) throws Exception {
        // 创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建访问的地址
        URIBuilder uriBuilder = new URIBuilder(url);
        if (params != null) {
            Set> entrySet = params.entrySet();
            for (Map.Entry entry : entrySet) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue());
            }
        }

        // 创建http对象
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
        httpGet.setConfig(requestConfig);

        // 设置请求头
        packageHeader(headers, httpGet);

        // 创建httpResponse对象
        CloseableHttpResponse httpResponse = null;

        //响应结果
        String result = "";
        try {

            // 执行请求
            httpResponse = httpClient.execute(httpGet);

            // 获取返回结果
            if (httpResponse != null && httpResponse.getStatusLine() != null) {
                if (httpResponse.getEntity() != null) {
                    result = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
                }
            }
        } finally {
            // 释放资源
            release(httpResponse, httpClient);
        }

        return result;
    }

    
    public static String doPost(String url) throws Exception {
        return doPost(url, null, null);
    }

    
    public static String doPost(String url, Map params) throws Exception {
        return doPost(url, null, params);
    }

    
    public static String doPost(String url, Map headers, Map params) throws Exception {
        // 创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建http对象
        HttpPost httpPost = new HttpPost(url);
        
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
        httpPost.setConfig(requestConfig);
        // 设置请求头
        
        packageHeader(headers, httpPost);

        // 封装请求参数
        packageParam(params, httpPost);

        // 创建httpResponse对象
        CloseableHttpResponse httpResponse = null;

        String result = "";
        try {

            // 执行请求
            httpResponse = httpClient.execute(httpPost);

            // 获取返回结果
            if (httpResponse != null && httpResponse.getStatusLine() != null) {
                if (httpResponse.getEntity() != null) {
                    result = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
                }
            }

        } finally {
            // 释放资源
            release(httpResponse, httpClient);
        }
        return result;
    }


    
    public static String sendPost(String url, JSONObject data) throws IOException {
        // 设置默认请求头
        Map headers = new HashMap<>();
        headers.put("content-type", "application/json");

        return doPostByJSON(url, headers, data, ENCODING);
    }

    
    public static String sendPost(String url, Map params) throws IOException {
        // 设置默认请求头
        Map headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        // 将map转成json
        JSONObject data = JSONObject.parseObject(JSON.toJSONString(params));
        return doPostByJSON(url, headers, data, ENCODING);
    }

    
    public static String sendPost(String url, Map headers, JSONObject data) throws IOException {
        return doPostByJSON(url, headers, data, ENCODING);
    }


    
    public static String sendPost(String url, Map headers, Map params) throws IOException {
        // 将map转成json
        JSONObject data = JSONObject.parseObject(JSON.toJSONString(params));
        return doPostByJSON(url, headers, data, ENCODING);
    }

    
    private static String doPostByJSON(String url, Map headers, JSONObject data, String encoding) throws IOException {
        // 请求返回结果
        String resultJson = null;
        // 创建Client
        CloseableHttpClient client = HttpClients.createDefault();
        // 发送请求,返回响应对象
        CloseableHttpResponse response = null;
        // 创建HttpPost对象
        HttpPost httpPost = new HttpPost();

        
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
        httpPost.setConfig(requestConfig);

        try {
            // 设置请求地址
            httpPost.setURI(new URI(url));
            // 设置请求头
            packageHeader(headers, httpPost);

            // 设置实体
            httpPost.setEntity(new StringEntity(JSON.toJSONString(data)));
            // 发送请求,返回响应对象
            response = client.execute(httpPost);
            // 获取响应状态
            int status = response.getStatusLine().getStatusCode();

            if (status != HttpStatus.SC_OK) {
                System.out.println("响应失败,状态码:" + status);
            }
            // 获取响应结果
            resultJson = EntityUtils.toString(response.getEntity(), encoding);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release(response, client);
        }
        return resultJson;
    }

    
    public static String doPostByXml(String url, String requestDataXml) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";

        try {
            //创建httpClient实例
            httpClient = HttpClients.createDefault();
            //创建httpPost远程连接实例
            HttpPost httpPost = new HttpPost(url);
            //配置请求参数实例
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(35000)//设置连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)//设置连接请求超时时间
                    .setSocketTimeout(60000)//设置读取数据连接超时时间
                    .build();
            //为httpPost实例设置配置
            httpPost.setConfig(requestConfig);
            //设置请求参数
            httpPost.setEntity(new StringEntity(requestDataXml,"UTF-8"));
            //设置请求头内容
            httpPost.addHeader("Content-Type","text/xml");

            //执行post请求得到返回对象
            response = httpClient.execute(httpPost);
            //通过返回对象获取数据
            HttpEntity entity = response.getEntity();
            //将返回的数据转换为字符串
            result = EntityUtils.toString(entity);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    
    public static void packageHeader(Map params, HttpRequestBase httpMethod) {
        // 封装请求头
        if (params != null) {
            Set> entrySet = params.entrySet();
            for (Map.Entry entry : entrySet) {
                // 设置到请求头到HttpRequestBase对象中
                httpMethod.setHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    
    public static void packageParam(Map params, HttpEntityEnclosingRequestBase httpMethod)
            throws UnsupportedEncodingException {
        // 封装请求参数
        if (null != params && params.size() > 0) {
            List nvps = new ArrayList();
            Set> entrySet = params.entrySet();
            for (Map.Entry entry : entrySet) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }

            // 设置到请求的http对象中
            httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
        }
    }




    
    private static String sendGet(String url, Map params, String encoding) throws IOException {
        // 请求结果
        String resultJson = null;
        // 创建client
        CloseableHttpClient client = HttpClients.createDefault();
        //响应对象
        CloseableHttpResponse response = null;
        // 创建HttpGet
        HttpGet httpGet = new HttpGet();
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            // 封装参数
            if (params != null) {
                for (String key : params.keySet()) {
                    builder.addParameter(key, params.get(key).toString());
                }
            }
            URI uri = builder.build();
            // 设置请求地址
            httpGet.setURI(uri);

            //设置配置请求参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(35000)//连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)//请求超时时间
                    .setSocketTimeout(60000)//数据读取超时时间
                    .build();

            // 发送请求,返回响应对象
            response = client.execute(httpGet);
            // 获取响应状态
            int status = response.getStatusLine().getStatusCode();

            if (status != HttpStatus.SC_OK) {
                System.out.println("响应失败,状态码:" + status);
            }

            // 获取响应数据
            resultJson = EntityUtils.toString(response.getEntity(), encoding);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release(response, client);
        }
        return resultJson;
    }


    
    public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
        // 释放资源
        if (httpResponse != null) {
            httpResponse.close();
        }
        if (httpClient != null) {
            httpClient.close();
        }
    }


}

然后创建后端的Controller,前端将手机号传递过来

@Reference(interfaceClass = RedisService.class,version = "1.0.0",check = false,timeout = 15000)
    private RedisService redisService;
    //这里部署了dubbo以及redis,将验证码存入redis方便验证

    @RequestMapping("/user/messageCode")
    @ResponseBody
    public Result sendMessageCode(@RequestParam("phone") String phone) throws Exception {
        //通过第三方接口发送短信验证码
        String randomNum = getRandNum(6);
        System.out.println("本次验证码为:"+randomNum);
        String url ="https://way.jd.com/kaixintong/kaixintong";
        Map params = new HashMap<>();
        params.put("appkey","你的appkey");//你的appkey
        params.put("mobile",phone);
        params.put("content","【凯信通】您的验证码是:"+randomNum);
//        String jsonStr = HttpClientUtils.doGet(url,params);
        String jsonStr = "{n" +
                "    "code": "10000",n" +
                "    "charge": false,n" +
                "    "remain": 0,n" +
                "    "msg": "查询成功",n" +
                "    "result": "\n Success\n ok\n -1111611\n 101609164\n 1"n" +
                "}";  //没有机会的可以使用伪数据尝试
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String code = jsonObject.getString("code");
        if(!StringUtils.equals(code,"10000")){
            return Result.error("验证码调用错误");
        }
        String resultXml = jsonObject.getString("result");
        Document document = DocumentHelper.parseText(resultXml);
        Node node = document.selectSingleNode("/returnsms/returnstatus");
        String returnstatus = node.getText();
        if (!StringUtils.equals(returnstatus,"Success")){
            return Result.error("验证码发送失败");
        }
        //验证码发送成功了,保存验证码(将验证码保存到redis)
        redisService.put(phone,randomNum);
        return Result.success(randomNum);
    }

这样就结束了
成果如图:

都看到这里了不给我个赞吗?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/827710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号