手机短信验证码登录

  • 方法
  • 总结

方法

*1.获取手机验证码

     /*** 获取手机验证码*/@RequestMapping("/getVerificationCode")@ResponseBodypublic ResponseData getVerificationCode(UserDto user, HttpServletRequest request, HttpServletResponse response, HttpSession session) {if (ToolUtil.isNotEmpty(user.getPhone())) {if(ToolUtil.isNotEmpty(session.getAttribute(user.getPhone()))){throw new ServiceException(BizExceptionEnum.AUTH_CODE_SENT);}// 调用短信接口String authCode = RandomNumUtils.getRandomNum();SMSUtils.sendTextMessage(user.getPhone(),authCode);// 存入sessionrequest.getSession().setAttribute(user.getPhone(),authCode);// 过期时间300秒request.getSession().setMaxInactiveInterval(300);}else {throw new ServiceException(BizExceptionEnum.PHONE_NULL);}return ResponseData.success();}/*** 用户手机登录*/@RequestMapping("/phoneLogin")@ResponseBodypublic ResponseData phoneLogin(UserDto user,HttpSession session) {if (ToolUtil.isOneEmpty(user.getPhone(), user.getVerificationCode())) {throw new ServiceException(BizExceptionEnum.PHONE_CODE_NULL);}// 账号校验User theUser = this.userService.getByPhone(user.getPhone());if (theUser == null) {throw new ServiceException(BizExceptionEnum.USER_NOT_EXISTED);}// 校验验证码是否失效if(ToolUtil.isEmpty(session.getAttribute(user.getPhone()))){throw new ServiceException(BizExceptionEnum.CODE_EXPIRY);}String authCode = (String) session.getAttribute(user.getPhone());if(authCode.equals(user.getVerificationCode())){//登录并创建tokenString token = authService.login(theUser.getAccount());authService.addLoginCookie(token);return  ResponseData.success();}else {throw new ServiceException(BizExceptionEnum.PHONE_CODE_ERROR);}}

2.短信发送

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*** 短信发送工具类*/
public class SMSUtils {/*** 访问秘钥*/private static final String ACCESSKEYID = "xxxxxxx";private static final String ACCESSKEYSECRET = "xxxxxx";/*** 模板号*/public static final String VALIDATE_CODE = "xxxxxxx";/*** 发送短信** @param phoneNumbers 手机号码* @param authCode     验证码*/public static void sendTextMessage (String phoneNumbers, String authCode) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",ACCESSKEYID,ACCESSKEYSECRET);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");request.putQueryParameter("PhoneNumbers", phoneNumbers);// 短信签名名称request.putQueryParameter("SignName", "xxxxxx");request.putQueryParameter("TemplateCode", VALIDATE_CODE);request.putQueryParameter("TemplateParam", "{\"code\":\"" + authCode + "\"}" );try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}}
}

3.随机生成验证码

    /*** 随机生成六位数验证码**/public class RandomNumUtils {public static String getRandomNum() {Random r = new Random();return (r.nextInt(900000) + 100000) + "";}
}

总结

前后端分离时,注意跨域导致的sessionID不一致的问题。

springboot手机短信验证码登录相关推荐

  1. SpringBoot + SpringSecurity 短信验证码登录功能实现

    实现原理 在之前的文章中,我们介绍了普通的帐号密码登录的方式:SpringBoot + Spring Security 基本使用及个性化登录配置(http://www.deiniu.com/artic ...

  2. cas5.3.9自定义手机短信验证码登录

    cas自定义多种登录方式 cas添加手机短信验证码登录 cas添加手机短信验证码登录 全部基于SpringBoot,以及SpringWebflow开发,请在有此基础再进行学习! 添加Maven依赖 & ...

  3. 手机短信验证码登录功能的开发实录(机器识别码、短信限流、错误提示、发送验证码倒计时60秒)

    短信验证码登录功能 项目分析 核心代码 1.外部js库调用 2.HTML容器构建 3.javaScript业务逻辑验证 4.后端验证逻辑 总结 短信验证码是通过发送验证码到手机的一种有效的验证码系统, ...

  4. 【SDU项目实训2019级】前端和后端实现手机短信验证码登录和注册功能

    目录 1.前端登录页面手机号验证码登录页面: 2.前端获取验证码的函数: 3.后端获取验证码代码 4.前端登录的函数: 5.后端登录的实现 6.注册功能前后端的实现 1.前端登录页面手机号验证码登录页 ...

  5. java手机验证码登录代码_java web实现手机短信验证码登录实例

    运行环境 jdk7+tomcat7 项目技术(必填) Servlet+Ajax+Bootstrap 数据库文件 我这里没用到数据库,比较简单,如果需要用到数据库不会的话可以私信我或者加我QQ jar包 ...

  6. java秒嘀短信登录验证实例_java web实现手机短信验证码登录实例

    运行环境 jdk7+tomcat7 项目技术(必填) Servlet+Ajax+Bootstrap 数据库文件 我这里没用到数据库,比较简单,如果需要用到数据库不会的话可以私信我或者加我QQ jar包 ...

  7. 5.Spring Security 短信验证码登录

    Spring Security 短信验证码登录 在 Spring Security 添加图形验证码一节中,我们已经实现了基于 Spring Boot + Spring Security 的账号密码登录 ...

  8. SpringSecurity短信验证码登录

    短信验证码登录 时下另一种非常常见的网站登录方式为手机短信验证码登录,但Spring Security默认只提供了账号密码的登录认证逻辑,所以要实现手机短信验证码登录认证功能,我们需要模仿Spring ...

  9. Spring Security 短信验证码登录(5)

    在Spring Security添加图形验证码中,我们已经实现了基于Spring Boot + Spring Security的账号密码登录,并集成了图形验证码功能.时下另一种非常常见的网站登录方式为 ...

最新文章

  1. ListBox滚动条置底
  2. jQuery操作标签--样式、文本、属性操作, 文档处理
  3. 企业中书写css,web前端开发企业级CSS常用命名,书写规范总结(示例代码)
  4. 从Boxee的Amie Street访问音乐
  5. leetcode之回溯backtracing专题2
  6. 固态函数不正确_固态硬盘可靠吗?
  7. Hibernate 多对多关系实现
  8. [论文翻译]Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation
  9. Beginning Python chapter 2 Lists and Tuples:1 Indexing and slicing
  10. 利用ldirectord实现lvs后端realserver健康状态检查
  11. php快速就业教程,PHP就业快学教程004,基本语法“条件控制语句”!
  12. scratch 控制、侦测、数据和数字逻辑模块  教案
  13. 计算机自带键盘无法使用,笔记本电脑键盘用不了怎么回事_笔记本电脑键盘不能用如何解决-win7之家...
  14. 图像处理:理想低通滤波器、butterworth滤波器(巴特沃斯)、高斯滤波器实现(python)
  15. SDCard权限设置
  16. for update造成的锁表以及解锁
  17. 系统时不变性与因果性的判断总结
  18. 快速原型模型(Rapid Prototype Model)
  19. 【C语言】之实现十进制转换为二进制
  20. RK3399平台开发系列讲解(SPI子系统)4.36、SPI子系统驱动框架详解

热门文章

  1. [趣谈网络协议学习] 08 世界这么大,我想出网关:欧洲十国游与玄奘西行
  2. PHP美团开放平台开发记录,美团外卖,第一步授权演示及错误提示:获取门店出错(3)解决方法
  3. h5、select下拉框右边加图标,深度美化页面增进用户体验
  4. 外霍尔水流量传感器0.3-10L/min 3.6-26.4V
  5. VyOS 路由器系统基本配置1
  6. CAS 票根‘ST-xxxxx‘不符合目标服务问题解决
  7. Java编程配置思路详解 1
  8. 天刀登录显示连接服务器失败ipis,天刀游戏服务器ip是什么意思
  9. HiC-Pro实战详解
  10. Charles主要功能讲解