赞
踩
此处我申请的是阿里云的短信服务,登录阿里云账号号,搜索短信服务,进入控控制台,即可按自己的需求申请
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>
import java.util.Random;
public class CodeUtils {
/**
* 随机生成验证码
* @param length 长度为4位或者6位
* @return
*/
public static Integer generateValidateCode(int length){
Integer code =null;
//长度为4
if(length == 4){
//生成随机数,最大为9999
code = new Random().nextInt(9999);
if(code < 1000){
//保证随机数为4位数字
code = code + 1000;
}
//长度为6
}else if(length == 6){
//生成随机数,最大为999999
code = new Random().nextInt(999999);
if(code < 100000){
//保证随机数为6位数字
code = code + 100000;
}
//其他情况
}else{
throw new RuntimeException("只能生成4位或6位数字验证码");
}
return code;
}
}
//调用阿里短信模板
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.profile.DefaultProfile;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
public class SendMessage {
public boolean senSms(String phoneNum,String code){
//第一个参数:hangzhou
//第二个参数:AccessKey ID
//第三个参数:secret
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-hangzhou");
//要发送的手机号
request.setPhoneNumbers(phoneNum);
//设置的签名(签名管理的签名名称))
request.setSignName("");
//设置的模板(模板管理中的模板code)
request.setTemplateCode("");
//设置模板的占位符,模板中变量的实际值
request.setTemplateParam("{\"code\":\""+ code +"\"}");
try{
SendSmsResponse response = client.getAcsResponse(request);
}catch (ClientException e){
e.printStackTrace();
}
return true;
}
}
@ApiOperation("发送验证码")
@GetMapping("/sendSms")
public booleansendSms(@RequestParam(name = "phoneNum")String phoneNum,HttpSession session){
Integer code = CodeUtils.generateValidateCode(4);
//将生成的验证码保存到session中
session.setAttribute(phoneNum,code);
boolean b = sendMessage.senSms(phoneNum, code.toString());
return b;
}
5.1可以在启动类中配置swagger,然后进行测试,可以参考swagger文档配置。
5.2可以在Apifox中进行测试。
5.3可以编写对应的单元测试,养成好习惯
@SpringBootTest
class SpringbootdemoApplicationTests {
@Test
void contextLoads() {
}
}
public class MessUtils {
/**ACCESS_KEY_ID*/
public static final String ACCESS_KEY_ID = "";
/**SECRET*/
public static final String SECRET = "";
/**REGION_ID*/
public static final String REGION_ID = "cn-hangzhou";
/**验证码*/
public static final String REGIS_CODE_TEMPLATE = "";
/**通知*/
public static final String CHECK_REMIND_TEMPLATE = "";
/**签名*/
public static final String SIGN_NAME = "";
/**
* @author:
* @Description: 短信验证码
*/
public static boolean sendRegisCode(String phone, String code){
DefaultProfile profile = DefaultProfile.getProfile(REGION_ID,ACCESS_KEY_ID, SECRET);
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", REGION_ID);
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", "");
request.putQueryParameter("TemplateCode", "");
request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
return true;
} catch (com.aliyuncs.exceptions.ClientException e) {
e.printStackTrace();
return false;
} catch (ClientException e) {
e.printStackTrace();
return false;
}
}
/**
* @author:
* @Description: 保单推送提醒短信
*/
public static boolean sendCheckRemind(String phone, String taskId, BigDecimal amount){
DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, SECRET);
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phone);
request.setSignName(SIGN_NAME);
request.setTemplateCode(CHECK_REMIND_TEMPLATE);
request.setTemplateParam("{\"taskId\":\""+ taskId +"\",\"amount\":\""+ amount +"\"}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
return true;
} catch (com.aliyuncs.exceptions.ClientException e) {
e.printStackTrace();
return false;
} catch (ClientException e) {
e.printStackTrace();
return false;
}
}
}
一个在学习的开发者,勿喷,欢迎交流
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。