短信实现:
1、构造手机验证码:使用random对象生成要求的随机数作为验证码,例如4位验证码:1000~9999之间随机数;
2、使用接口向短信平台发送手机号和验证码数据,然后短信平台再把验证码发送到制定手机号上,接口参数一般包括:目标手机号,随机验证码(或包含失效时间),平台接口地址,平台口令;
3、保存接口返回的信息(一般为json文本数据,然后需转换为json对象格式);
4、将手机号–验证码、操作时间存入Session中,作为后面验证使用;
5、接收用户填写的验证码及其他数据;
6、对比提交的验证码与Session中的验证码是否一致,同时判断提交动作是否在有效期内;
7、验证码正确且在有效期内,请求通过,处理相应的业务
飞鸽国际短信实现过程:
controller层:
import com.yy.service.YuumiUserService;
import com.yy.util.ResponseProtocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private YuumiUserService yuumiUserService;
@RequestMapping("/v1.0/sendMessage")
public ResponseProtocol<String> message(String telephone,String areaCode) {
return this.yuumiUserService.message(telephone,areaCode);
}
}
util层:
**public class ResponseProtocol<T> {
/***
* 响应信息,无论成功还是失败,都需要写入相应的信息
*/
private String message;
/**
* 0失败 1成功
*/
private Integer resultCode;
/***
* 响应数据
*/
private T data;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getResultCode() {
return resultCode;
}
public void setResultCode(Integer resultCode) {
this.resultCode = resultCode;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}**
生成验证码:
import java.util.Date;
/**
* @author
* 生成6位数验证码
*/
public class VolidateCodeUtil {
public static Integer getVolidateCode() {
Integer random = (int)(Math.random()*899999)+100000;
return random;
}
}
constant:常量层
public class Constant {
public final static String CODE_ERROR = "验证码不正确!";
public final static String SUCCESS = "success!";
public final static String FAILED = "failed!";
public final static int RET_CODE_SUCCESS = 1;
public final static int RET_CODE_FAILED = 0;
public final static int RET_CODE_FAILED2 = 2;
}
Service层:
import com.yy.util.ResponseProtocol;
public interface YuumiUserService {
/**
* 发送短信验证码
* @param tel
* @return
*/
public ResponseProtocol<String> message(String tel,String areaCode);
}
Servicelmpl层:
import com.yy.config.Config;
import com.yy.service.YuumiUserService;
import com.yy.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
/**
* 用户表
*/
@Service
public class YuumiUserServiceImpl implements YuumiUserService {
private static Logger log = LoggerFactory.getLogger(YuumiUserServiceImpl.class);
@Autowired
private Config config;
/**
* 发送短信验证码
* @param tel
* @return
*/
public ResponseProtocol<String> message(String tel,String areaCode) {
System.out.println("tel="+tel);
System.out.println("areaCode="+areaCode);
ResponseProtocol<String> resp = new ResponseProtocol<String>();
if(tel==null||areaCode==null){
resp.setMessage("地区码|号码不能为空!");
resp.setResultCode(Constant.RET_CODE_FAILED);
return resp;
}
//生成验证码
Integer code = VolidateCodeUtil.getVolidateCode();
//调用feige发送接口
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("Account",config.getMsgAccount());//在飞鸽传书应用集成中创建的短信应用账号
params.add("Pwd",config.getMsgPwd());//飞鸽传书应用集成中的短信请求接口密钥
params.add("Content",code.toString());//验证码
params.add("Mobile",areaCode+tel);//手机号码 格式(区号+手机号码) 例如:8615800000000,其中86为中国的区号
params.add("SignId",config.getMsgSignId());//短信签名建议2-8字符,id可在管理后台获取、添加、编辑
params.add("TemplateId",config.getMsgTemp());//短信免审模板ID,id可在管理后台获取、添加、编辑
String res = HttpUtil.sendPostRequest(config.getFeigeUrl(),params);
ResultVO vo = HttpUtil.convert(res);
if(!"OK".equals(vo.getMessage())){
log.error("发送短信报错!");
resp.setMessage("发送短信报错!");
resp.setResultCode(Constant.RET_CODE_FAILED);
return resp;
}
resp.setData(code.toString());
resp.setMessage(Constant.SUCCESS);
resp.setResultCode(Constant.RET_CODE_SUCCESS);
return resp;
}
}
config层:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Component
public class Config {
@Value("${feige.message.feigeUrl}")
String feigeUrl;
@Value("${feige.message.msgAccount}")
String msgAccount ;
@Value("${feige.message.msgPwd}")
String msgPwd ;
@Value("${feige.message.msgTemp}")
String msgTemp;
@Value("${feige.message.msgSignId}")
String msgSignId ;
public String getFeigeUrl() {
return feigeUrl;
}
public void setFeigeUrl(String feigeUrl) {
this.feigeUrl = feigeUrl;
}
public String getMsgAccount() {
return msgAccount;
}
public void setMsgAccount(String msgAccount) {
this.msgAccount = msgAccount;
}
public String getMsgPwd() {
return msgPwd;
}
public void setMsgPwd(String msgPwd) {
this.msgPwd = msgPwd;
}
public String getMsgTemp() {
return msgTemp;
}
public void setMsgTemp(String msgTemp) {
this.msgTemp = msgTemp;
}
public String getMsgSignId() {
return msgSignId;
}
public void setMsgSignId(String msgSignId) {
this.msgSignId = msgSignId;
}
}