springboot整合阿里短信服务

近期整理了一下阿里的oss短信服务,官网也有sdk说明

https://help.aliyun.com/document_detail/55284.html?spm=a2c4g.11186623.6.556.xinFP6%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E7%9A%84%EF%BC%8C%E8%BF%99%E4%B8%AA%E7%9C%8B%E4%B8%80%E4%BC%9A%E5%B0%B1%E5%9B%9E%E4%BA%86

在官网上注册好短信服务的前提下,大致可以分为四个步骤完成阿里oss服务。前提是你自己的代码里面已经整和了redis服务。在这里我就直接写SmsService,redis服务有问题的可以私聊我。

1.导入阿里短信服务sdk

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    <version>1.0.0</version>
</dependency>
2.yml配置阿里服务的一些信息
sms:
  accessId: 填自己真实的配置
  accessKey: 填自己真实的配置
  signName: 填自己真实的配置
  codeTemplate: 填自己真实的配置
  product: Dysmsapi
  domain: dysmsapi.aliyuncs.com

3.整和SmsService

package com.ijuvenile.utils.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ijuvenile.utils.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.UUID;

@Service
public class SmsService {

    @Value("${sms.accessId}")
    private String accessId;
    @Value("${sms.accessKey}")
    private String accessKey;
    @Value("${sms.signName}")
    private String signName;
    @Value("${sms.codeTemplate}")
    private String codeTemplate;
    @Value("${sms.product}")
    private String product;     //短信API产品名称(短信产品名固定,无需修改)
    @Value("${sms.domain}")
    private String domain;      //dysmsapi.aliyuncs.com

    @Autowired
    private RedisService redisService;(有问题私聊我)

    /**
     * 根据用户输入的phone发送验证码
     * @param phone 电话号码
     */
    public void sendSmsCode(String phone){
        if(!phone.matches("^1[3|4|5|7|8][0-9]{9}$")){
            System.out.println("手机号码格式不正确");
            return;
        }
        //判断用户输入的电话号码是否频繁发送
        if(isSendOfen(phone)){
            System.out.println("发送短信频繁,请稍后再试");
            return;
        }
        Sms sms = makeCode(phone);      //制作验证码,6位随机数字
        JSONObject smsJson=new JSONObject();
        smsJson.put("code",sms.getCode());
        smsJson.put("product","Dysmsapi");
        SendSmsResponse sendSmsResponse=null;
        try {
            sendSmsResponse = send(phone,signName,codeTemplate,smsJson);
        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println("短信验证码发送失败");
            return;
        }
        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            //短信发送成功,将短信记录到redis中
            redisCode(sms);
            System.out.println("短信发送成功");
        }
    }

    //将验证码缓存到redis中,10分钟过后自动清除该缓存
    private void redisCode(Sms sms) {
        redisService.set(sms.getPhone(),10L,sms);
    }

    //随机生成6位数的短信码
    private Sms makeCode(String phone) {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for(int i=0;i<6;i++){
            int next =random.nextInt(10);
            code.append(next);
        }
        return new Sms(phone,code.toString(),System.currentTimeMillis());
    }

    //判断验证功发送时候频繁
    private boolean isSendOfen(String phone) {
        if(redisService.get(phone)==null) {
            return false;
        }else{
            //判断上一次记录的时间和当前时间进行对比,如果两次相隔时间小于120s,视为短信发送频繁
            Sms sms=redisService.get(phone,Sms.class);
            //两次发送短信中间至少有2分钟的间隔时间
            if(sms.getTime()+120*1000>=System.currentTimeMillis()) {
                return true;
            }
            return false;
        }
    }

    /**
     * 验证短信
     * @param phone
     * @param code
     * @return
     */
    public boolean validSmsCode(String phone, String code){
        //取出所有有关该手机号的短信验证码
        if(redisService.get(phone)==null){
            System.out.println("短信验证失败");
            return false;
        }
        Sms sms=redisService.get(phone,Sms.class);
        if (sms.getCode().equals(code)){
            System.out.println("短信验证成功");
            //删除掉该redis
            redisService.delete(phone);
            return true;
        }
        return false;
    }

    /**
     * 发信
     * @param phone
     * @param signName
     * @param templateCode
     * @param params
     * @return
     * @throws ClientException
     */
    SendSmsResponse send(String phone, String signName, String templateCode, JSONObject params) throws ClientException {
        //初始化ascClient,暂时不支持多region(请勿修改)
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessId,
                accessKey);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
        request.setTemplateParam(params.toJSONString());
        request.setOutId(UUID.randomUUID().toString());
        //请求失败这里会抛ClientException异常
        return acsClient.getAcsResponse(request);
    }

}


4.Sms.java

package com.ijuvenile.utils.sms;

public class Sms {

    private String phone;   //电话号码
    private String code;    //短信验证码
    private Long time;      //短信验证码生成时间

    public Sms() {
    }

    public Sms(String phone, String code, Long time) {
        this.phone = phone;
        this.code = code;
        this.time = time;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }
}


发布了7 篇原创文章 · 获赞 11 · 访问量 4281

猜你喜欢

转载自blog.csdn.net/qq_38662806/article/details/79615408