java阿里云短信验证码发送

	<!--阿里短信验证-->
	<dependency>
		<groupId>com.aliyun</groupId>
		<artifactId>aliyun-java-sdk-core</artifactId>
		<version>4.0.6</version>
	</dependency>
	<dependency>
		<groupId>com.aliyun</groupId>
		<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
		<version>1.1.0</version>
	</dependency>

/**

*申请短信签名喝模板,模板的参数一定要设

  • 阿里短信验证码发送

  • */

public Map send(String mobile)throws Exception, ServerException {

Map map =new HashMap();

//accessKeyId,secret填入自己的

DefaultProfile profile = DefaultProfile.getProfile(“cn-hangzhou”,accessKeyId,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”,“cn-hangzhou”);

//mobile 手机号

request.putQueryParameter(“PhoneNumbers”, mobile);

//自己的签名

request.putQueryParameter(“SignName”,“XXXXX”);

//短信模板签名

request.putQueryParameter(“TemplateCode”,“SMS_XXXXX”);

//随机生成6位数验证码

String randomCode = getRandomCode();

request.putQueryParameter(“TemplateParam”,"{“code”:""+randomCode+""}");

String code ="";

try {

CommonResponse response = client.getCommonResponse(request);

    code = com.alibaba.fastjson.JSONObject.parseObject(response.getData()).getString("Code");

}catch (com.aliyuncs.exceptions.ServerException e) {

e.printStackTrace();

}catch (ClientException e) {

e.printStackTrace();

}

if(code!=null && code.equals(“OK”)){

map.put(“randomCode”,randomCode);

map.put(“code”,“0000”);

map.put(“codemsg”,“短信发送成功”);

}else {

map.put(“randomCode”,randomCode);

map.put(“code”,“9999”);

map.put(“codemsg”,“短信发送失败”);

}

return map;

}

/**

  • 随机生成6位验证码

  • @return

*/

private String getRandomCode(){

Random random =new Random();

StringBuffer result=new StringBuffer();

for (int i=0;i<6;i++){

result.append(random.nextInt(10));

}

return result.toString();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值