springboot手机短信验证码登录
生活随笔
收集整理的這篇文章主要介紹了
springboot手机短信验证码登录
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
手機(jī)短信驗(yàn)證碼登錄
- 方法
- 總結(jié)
方法
*1.獲取手機(jī)驗(yàn)證碼
/*** 獲取手機(jī)驗(yàn)證碼*/@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);}// 調(diào)用短信接口String authCode = RandomNumUtils.getRandomNum();SMSUtils.sendTextMessage(user.getPhone(),authCode);// 存入sessionrequest.getSession().setAttribute(user.getPhone(),authCode);// 過(guò)期時(shí)間300秒request.getSession().setMaxInactiveInterval(300);}else {throw new ServiceException(BizExceptionEnum.PHONE_NULL);}return ResponseData.success();}/*** 用戶手機(jī)登錄*/@RequestMapping("/phoneLogin")@ResponseBodypublic ResponseData phoneLogin(UserDto user,HttpSession session) {if (ToolUtil.isOneEmpty(user.getPhone(), user.getVerificationCode())) {throw new ServiceException(BizExceptionEnum.PHONE_CODE_NULL);}// 賬號(hào)校驗(yàn)User theUser = this.userService.getByPhone(user.getPhone());if (theUser == null) {throw new ServiceException(BizExceptionEnum.USER_NOT_EXISTED);}// 校驗(yàn)驗(yàn)證碼是否失效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())){//登錄并創(chuàng)建tokenString token = authService.login(theUser.getAccount());authService.addLoginCookie(token);return ResponseData.success();}else {throw new ServiceException(BizExceptionEnum.PHONE_CODE_ERROR);}}2.短信發(fā)送
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; /*** 短信發(fā)送工具類*/ public class SMSUtils {/*** 訪問(wèn)秘鑰*/private static final String ACCESSKEYID = "xxxxxxx";private static final String ACCESSKEYSECRET = "xxxxxx";/*** 模板號(hào)*/public static final String VALIDATE_CODE = "xxxxxxx";/*** 發(fā)送短信** @param phoneNumbers 手機(jī)號(hào)碼* @param authCode 驗(yàn)證碼*/public static void sendTextMessage (String phoneNumbers, String authCode) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",ACCESSKEYID,ACCESSKEYSECRET);IAcsClient client = new DefaultAcsClient(profile);// 組裝請(qǐng)求對(duì)象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.隨機(jī)生成驗(yàn)證碼
/*** 隨機(jī)生成六位數(shù)驗(yàn)證碼**/public class RandomNumUtils {public static String getRandomNum() {Random r = new Random();return (r.nextInt(900000) + 100000) + "";} }總結(jié)
前后端分離時(shí),注意跨域?qū)е碌膕essionID不一致的問(wèn)題。
總結(jié)
以上是生活随笔為你收集整理的springboot手机短信验证码登录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 外军的现役机载预警雷达(AEW)参数一览
- 下一篇: kylin云平台搭建问题