需要依赖的pom:
<dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.1</version></dependency>
java代码:
packagecom.ehl.developerplatform.service.impl; importcom.alibaba.fastjson.JSONObject; importcom.aliyun.dysmsapi20170525.models.SendSmsRequest; importcom.aliyun.dysmsapi20170525.models.SendSmsResponse; importcom.aliyun.teaopenapi.models.Config; importcom.ehl.developerplatform.config.SmsProperties; importcom.ehl.developerplatform.enums.ResponseEnum; importcom.ehl.developerplatform.exception.DeveloperPlatFormException; importcom.ehl.developerplatform.pojo.DataResponse; importcom.ehl.developerplatform.util.RedisPrefix; importcom.ehl.developerplatform.util.RedisUtil; importlombok.extern.slf4j.Slf4j; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.context.properties.EnableConfigurationProperties; importorg.springframework.stereotype.Service; importjava.util.HashMap; importjava.util.Map; /*** #------------------------------------------------------------------- #* # CONFIDENTIAL --- CUSTOM STUDIOS #* #------------------------------------------------------------------- #* # #* # @Project Name : develop #* # #* # @File Name : SmsService.java #* # #* # @Programmer : 王震 #* # #* # @Start Date : 2021/4/28 18:08 #* # #* # @Last Update : 2021/4/28 18:08 #* # #* #------------------------------------------------------------------- #* # Classes: #* # #* #------------------------------------------------------------------- #*/"all") (value=SmsProperties.class) (publicfinalclassSmsService { privateSmsPropertiessmsProperties; privateRedisUtilredisUtil; privateSmsService() { } /*** 功能描述: <br>* 〈根据用户输入的phone发送验证码〉** @Param: [phone] 电话号码* @Return: com.ehl.developerplatform.pojo.DataResponse* @Author: 王震* @Date: 2021/4/28 19:01*/publicDataResponse<?>sendSmsCode(finalStringphone) { Stringcode=this.randomCode(); Stringmessage=this.sendMessage(phone, code).getBody().getMessage(); if (!"OK".equals(message)) { log.error("手机号码:{},发送短信失败,失败原因:{},code:{}", phone, message,code); returnDataResponse.fail(ResponseEnum.SEND_MESSAGE_ERROR); } log.info("手机号码:{},发送短信成功,验证码为:{}", phone, code); redisUtil.set(RedisPrefix.buildSendMessageKey(phone), code, 300); returnDataResponse.success("短信发送成功!"); } publicSendSmsResponsesendMessage(finalStringphone, finalStringcode) { try { com.aliyun.dysmsapi20170525.Clientclient=createClient(smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret()); Map<String, Object>map=newHashMap<>(); map.put("code", code); SendSmsRequestsendSmsRequest=newSendSmsRequest() .setSignName(smsProperties.getSignName()) .setTemplateCode(smsProperties.getTemplate()) .setPhoneNumbers(phone) .setTemplateParam(JSONObject.toJSONString(map)); // 复制代码运行请自行打印 API 的返回值returnclient.sendSms(sendSmsRequest); } catch (Exceptione) { log.error("手机号码:{},发送短信失败,失败原因:{}", phone, e.getMessage()); thrownewDeveloperPlatFormException(ResponseEnum.SEND_MESSAGE_ERROR); } } /*** 使用AK&SK初始化账号Client** @param accessKeyId* @param accessKeySecret* @return Client* @throws Exception*/publicstaticcom.aliyun.dysmsapi20170525.ClientcreateClient(finalStringaccessKeyId, finalStringaccessKeySecret) throwsException { Configconfig=newConfig() // 您的AccessKey ID .setAccessKeyId(accessKeyId) // 您的AccessKey Secret .setAccessKeySecret(accessKeySecret); // 访问的域名config.endpoint="dysmsapi.aliyuncs.com"; returnnewcom.aliyun.dysmsapi20170525.Client(config); } /*** 功能描述: <br>* 〈随机数6位〉** @Return: com.ehl.developerplatform.pojo.DataResponse* @Author: 王震* @Date: 2021/4/28 19:34*/privateStringrandomCode() { returnString.valueOf((int) ((Math.random() *9+1) *Math.pow(10, 5))); } }
一些项目中的redis工具类、就不贴了,朋友们可以自行替换成自己的。