一、短信验证码业务
我用的是第三平台的短信服务,当用户点击发送验证码,会调用短信平台接口,从而给手机发验证码。流程如下:
c
首先需要工具类,来发送验证码
public class DXMessageUtil {
public static Boolean sendMessage(Long phone, String code){
int appId = xxxxxx;
String appKey = "xxxxxxxxxxxxxxxxxxx";
String phoneNumber = String.valueOf(phone);
int templateId = xxxx; // 短信正文模板
String smsSign = "xxxx"; //签名内容
//可能获取失败,所以放在try-chach中
try {
String[] params = {code};//传入验证码和时间数组,根据短信模板自行调整
log.info("入参电话:"+phoneNumber);
log.info("入参验证码:"+code);
//创建短信调用接口 传入刚刚的应用id 和 key
SmsSingleSender sender = new SmsSingleSender(appId, appKey);
//调用方法发送短信 传入国家码 电话号码 模板id 模板参数 签名 扩展码为空 返回参数为空
SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber, templateId,params,smsSign,"","");
} catch(HTTPException e) {
// HTTP响应码错误
log.info("HTTP响应码错误!");
e.printStackTrace();
return false;
} catch(JSONException e) {
// json解析错误
log.info("json解析错误!");
e.printStackTrace();
return false;
} catch(IOException e) {
// 网络IO错误
log.info("网络IO错误!");
e.printStackTrace();
return false;
}
return true;
}
//随机生成6位短信验证码的函数
public static String random(){
String x= String.valueOf((int)((Math.random()*9+1)* Math.pow(10,6-1)));
return x;
}
//
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
}
二、短信验证登录业务
接下来我们看下登录的流程
登录的代码:
@RequestMapping("getPhoneCode")
public ResultJson getPhoneCode(@RequestBody SysUserNewVo sysUserNewVo){
Long phone = sysUserNewVo.getPhone();
log.info("getPhoneCode入参:"+phone);
//1、判断手机号是否为空
if(StringUtils.isEmpty("phone")){
return ResultJson.error("手机号不能为空!");
}
//2、判断手机号是否符合格式(使用正则表达式实现)
String regex = "^((11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15([0-9]))|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
if (!Pattern.matches(regex, String.valueOf(phone))) {
return ResultJson.error("手机号格式不正确!");
}
//3、将验证码保存至服务器Redis
String code = DXMessageUtil.random();
// redisUtil.set("phoneCode",code,60*10);
redisUtil.setex("phoneCode",code,60L);
log.info("出参:生成的验证码:"+redisUtil.get("phoneCode"));
//4、发送短信验证码
if(!DXMessageUtil.sendMessage(phone,String.valueOf(redisUtil.get("phoneCode")))){
return ResultJson.error("验证码发送失败!");
}
return ResultJson.ok("验证码发送成功!");
}
首先是进行判断手机号是否为空、格式是否正确,然后将验证码存入redis中
5@RequestMapping("phoneLogin")
public ResultJson phoneLogin(@RequestBody SysUserNewVo sysUserNewVo){
Long phone = sysUserNewVo.getPhone();
String code = sysUserNewVo.getCode();
//1、判断手机号是否为空
if(phone.equals(null)){
return ResultJson.error("手机号不能为空!");
}
//2、判断手机号格式是否正确
String regex = "^((11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15([0-9]))|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
if (!Pattern.matches(regex, String.valueOf(phone))) {
return ResultJson.error("手机号格式不正确!");
}
//3、判断距离上次发送该类型的验证码是否超过5分钟,失效
String oldCode = String.valueOf(redisUtil.get("phoneCode"));
log.info("验证码发送超时进行测试oldCode:"+oldCode);
//if(!DXMessageUtil.isBlank(oldCode)){
if((String.valueOf(redisUtil.get("phoneCode")))==null){
return ResultJson.error("验证码已失效!");
}
//4、判断用户是否存在
SysUserNewVo userInfo = loginService.getUserInfoByPhone(phone);
if(userInfo==null){
return ResultJson.error("不存在此用户!");
}
String codeRedis = String.valueOf(redisUtil.get("phoneCode"));
log.info("pLogin的出参code: "+code+"; codeRedis:"+codeRedis+"; phone: "+phone);
SysUserNewVo x = loginService.phoneLogin(phone,code,codeRedis);
if(x!=null){
String token = JwtUtil.createToken(x.getAccount());
x.setToken(token);
List<SysRole> roleList = sysRoleService.getRoleById(x.getId());
x.setRoleList(roleList);
redisUtil.set("SysUser",JSON.toJSONString(x));
SysUserNewVo xxx = JSON.parseObject(String.valueOf(redisUtil.get("SysUser")), SysUserNewVo.class);
redisUtil.set("sysUserAccount",x.getAccount());
return ResultJson.ok(x);
}
return ResultJson.error("验证码输入错误!");
}
然后判断验证码是否失效,我目前是设置的5分钟,在然后判断用户是否存在。登陆成功后,会将用户的信息存入到token里