SpringBoot集成发送短信验证码

1,在pom文件里引入阿里云依赖

<!--短信sdk-->
  <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>3.7.1</version>
  </dependency>

  <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
      <version>1.1.0</version>
  </dependency>

2,生成验证码

private static final String OFTEN = "OFTEN:";

@Resource
private AliSmsConfig smsConfig;
//code为短信模板
 code = smsConfig.getLogin_verify();
 //生成验证码
   String number = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
   try {
       SendSmsResponse sendSmsResponse = AliSmsUtils.sendSms(phone, number, code);
       String message = sendSmsResponse.getMessage();
       if (!("OK".equals(message))) {
           return ResponseUtil.fail("验证码发送失败请稍后再试");
       }
   } catch (ClientException e) {
       return ResponseUtil.fail("验证码发送失败请稍后再试");
   }

3,配置文件AliSmsConfig,配置短信模板,信息在application.yml里存入模板信息

package com.linzhuo.app.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author jss
 * @version 1.0
 * @date 2023/11/11 13:45
 */
@Configuration
public class AliSmsConfig {

    //短信模板
    @Value("${sms.aliCode.message}")
    private String message;//信息变更
    @Value("${sms.aliCode.update_password}")
    private String update_password;//修改密码
    @Value("${sms.aliCode.login_abnormal}")
    private String login_abnormal;//登录异常
    @Value("${sms.aliCode.login_verify}")
    private String login_verify;//登录确认
    @Value("${sms.aliCode.register}")
    private String register;//用户注册
    @Value("${sms.aliCode.phone_binding}")
    private String phone_binding;//手机绑定
    @Value("${sms.aliCode.password_fin}")
    private String password_fin;//密码找回
    @Value("${sms.aliCode.identity_verification}")
    private String identity_verification;//身份验证

    public String getMessage() {
        return message;
    }

    public String getUpdate_password() {
        return update_password;
    }

    public String getLogin_abnormal() {
        return login_abnormal;
    }

    public String getLogin_verify() {
        return login_verify;
    }

    public String getRegister() {
        return register;
    }

    public String getPhone_binding() {
        return phone_binding;
    }

    public String getPassword_fin() {
        return password_fin;
    }

    public String getIdentity_verification() {
        return identity_verification;
    }
}

4,发信息工具类AliSmsUtils,在Util包里

package com.linzhuo.app.util.ssm;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jss
 * @version 1.0
 * @date 2023/7/14 13:39
 */
public class AliSmsUtils {

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    private final static String accessKey = "自己的key";

    private final static String accessKeySecret = "自己的secret";


    public static SendSmsResponse sendSms(String phone, String code, String templateCode) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("北京**");
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{\"code\":" + code + "}");

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }


    public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        //必填-待发送的手机号码
        request.setPhoneNumber(bizId);
        //可选-流水号
        request.setBizId(bizId);
        //必填-发送日期 支持30天内记录查询,格式yyyyMMdd
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        //必填-页大小
        request.setPageSize(10L);
        //必填-当前页码从1开始计数
        request.setCurrentPage(1L);

        //hint 此处可能会抛出异常,注意catch
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);

        return querySendDetailsResponse;
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值