JAVA 实现阿里云的短信验证码

调用发送短信代码

1、此处:String template = “SMS_22*****05”;这里修改为你自己的短信模板

/**
     * 发送手机短信
     */
    @Autowired
    private SendSmsImpl sendSms;
   
    
    @CrossOrigin
    @PostMapping("/aliyun")
    @ApiOperation(value = "发送阿里云短信验证码",response = R.class)
    public R aliyun(@Validated RequestAliyunNoteEntity requestAliyunNoteEntity) {
        //获取手机号,发送短信验证码
        String account = requestAliyunNoteEntity.getAccount();
        //code模板
        String template = "SMS_22*****05";
        //随机获取验证码-6位
        HashMap<String, Object> mp = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<6;i++){
            sb.append(random.nextInt(10));
        }
        mp.put("code",sb);
        //如果发送代码成功!那么就存储(替换)验证码到数据库
        boolean send = sendSms.send(account, template, mp);
        if(send){
            Boolean aBoolean = clientCodeService.saveNote(account, sb.toString());
            if(aBoolean){
                return R.ok();
            }else{
                return R.error("出现了点意外!");
            }
        }else{
            return R.error("刚刚发生了什么事儿?");
        }
    }

二、发送短信的主要代码

/**
 * @author LunarYouI
 * @create 2021-06-15 10:56
 *
 * 传入手机号、模板code、验证码
 */
public interface SendSms {
    boolean send(String phoneNum, String templateCode, Map<String,Object> code);
}

1、此处:DefaultProfile profile = DefaultProfile.getProfile(“cn-hangzhou”, “参数2”, “参数3”); 需要将2、3参数修改为自己的accessKeyIdsecret
2、此处:request.putQueryParameter(“SignName”, “参数2”);参数2需要修为自己的签名名称,否则不可用

/**
 * @author LunarYouI
 * @create 2021-06-15 10:59
 */
@Service
public class SendSmsImpl implements SendSms{
    @Override
    public boolean send(String phoneNum, String CODE, Map<String, Object> code) {
        //链接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "####5tNzdq3Gw8Wh########", "####qRNeKwUrEQjsAwIbIb########");
        //构建成客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //自定义的参数(手机号、验证码、签名、模板)
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);//手机号码
        request.putQueryParameter("SignName", "自行修改");//签名名称
        request.putQueryParameter("TemplateCode", CODE);//模板的code
        //构建一个短信验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //成功就返回true【response.getHttpResponse().isSuccess()里面默认就带了布尔值】
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}

最终效果图:
在这里插入图片描述


版权声明:本文为qq_46100517原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。