zoukankan      html  css  js  c++  java
  • 短信验证码--+验证码存在redis中--直接可用的后台代码

    1.小程序存值在redis里面,不建议用cook存sessionid,因为同一个会话每次请求接口值会变,存不住,

     1.1工具包(短信工具包---记住:发给谁(手机号),发什么(发送内容),发送路径(url))---工具包直接粘了用

    public static void sendSms(final String url, final String smsContent) throws Exception {
    if (null == url || 0 == url.length())
    throw new IllegalArgumentException("短信接口連接地址不能為空");
    if (null == smsContent || 0 == smsContent.length())
    throw new IllegalArgumentException("短信發送內容不能為空");
    try {
    URL connection = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) connection.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty(
    "User-Agent",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ");
    conn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
    conn.getOutputStream()));
    bw.write(smsContent);
    bw.close();
    BufferedReader br = new BufferedReader(new InputStreamReader(
    conn.getInputStream()));
    String out = null;
    while ((out = br.readLine()) != null) {
    System.out.println(out);
    }
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    throw e;
    }
    }

    1.2 ctroller接口代码

    /*
    * 短信验证码(给短信平台手机号和自主生成的随机数,即验证码)
    * */
    @RequestMapping(value = "/identifyingCode")
    public CIPResponseMsg identifyingCode(HttpServletRequest request,HttpServletResponse response)throws Exception {
    CIPResponseMsg msg = new CIPResponseMsg();

    String per_phone = request.getParameter("per_phone");   //用get请求
    boolean isJedisConnectExeptionOccured = false;      //redis申明
    try {
    String rd_code = WCM_utils_SequenceUtils.rd_code();
    String dx_id = WCM_utils_SequenceUtils.dx_id();
    //String url = "http://10.**.***.***:****/smsinterface/sendInterface/sendSms_xml.do";   //本地短信接口的ip
    String url = "http://10.**.***.***:****/smsinterface/sendInterface/sendSms_xml.do";    //uat测试短信接口ip  --正式上线的时候
    String code = "<req op='sms_01send'>" +
    "<h>" +
    "<ver>1.0</ver>" +
    "<user>dkh_app</user>" +
    "<pass>lV4g6pzrrOvZ3MvOqPbF</pass>" +
    "</h>" +
    "<items>" +
    "<item>" +
    "<id>"+dx_id+"</id>" +
    "<content>"+随机数+"</content>" +
    "<tele>"+发送给谁的手机号+"</tele>" +
    "</item>" +
    "</items>" +
    "</req>";
    SmsUtil.sendSms(url, code);
    System.out.println(code);
    //短信平台获取短信验证码
    String dx_cd = rd_code;
    if(dx_cd==null&dx_cd.equals("")){
    msg.data=dx_cd;
    msg.errorCode = CIPErrorCode.CALL_SUCCESS.code;
    msg.msg = "验证码获取失败";
    }
    msg.data=dx_cd;
    jedisSet = CIPRedisUtils.getJedisResource();    //new jedisSet,用这个存,(死的)
    jedisSet.setex("dx_cd", 900,dx_cd);    //存---用(key,900存储时间,value)
    System.out.println(jedisSet.get("dx_cd"));   //控制台打印一下
    log.info("dx_cd="+dx_cd);  //linux打印日志
    msg.data = jedisSet.get("dx_cd");
    msg.errorCode = CIPErrorCode.CALL_SUCCESS.code;
    msg.msg = "操作成功";
    /*} catch (Exception e) {
    e.printStackTrace();
    }*/
    } catch (Exception e) {
    isJedisConnectExeptionOccured = true;    
    }finally {
    if(null != jedisSet) {
    CIPRedisUtils.returnJedisResource(jedisSet, isJedisConnectExeptionOccured);                 //重点:重点:重点:用完redis一定要释放资源
    }
    }
    return msg;
    }

    //在另一个类里面去存在redis

  • 相关阅读:
    网络编程--requests模块
    python连接oracle--cx_Oracle模块
    操作excel
    操作mysql数据库
    枚举--enumerate
    邮箱
    匿名函数
    使用spring security jwt 、spring security oauth2权限控制遇到的坑记录
    基于element-ui定义自己的Menu 菜单组件并以component方式去定渲染
    VUE同域情况下父窗体打开子窗口,关闭子窗口时刷新父窗体
  • 原文地址:https://www.cnblogs.com/Darkqueen/p/9848471.html
Copyright © 2011-2022 走看看