🎈个人公众号:🎈 :✨✨ 可为编程 ✨✨ 🍟🍟
🔑个人信条:🔑 知足知不足 有为有不为 为与不为皆为可为🌵
🍉本篇简介:🍉 本片详细说明了JAVA采用手机号获取短信验证进行登录与注册使用规则和注意要点,并给出具体操作实例,如有出入还望指正。关注公众号【可为编程】回复【面试】领取最新面试题!!!
/**
* 用户注册发送短信
*/
@PostMapping("/sendMassage")
@ApiOperationSupport(order = 12)
@ApiOperation(value = "用户注册发送短信", notes = "传入electricMassage")
//@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public R sendMassage(String phone) throws IOException {
Boolean flag = false;
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://xxxxxxxx:8080/xxxx.do");
post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
NameValuePair[] data = {
new NameValuePair("corp_id", "xxx"),
new NameValuePair("corp_pwd", "xxx"),
new NameValuePair("corp_service", "1069106967491"),
new NameValuePair("mobile", phone),
new NameValuePair("msg_content", "尊敬的用户,您好,您的验证码为:"+ verifyCode + ",验证码失效时间为5分钟,若非本人操作,请忽略此短信。"),
new NameValuePair("corp_msg_id", ""),
new NameValuePair("ext", "")
};
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
for (Header h : headers) {
System.out.println(h.toString());
}
int statusCode = post.getStatusCode();
if (statusCode == 200) {
flag = saveCaptcha(verifyCode, phone);
if (flag) {
String result = new String(post.getResponseBodyAsString());
System.out.println(result);
post.releaseConnection();
}
}
return flag ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL);
}
/**
* 用户注册接口
*
* @param user 用户信息
* @return
*/
@PostMapping("/regist")
@ApiOperationSupport(order = 12)
@ApiOperation(value = "用户注册接口", notes = "传入code")
//@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
public R regist(@Valid @RequestBody User user) {
boolean validate = checkCaptcha(user.getCaptcha(), user.getPhone());
Boolean index = false;
if (validate) {
CacheUtil.clear(USER_CACHE);
index = userService.submit(user);
}
return R.status(index);
}
//讲注册用的验证码存入到redis
public Boolean saveCaptcha(String captcha, String phone) {
try {
//将验证码存储在redis中,并且设置过期时间5分钟
redisTemplate.boundValueOps(phone).set(captcha, 300, TimeUnit.SECONDS);
return true;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
//验证短信验证码
public Boolean checkCaptcha(String captcha, String phone) {
try {
if (captcha.equals(redisTemplate.boundValueOps(phone).get())) {
return true;
} else {
return false;
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
欢迎感兴趣的小伙伴一起探讨学习知识,以上是个人的一些总结分享,如有错误的地方望各位留言指出,十分感谢。觉得有用的话别忘点赞、收藏、关注,手留余香! 😗 😗 😗
这里是一个真诚的***青年技术交流QQ群:761374713***,不管你是大学生、社畜、想学习变成的其他人员,欢迎大家加入我们,一起成长,一起进步,真诚的欢迎你,不管是技术,还是人生,还是学习方法。有道无术,术亦可求,有术无道,止于术。
欢迎大家关注【可为编程】,成长,进步,编程,技术、掌握更多知识!