阿里云短信验证Java版
1.导入相关坐标
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.6.0</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>2.1.0</version> </dependency>
2.创建工具类
(1) 短信发送工具类
public class SMSUtils { /** * 发送短信 * @param signName 签名 * @param templateCode 模板 * @param phoneNumbers 手机号 * @param param 参数 */ public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){ DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", ""); IAcsClient client = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setSysRegionId("cn-hangzhou"); request.setPhoneNumbers(phoneNumbers); request.setSignName(signName); request.setTemplateCode(templateCode); request.setTemplateParam("{\"code\":\""+param+"\"}"); try { SendSmsResponse response = client.getAcsResponse(request); System.out.println("短信发送成功"); }catch (ClientException e) { e.printStackTrace(); } } }
(2) 随机生成验证码工具类
public class ValidateCodeUtils { /** * 随机生成验证码 * @param length 长度为4位或者6位 * @return */ public static Integer generateValidateCode(int length){ Integer code =null; if(length == 4){ code = new Random().nextInt(9999);//生成随机数,最大为9999 if(code < 1000){ code = code + 1000;//保证随机数为4位数字 } }else if(length == 6){ code = new Random().nextInt(999999);//生成随机数,最大为999999 if(code < 100000){ code = code + 100000;//保证随机数为6位数字 } }else{ throw new RuntimeException("只能生成4位或6位数字验证码"); } return code; } /** * 随机生成指定长度字符串验证码 * @param length 长度 * @return */ public static String generateValidateCode4String(int length){ Random rdm = new Random(); String hash1 = Integer.toHexString(rdm.nextInt()); String capstr = hash1.substring(0, length); return capstr; } }
3.控制层测试
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/sendMsg") public R<String> sendMsg(@RequestBody User user, HttpSession session) { //获取手机号 String phone = user.getPhone(); if (StringUtils.isNotEmpty(phone)) { // 生成随机的4位验证码 String code = ValidateCodeUtils.generateValidateCode(4).toString(); // 调用阿里云提供的短信服务API完成发送短信 SMSUtils.sendMessage("测试名字","",phone,code); // 将生成的验证码保存到Session session.setAttribute(phone, code); return R.success("手机验证码发送成功"); } return R.error("短信发送失败"); } @PostMapping("/login") public R<User> login(@RequestBody Map map, HttpSession session) { // 获取手机号 String phone = map.get("phone").toString(); // 获取验证码 String code = map.get("code").toString(); // 从session中获取保存的验证码 Object codeInSession = session.getAttribute(phone); // 进行验证码的比对 if (codeInSession != null && codeInSession.equals(code)) { // 如果比对成功,说明登录成功 LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(User::getPhone, phone); User user = userService.getOne(queryWrapper); if (user == null) { // 判断当前手机号对应的用户是否为新用户,如果是新用户就自动完成注册 user = new User(); user.setPhone(phone); user.setStatus(1); userService.save(user); } session.setAttribute("user", user.getId()); return R.success(user); } return R.error("登录失败"); } }