【编程开发】之短信注册用户流程


注册用户账号需要使用手机验证码进行操作,而手机验证码发送使用的是阿里云短信服务,发送短信操作可以参考:阿里云短信服务官方文档 。其原理也比较简单,下面是使用步骤:

首先我们需要引入相关依赖:

<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 = "";	// 改成自己阿里云上的 accessKeyId
    private String accessSecretId = ""; // 改成自己阿里云上的 accessSecretId

    // 发送验证码
    @Override
    public 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"); //申请阿里云 模板code
        request.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 {
    
        @Autowired
        private MsmService msmService;
    
        @Autowired
        private 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);
        }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中国电信移动终端需求白皮书-短信注册功能分册 ................................ 1 1 范围....................................................................... 1 2 规范性引用文件 ............................................................. 1 3 缩略语、术语和定义 ......................................................... 1 3.1 缩略语................................................................. 1 3.2 术语和定义............................................................. 1 4 要求等级................................................................... 1 5 CDMA 短信注册功能 ........................................................ 1 AUTOReg-50001 [必选] 单卡终端短信注册流程要求 ...................... 1 AUTOReg-50002 [必选] 双卡终端短信注册流程要求 ...................... 2 AUTOReg-50003 [必选] 终端短信注册消息格式及内容要求 ................ 3 AUTOReg-50004 [必选] 终端短信注册业务流程要求 ...................... 5 6 IMS 短信注册功能 ......................................................... 7 AUTOReg-60001 [必选] 单卡终端 IMS 短信注册流程要求 .................. 7 AUTOReg-60002 [必选] 双卡终端 IMS 短信注册流程要求 .................. 8 AUTOReg-60003 [必选] 终端 IMS 短信注册消息格式及内容要求 ............ 8 AUTOReg-50004 [必选] 终端 IMS 短信注册业务流程要求 ................. 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值