【编程开发】之短信注册用户流程
生活随笔
收集整理的這篇文章主要介紹了
【编程开发】之短信注册用户流程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注冊用戶賬號需要使用手機驗證碼進行操作,而手機驗證碼發送使用的是阿里云短信服務,發送短信操作可以參考:阿里云短信服務官方文檔 。其原理也比較簡單,下面是使用步驟:
首先我們需要引入相關依賴:
<dependencies><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId></dependency> </dependencies>下面是我項目中發送驗證碼使用的一個實例,其方法過程都是固定的,只需要根據項目修改相應的參數即可:
import com.alibaba.fastjson.JSONObject; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.wang.allservice.service.msm.MsmService; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils;import java.util.HashMap;@Service public class MsmServiceImpl implements MsmService {private String accessKeyId = ""; // 改成自己阿里云上的 accessKeyIdprivate String accessSecretId = ""; // 改成自己阿里云上的 accessSecretId// 發送驗證碼@Overridepublic boolean sendCode(HashMap<String, Object> param, String phone) {if(StringUtils.isEmpty(phone)) return false;// 創建 Acs 請求客戶端DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecretId);IAcsClient client = new DefaultAcsClient(profile);//設置相關固定的參數CommonRequest request = new CommonRequest();//request.setProtocol(ProtocolType.HTTPS);request.setMethod(MethodType.POST);request.setDomain("dysmsapi.aliyuncs.com");request.setVersion("2017-05-25");request.setAction("SendSms");//設置發送相關的參數request.putQueryParameter("PhoneNumbers",phone); //手機號request.putQueryParameter("SignName","我的ES在線教育網站"); //申請阿里云 簽名名稱request.putQueryParameter("TemplateCode","SMS_199792318"); //申請阿里云 模板coderequest.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //驗證碼數據,轉換json數據傳遞try {//最終發送CommonResponse response = client.getCommonResponse(request);boolean success = response.getHttpResponse().isSuccess();return success;}catch(Exception e) {e.printStackTrace();return false;}} }注冊過程如下:
-
前端填寫完用戶注冊信息之后,點擊發送驗證碼按鈕調用短信驗證碼服務模塊接口獲取驗證碼;
-
服務器收到發送驗證請求之后,首先會根據手機號碼去 redis 中獲取數據,因為有可能之前已經發送過了,為了防止一定時間內重復發送問題。如果能獲取到則直接返回相應的驗證碼值,如果沒有獲取到則創建一個驗證碼值并調用上面的阿里云短發發送接口進行發送;
-
發送成功之后,就把發送成功驗證碼放到 redis 里面,并設置有效時間為5分鐘,流程如下:
@Api(tags = {"阿里云短信服務"}) @RestController @RequestMapping(value = "/edumsm/msm") @CrossOrigin public class MsmController {@Autowiredprivate MsmService msmService;@Autowiredprivate RedisTemplate<String, String> redisTemplatel;// 發送短信@ApiOperation(value = "發送短信")@GetMapping("/send/{phone}")public RetMsg sendMsm(@ApiParam(name = "phone", value = "手機號碼", required = true)@PathVariable String phone) {// 從redis獲取驗證碼,如果獲取得到則直接返回String code = redisTemplatel.opsForValue().get(phone);if (!StringUtils.isEmpty(code)) {return RetMsg.ok();}// 生產隨機值,傳遞給阿里云進行發送code = RandomUtils.getFourBitRandom();HashMap<String, Object> param = new HashMap<>();param.put("code", code);// 調用service發送短信的接口boolean isSend = msmService.sendCode(param, phone);if (isSend) {// 發送成功,把發送成功驗證碼放到redis里面,并設置有效時間為5分鐘redisTemplatel.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);return RetMsg.ok();} else {return RetMsg.error().message("短信發送失敗");}} } -
前端收到驗證碼后點擊注冊按鈕,發送注冊請求到用戶注冊模塊中進行注冊,下面是一個注冊流程代碼:
public void register(RegisterVo registerVo) {// 獲取用戶注冊的數據String mobile = registerVo.getMobile(); // 手機號String nickname = registerVo.getNickname(); // 用戶名String password = registerVo.getPassword(); // 密碼String code = registerVo.getCode(); // 驗證碼// 非空判斷if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname)|| StringUtils.isEmpty(password) || StringUtils.isEmpty(code)) {throw new EduShopException(20001, "注冊失敗");}// 判斷驗證碼是否有效String redisCode = redisTemplate.opsForValue().get(mobile);if (!code.equals(redisCode)) {throw new EduShopException(20001, "注冊失敗");}// 判斷手機號是否已注冊QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();wrapper.eq("mobile", mobile);Integer count = this.baseMapper.selectCount(wrapper);if (count > 0) {throw new EduShopException(20001, "注冊失敗");}// 注冊用戶數據到數據庫中UcenterMember member = new UcenterMember();member.setMobile(mobile);member.setPassword(MD5Utils.encrypt(password)); // 密碼需要MD5加密member.setNickname(nickname);member.setIsDisabled(false); // 設置用戶不禁用member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");this.baseMapper.insert(member);}
總結
以上是生活随笔為你收集整理的【编程开发】之短信注册用户流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机网页端查看百度等搜索引擎网页快照的方
- 下一篇: K12在线教育平台的产品需求