用戶注冊

1.注冊流程分析

客戶端先發(fā)送一個(gè)手機(jī)號碼到后端獲取驗(yàn)證碼,此時(shí)“獲取驗(yàn)證碼”按鈕是激活狀態(tài),“下一步”按鈕是禁用狀態(tài)

點(diǎn)擊“獲取驗(yàn)證碼”按鈕,該按鈕60秒倒計(jì)時(shí)變?yōu)榻脿顟B(tài),等待用戶接收驗(yàn)證碼并輸入

后端根據(jù)這個(gè)手機(jī)號先到數(shù)據(jù)庫查詢該手機(jī)號是否已被注冊,如果已經(jīng)被注冊就返回“該手機(jī)號已被注冊”,不繼續(xù)后續(xù)步驟

如果沒被注冊,后端為該手機(jī)號生成一個(gè)隨機(jī)6位驗(yàn)證碼存在Redis中(指定時(shí)限),然后將該驗(yàn)證碼通過阿里云的短信服務(wù)接口發(fā)送給客戶端

客戶端收到短信,輸入驗(yàn)證碼,點(diǎn)擊下一步

后端收到客戶端發(fā)送的驗(yàn)證碼,就將其和Redis中暫存的驗(yàn)證碼比對,如果一致就放行,否則提示“驗(yàn)證碼錯(cuò)誤”

驗(yàn)證碼正確,進(jìn)入填寫密碼的頁面,填寫完畢提交到后端,加密存入數(shù)據(jù)庫,注冊成功,讓用戶選擇去登錄,還是回到首頁

2.Redis準(zhǔn)備

GitHub下載

解壓后放入本地目錄

運(yùn)行運(yùn)行redis-server

cdD:\tools\redisredis-server.exeredis.windows.conf

image.png

再開一個(gè)命令行窗口,進(jìn)入redis目錄,運(yùn)行redis-cli.exe命令,進(jìn)行一下基本操作

set是設(shè)置鍵值對,keys是列出所有鍵,get是獲取指定鍵的值,del是刪除指定的鍵值

image.png

3. 阿里云短信服務(wù)

短信服務(wù)

API文檔

demo程序,注意修改其中***的內(nèi)容為自己的

package com.soft1721.jianyue.api.util;importcom.aliyuncs.CommonRequest;importcom.aliyuncs.CommonResponse;importcom.aliyuncs.DefaultAcsClient;importcom.aliyuncs.IAcsClient;importcom.aliyuncs.exceptions.ClientException;importcom.aliyuncs.exceptions.ServerException;importcom.aliyuncs.http.MethodType;importcom.aliyuncs.profile.DefaultProfile;/**

* 短消息測試程序

*/publicclassSMSTest{publicstaticvoidmain(String[] args){? ? ? ? DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","*******","*****");? ? ? ? IAcsClient client =newDefaultAcsClient(profile);? ? ? ? CommonRequest request =newCommonRequest();? ? ? ? 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","****");? ? ? ? request.putQueryParameter("SignName","****");? ? ? ? request.putQueryParameter("TemplateCode","SMS_135805735");? ? ? ? request.putQueryParameter("TemplateParam","{\"code\":\"888888\"}");try{? ? ? ? ? ? CommonResponse response = client.getCommonResponse(request);? ? ? ? ? ? System.out.println(response.getData());? ? ? ? }catch(ServerException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }catch(ClientException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }? ? }}

image.png

4.后端

pom.xml,增加阿里云短信服務(wù)SDK依賴和SpringBoot集成redis依賴

com.aliyunaliyun-java-sdk-core4.0.3org.springframework.bootspring-boot-starter-data-redis

util包的StringUtil類增加一個(gè)方法,用來獲取六位隨機(jī)數(shù)驗(yàn)證碼

publicstaticStringgetVerifyCode(){? ? Random random =newRandom();? ? StringBuilder stringBuilder =newStringBuilder();for(inti =0; i <6; i++) {? ? ? ? stringBuilder.append(String.valueOf(random.nextInt(10)));? ? }returnstringBuilder.toString();}

編寫SMSUtil短信發(fā)送工具類,主要將手機(jī)號和短信字符串分離成變量,main方法測試通過后即可刪除

