springboot接入榛子云短信验证码(超详细)

上干货,简单粗暴三步骤,搞定。

1.在项目中引入榛子云的jar包。

查看最新版本的榛子云的jar包地址

<dependency>
	  <groupId>com.zhenzikj</groupId>
	  <artifactId>zhenzisms</artifactId>
	  <version>2.0.2</version>
</dependency>

2.编写发送验证码和验证验证码的工具类,博主这里已经把完整代码贴入,直接复制粘贴即可使用.(需要引入fastjson和lombok)。

package com.salong.myself.sms;

import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Salong
 * @date 2021/3/10 18:37
 * @Email:salong0503@aliyun.com
 */
@Configuration
public class zhenziSMS {

    @Value("${sms.apiUrl}")
    //访问路径(个人开发者使用https://sms_developer.zhenzikj.com,企业开发者使用https://sms.zhenzikj.com)
    private String apiUrl;

    /*
     *注册账号,进入榛子云短信平台用户中心,打开应用管理,我的应用,
     *即可看到默认有个应用名称为云短信体验的应用,输入对应的appId和appSecret
     *需要先充值至少20块钱,一条验证码短信预计3分钱左右
     */
    @Value("${sms.appId}")
    private String appId;

    @Value("${sms.appSecret}")
    private String appSecret;

    //设置过期时间
    @Value("${sms.timeOut}")
    private Integer timeOut;

    //设置验证码长度
    @Value("${sms.codeLength}")
    private Integer codeLength;

    private ZhenziSmsClient client;


    @Bean
    public ZhenziSmsClient initSMS() {
        if (client == null) {
            client = new ZhenziSmsClient(apiUrl, appId, appSecret);
            return client;
        }
        return client;
    }

    //查询榛子云剩余当前账户可发验证码短信条数
    public Integer checkBalance() throws Exception {
        String balance = client.balance();   //{"code":0,"data":537}
        isSendSuccess isSendSuccess = JSONObject.parseObject(balance, isSendSuccess.class);
        return (int)isSendSuccess.getData();
    }

    //生成验证码和验证码id
    private Map<String, Object> createVerificationCode() {
        HashMap<String, Object> map = new HashMap<>();
        //这个是榛子云短信平台用户中心下的短信管理的短信模板的模板id
        //todo 这个模板id需要自己去平台查看自己的模板id并修改
        map.put("templateId", "3966");
        //生成验证码
        int pow = (int) Math.pow(10, codeLength - 1);
        String verificationCode = String.valueOf((int) (Math.random() * 9 * pow + pow));
        //随机生成messageId,验证验证码的时候,需要携带这个参数去取验证码
        String messageId = UUID.randomUUID().toString();
        map.put("messageId", messageId);
        String[] templateParams = new String[2];
        //两个参数分别为验证码和过期时间
        templateParams[0] = verificationCode;
        templateParams[1] = String.valueOf(timeOut);
        map.put("templateParams", templateParams);
        return map;
    }

    //发送验证码(如果params的success为true,则发送成功,则需要把params中的messageId和验证码存起来,验证验证码的时候用到)
    public Map<String, Object> sendMessage(String phoneNumber,String clientIp) throws Exception {
        Map<String, Object> params = createVerificationCode();
        //发送手机目标(number字段不可修改)
        params.put("number", phoneNumber);
        //防止一个客户端多次刷验证码,防刷专用,这个clientIp只是个防刷标记,
        // 不一定是客户端ip,也可以是客户端登录的账号,或者能鉴权的属性
        if (StringUtils.isNotBlank(clientIp)){
            params.put("clientIp",clientIp);
        }
        String result = client.send(params);
        isSendSuccess success = JSONObject.parseObject(result, isSendSuccess.class);
        if (success.getCode() == 0) {
            params.put("success",true);
        }else {
            params.put("success",false);
        }
        return params;
    }

    //验证短信验证码,传入缓存中存入的messageId和验证码
    public boolean checkMessage(String messageId, String cacheCode) throws Exception {
        String result = client.findSmsByMessageId(messageId);
        Verification verification = JSONObject.parseObject(result, Verification.class);
        if (verification.getCode() == 0) {
            //取到了数据,开始验证码是否正确
            String veificationCode = verification.getData().getMessage();
            int index = veificationCode.indexOf("验证码:");
            String code = veificationCode.substring(index + 4, index + 4 + codeLength);
            //验证验证码
            if (!code.equals(cacheCode)) {
                return false;
            }
            //验证时间是否过期
            Date createTime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").parse(verification.getData().getCreateTime());
            long spent = new Date().getTime() - createTime.getTime();
            if (spent / (1000 * 60) > timeOut) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    //向榛子云发送验证码请求时候,榛子云返回的报文,code=0为成功
    //实例:{"code":0,"data":"发送成功"}
    @Data
    static class isSendSuccess implements Serializable {
        private Integer code;
        private Object data;
    }

    //验证验证码的时候,榛子云返回的报文
    //实例:{"code":0,"data":{"message":"验证码:33356,2分钟内有效,请勿泄漏给他人使用。","appName":"云短信体验","createTime":"2021-03-10 19:24:19","status":"success","toNumber":"15871770252","messageId":"123"}}
    @Data
    static class Verification implements Serializable {
        private Integer code;
        private Message data;
    }

    @Data
    static class Message implements Serializable {
        private String message;
        private String appName;
        private String createTime;
        private String status;
        private String toNumber;
        private String messageId;
    }
}

3.在springboot的yml启动文件中加入以下代码。

sms:
  apiUrl: https://sms_developer.zhenzikj.com
  appId: XXXXX
  appSecret: XXXXXXXXXXXXXXXXXXX
  timeOut: 3
  codeLength: 6

解释一下各个参数,

其中,apiUrl是 访问路径(个人开发者使用https://sms_developer.zhenzikj.com,企业开发者使用https://sms.zhenzikj.com)

timeOut是验证码几分钟过期,codeLength是验证码的长度。

appId和appSecret获取如下:

4.调用,直接通过Autowired自动装配即可使用。

5.最后平台做设置,为了避免人为恶意刷验证码,可在榛子云的平台我的应用后面的详情进行编辑,如下图。

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

却诚Salong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值