springboot手机短信验证码登录
生活随笔
收集整理的這篇文章主要介紹了
springboot手机短信验证码登录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
手機短信驗證碼登錄
- 方法
- 總結
方法
*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手机短信验证码登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外军的现役机载预警雷达(AEW)参数一览
- 下一篇: kylin云平台搭建问题