importcom.aliyuncs.CommonRequest;importcom.aliyuncs.CommonResponse;importcom.aliyuncs.DefaultAcsClient;importcom.aliyuncs.IAcsClient;importcom.aliyuncs.exceptions.ClientException;importcom.aliyuncs.exceptions.ServerException;importcom.aliyuncs.http.MethodType;importcom.aliyuncs.profile.DefaultProfile;/**

* 短信發(fā)送工具類,返回生成的隨機(jī)驗(yàn)證碼

*/publicclassSMSUtil{publicstaticStringsend(String mobile){? ? ? ? DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","*******","*******");? ? ? ? IAcsClient client =newDefaultAcsClient(profile);? ? ? ? CommonRequest request =newCommonRequest();? ? ? ? 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", mobile);? ? ? ? request.putQueryParameter("SignName","*****");? ? ? ? request.putQueryParameter("TemplateCode","SMS_135805735");? ? ? ? String verifyCode = StringUtil.getVerifyCode();? ? ? ? request.putQueryParameter("TemplateParam","{\"code\":"+ verifyCode +"}");try{? ? ? ? ? ? CommonResponse response = client.getCommonResponse(request);? ? ? ? ? ? System.out.println(response.getData());? ? ? ? }catch(ServerException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }catch(ClientException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }returnverifyCode;? ? }publicstaticvoidmain(String[] args){? ? ? ? System.out.println(send("139****1489"));? ? }}

mapper層增加insertUser方法,自行實(shí)現(xiàn)(id和token不用給值)

service接口增加注冊方法

voidsignUp(UserDTO userDTO);

signUp方法實(shí)現(xiàn)代碼

@OverridepublicvoidsignUp(UserDTO userDTO){? ? User user1 =newUser();? ? user1.setMobile(userDTO.getMobile()); user1.setPassword(StringUtil.getBase64Encoder(userDTO.getPassword()));? ? user1.setNickname("新用戶"); user1.setAvatar("http://ppeto2k90.bkt.clouddn.com/avatar/default.png");? ? user1.setRegtime(newDate());? ? user1.setStatus((short)1);? ? userMapper.insert(user1);}

單元測試

@TestpublicvoidsignUp(){? ? UserDTO userDTO =newUserDTO();? ? userDTO.setMobile("139****1489");? ? userDTO.setPassword("111");? ? userService.signUp(userDTO); }

controller層編寫前,先到StatusConst和MsgConst添加需要用到的常量

publicstaticfinalintMOBILE_EXIST =5;publicstaticfinalintVERIFYCODE_ERROR =6;

publicstaticfinalString MOBILE_EXIST ="手機(jī)號已被注冊";publicstaticfinalString VERIFYCODE_ERROR ="驗(yàn)證碼錯(cuò)誤";

獲取短信驗(yàn)證碼接口

@PostMapping(value ="/verify")publicResponseResultgetVerifyCode(@RequestParam("mobile")String mobile){? ? ? ? User user = userService.getUserByMobile(mobile);//手機(jī)號已經(jīng)被注冊if(user !=null) {returnResponseResult.error(StatusConst.MOBILE_EXIST, MsgConst.MOBILE_EXIST);? ? ? ? }else{//發(fā)送驗(yàn)證碼String verifyCode = SMSUtil.send(mobile);//? ? ? ? ? ? String verifyCode = StringUtil.getVerifyCode();System.out.println(verifyCode);//手機(jī)號和驗(yàn)證碼作為鍵值對存入redis中redisService.set(mobile, verifyCode);returnResponseResult.success();? ? ? ? }? ? }

驗(yàn)證短信碼接口

@PostMapping(value ="/check")public ResponseResult checkVerifyCode(@RequestParam("mobile") String mobile, @RequestParam("verifyCode") String verifyCode) {//從Redis中取出這個(gè)手機(jī)號的驗(yàn)證碼? ? String code = redisService.get(mobile).toString();//System.out.println(code +"---");//System.out.println(verifyCode);//和客戶端傳過來的驗(yàn)證碼比對if(code.equals(verifyCode)) {returnResponseResult.success();? ? }else{returnResponseResult.error(StatusConst.VERIFYCODE_ERROR, MsgConst.VERIFYCODE_ERROR);? ? }}

注冊接口

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容