Java调阿里云短信服务,发送短信验证码

//发送验证码
public String sendVerifyCode(String phone) throws ClientException {
		//发送验证码
		String verifyCode = RandomCode.game(6);
		SendSmsResponse smsResp = SmsUtils.sendSms(phone, "短信模板", "{\"code\":\""+verifyCode+"\"}");
		if(!"OK".equals(smsResp.getCode())) {
			return Common.getErrorReturn("验证码发送失败");
		}		
		//存储到redis中
		RedisSetter.setRedisStringDuration(phone, verifyCode,86400);//五分钟内有效
		return Common.getReturn(null);
	}

//校验验证码
public String registerLogin(User user, String writeCode) {
		String verifyCode = RedisGetter.getRedisJSONStringByKey(user.getPhone());
		//校验验证码
		if(!writeCode.equals(verifyCode)) {
			return Common.getErrorReturn("验证码错误");
		}
		User userTemp = new User();
		userTemp.setUserId(user.getUserId());
		List users = userDao.selectSingleUser(userTemp);
		if(users.size()==0) {
			return Common.getErrorReturn("无此用户");
		}
		return Common.getReturn(JSONObject.parseObject(JSONObject.toJSON(users.get(0)).toString()));
	}

阿里云提供的发送短信的方法封装的工具类,可自行去阿里云查看

package com.king.util;

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

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.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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Created on 17/6/7.
 * 短信API产品的DEMO程序,工程中包含了一个SmsDemo类,直接通过
 * 执行main函数即可体验短信产品API功能(只需要将AK替换成开通了云通信-短信产品功能的AK即可)
 * 工程依赖了2个jar包(存放在工程的libs目录下)
 * 1:aliyun-java-sdk-core.jar
 * 2:aliyun-java-sdk-dysmsapi.jar
 * 

* 备注:Demo工程编码采用UTF-8 * 国际短信发送请勿参照此DEMO */ public class SmsUtils { //产品名称:云通信短信API产品,开发者无需替换 static final String product = "Dysmsapi"; //产品域名,开发者无需替换 static final String domain = "dysmsapi.aliyuncs.com"; private static final int LEN = 6; //验证码字符数组 private static char[] codeSequence = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找) static final String accessKeyId = "开发者购买后获得"; static final String accessKeySecret = "开发者购买后获得"; public static SendSmsResponse sendSms(String phoneNo, String templateCode, String param) throws ClientException { //初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象 SendSmsRequest request = new SendSmsRequest(); //使用post提交 request.setMethod(MethodType.POST); //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为国际区号+号码,如“85200000000” request.setPhoneNumbers(phoneNo); //必填:短信签名-可在短信控制台中找到 request.setSignName("北京国品时尚"); //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版 request.setTemplateCode(templateCode); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 request.setTemplateParam(param); //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.setOutId("yourOutId"); //请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { return sendSmsResponse; } return sendSmsResponse; } public static void main(String[] args) throws ClientException, InterruptedException { String verifyCode = RandomCode.game(6); SmsUtils.sendSms("phone", "短信模板", "{\"code\":\""+verifyCode+"\"}"); /* System.out.println(msMp.get("code")); System.out.println(msMp.get("sendCode"));*/ } }

生成随机数的工具类:

public class RandomCode {
	
	public static String game(int count){
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for(int i=0;i

你可能感兴趣的:(java)