一.前言

今天给大家分享一下利用Springboot玩腾讯云短信验证码的服务,把真正的短信验证码发送到手机上并且存进Redis里面,再判断验证码的正确性实现注册用户功能,为什么不用到登录功能,因为腾讯云的短信验证码免费的只有一百条,博主没钱购买。。。

二.功能实现

第一步

首先,大家按照下面这个博主分享的方法,先注册公众号和注册腾讯云服务器,我只是在他的基础上加上了注册功能的实现,并补充一下需要注意的地方。

博主

第二步

一,博主用的是centos7系统,先在虚拟机查出虚拟机的ip地址,用命令ifconfig可查看,这个inet对应的就是ip地址,并记住

二,再用ping命令测试你的centos7是否可以连接本机,本机指的是你当前的windows电脑,用ipconfig查询IP地址


有回应表示ping成功。

第三步

整合到springboot中,我们现在在yml的配置文件中连接Redis

#配置redisredis:#刚刚查询到的虚拟机IP地址host: ***    port: 6379  timeout: 20000#redis的密码,如果没设置就删掉password: ***database: 0pool:max-active: 8min-idle: 0max-idle: 8max-wait: -1

把上面的***换成你们自己电脑的信息,然后就根据那个博主的文章进行操作。
然后根据你redis的安装路径启动redis,我的是命令是 redis-server /etc/redis.conf ,然后访问 redis-cli ,有密码的需要用auth命令登录。

第四步

实现注册功能

注册页代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>注册</title><!-- Font Icon --><link rel="stylesheet" th:href="@{/fonts/material-icon/css/material-design-iconic-font.min.css}"><!-- Main css --><link rel="stylesheet" th:href="@{/css/style-regist.css}">
</head>
<body>
<div class="main" style="padding: 50px 0"><!-- Sing in  Form --><section class="sign-in"><div class="container"><div class="signin-content"><div class="signin-image"><figure><img th:src="@{/images/signup-image.jpg}" alt="sing up image"></figure><a th:href="@{/toLogin}" class="signup-image-link">登录</a></div><div class="signin-form"><h2 class="form-title">欢迎注册</h2><h6 id="msg" style="color: orangered"></h6><form method="POST" class="register-form" id="login-form"><div class="form-group"><label for="username"><i class="zmdi zmdi-account material-icons-name"></i></label><input type="text" name="username" id="username" placeholder="用户名"/></div><div class="form-group"><label for="password"><i class="zmdi zmdi-lock"></i></label><input type="password" name="password" id="password" placeholder="密码" required></div><div class="form-group"><label for="email"><i class="zmdi zmdi-email"></i></label><input type="email" name="email" id="email" placeholder="邮箱" required></div><div class="form-group"><label for="address"><i class="zmdi zmdi-pin"></i></label><input type="text" name="address" id="address" placeholder="地址" required></div><div class="form-group"><label for="phone"><i class="zmdi zmdi-phone"></i></label><input type="text" name="phone" id="phone" placeholder="电话"/></div><div class="form-group display-flex align-items-center"><label for="phoneCode"><i class="zmdi zmdi-shield-check"></i></label><input type="text" name="phoneCode" id="phoneCode" placeholder="验证码" style="width: 50%"><input class="form-submit " type="button" name="setPhoneCode" id="setPhoneCode"value="发送验证码" onclick="settime(this)" style="padding: 8px 35px;margin-top: 0px"/></div><div class="form-group form-button"><input type="button" name="register" id="register" class="form-submit" value="注册"/></div></form><div class="social-login"><span class="social-label">Or login with</span><ul class="socials"><li><a href="#"><i class="display-flex-center zmdi zmdi-facebook"></i></a></li><li><a href="#"><i class="display-flex-center zmdi zmdi-twitter"></i></a></li><li><a href="#"><i class="display-flex-center zmdi zmdi-google"></i></a></li></ul></div></div></div></div></section>
</div>
<!-- JS -->
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/main-regist.js}"></script>
<script type="text/javascript">$(function () {$("#register").click(function () {var username = $("#username").val();var password = $("#password").val();var email = $("#email").val();var address = $("#address").val();var phone = $("#phone").val();var phoneCode = $("#phoneCode").val();$.ajax({type: "post",url: "/user/register",data: {username: username,password: password,email: email,address: address,phone: phone,phoneCode: phoneCode,},dataType: "json",success: function (data) {console.log("data-------", data)if (data.code == 200) {{console.log(data.message)window.location.href = "/index";}} else {$("#msg").text(data.message);console.log(data.code);console.log(data.message)// refresh();}}});})})$(function () {$("#setPhoneCode").click(function () {var phone = $("#phone").val();console.log(phone)$.ajax({type: "post",url: "/user/phoneCode",data: {phone: phone,},dataType: "json",success: function (data) {console.log("data-------", data)if (data.code == 200) {{console.log(data.message)//window.location.href = "/index";}} else {$("#msg").text(data.message);console.log(data.code);console.log(data.message)// refresh();}}});})})/*发送短信后60秒才能再次发送*/var countdown = 60;function settime(val) {if (countdown == 0) {val.removeAttribute("disabled");val.value = "发送验证码";countdown = 60;} else {val.setAttribute("disabled", true);val.value = "重新发送(" + countdown + ")";countdown--;}setTimeout(function () {settime(val)}, 1000)}
</script>
</body></html>

控制器Controller

    //注册@RequestMapping("/register")@ResponseBodypublic RespBean register(User user,String phone,String phoneCode){//首先去redis中查该规定时间内手机号有没有生成验证码String code = redisTemplate.opsForValue().get(phone);System.out.println("redis中的验证码----------"+code);AssertUtil.isTrue(StringUtil.isEmpty(code), "验证码已过期!");AssertUtil.isTrue(!(phoneCode.equals(code)),"验证码错误");userService.register(user);return RespBean.success("注册成功");}

Service层

void register(User user);

Serviceimpl

 //注册@Override@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)public void register(User user) {AssertUtil.isTrue(StringUtil.isEmpty(user.getUsername()), "用户名不能为空!");AssertUtil.isTrue(null != this.findUserByUsername(user.getUsername()), "用户名已存在");user.setPassword(passwordEncoder.encode(user.getPassword()));user.setStatus("正常");user.setType(0);user.setImgPath("/upload/8473656f-695c-4e5d-9d7f-190f58be3249.jpg");AssertUtil.isTrue(!(this.save(user)), "用户添加失败");}

第五步

默认是0号库,先看看库里面有没有key

然后我们就发送一下验证码,有可能会遇到报错问题,说连接超时等问题,可能出现的原因:
第一,6379端口没开放
第二,ip地址写错了或者没用密码登录
第三,防火墙没关闭,用以下命令操作

1、查看防火墙状态
systemctl status firewalld.service
active(running)说明防火墙已经被打开了。
Active: inactive (dead) :说明防火墙关闭。2、在命令行中输入systemctl stop firewalld.service 关闭防火墙3、再在命令行中输入命令“systemctl disable firewalld.service” 永久关闭防火墙

三.总结

最后就发送成功啦,也收到短信了,还是挺好玩的。



希望大家关注一下《南辞个人公众号》哈,有资料也会跟大家分享,最后希望大家多多指教,大家一起进步。

Springboot整合Redis实现腾讯云发送短信验证码并实现注册功能相关推荐

  1. 腾讯云发送短信验证码服务

    腾讯云发送短信验证码服务 1.注册腾讯云的账号 在腾讯云的官网:https://cloud.tencent.com/注册一个腾讯云的账号,就是日常的注册流程(这里就不贴图了),不过要实名认证啥的,认证 ...

  2. TP6 腾讯云发送短信验证码配置详解

    一.发送注册验证码代码实现 参考腾讯云文档 https://cloud.tencent.com/document/product/382/56058 1.通过composer安装 composer r ...

  3. thinkphp5使用腾讯云发送短信验证码服务

    1.打开腾讯云官网开通短信服务:https://console.cloud.tencent.com/sms/smslist,需要实名验证 2.下载相对应的sdk文件,我这里是php文件 3.解压后将以 ...

  4. Go语言初识应用--容联云发送短信验证码、手机号注册

    使用gin框架.gorm映射 所使用的连接容联云参考容联云官方文档,放置到utils中, gin项目结构根据自身需要,大题如下设置: utils--sms.go package main import ...

  5. 腾讯云发送短信验证码

    腾讯云短信服务下载源码 maven配置 <dependency><groupId>com.github.qcloudsms</groupId><artifac ...

  6. 短信宝、腾讯云 发送短信验证码

    安装composer扩展包 composer require mrwanghongda/sms-sdk 使用 use App\Factory\SmsFactory;/*** 短信宝*/const SM ...

  7. 【短信发送】实现腾讯云发送短信功能--工具类和SpringBoot配置两种方法实现

    实现腾讯云发送短信功能--工具类和SpringBoot配置两种方法实现 一.开通腾讯云短信服务 二.工具类--使用qcloudsms实现短信发送 三.Spring Boot项目引入短信发送功能 我们发 ...

  8. python调用qq发送短信_使用腾讯云发送短信

    使用腾讯云发送短信 第一次注册使用白送你200条,美滋滋 在腾讯云平台申请 点点点操作就完事了,要想发送短信,必须要申请签名和模板.签名类型可以使用网站,公众号,小程序,app 填好信息,上传指定的截 ...

  9. qpython3h手机版怎么发短信_python如何使用腾讯云发送短信

    腾讯云方面的申请和流程都比较简单,基本都是可视化操作的,这里就不在赘述了.这篇文章着重讲解怎么用python实现调用. 我假设你已经满足了以下几个前提 + 已经开通了腾讯云短信业务 + 创建好了短信签 ...

  10. python项目对接腾讯云发送短信

    python项目对接腾讯云发送短信 先安装需要的包 pip install tencentcloud-sdk-python # -*- coding: utf-8 -*- # pip install ...

最新文章

  1. 从0开始学习GitHub系列之「认识并加入GitHub」
  2. 微软一站式示例代码库 7月新代码示例发布
  3. 计算机应用主要学PS,全国计算机一级Photoshop应用试题及答案
  4. commons-lang3:ArrayUtils
  5. mysql-plus多数据库_Springboot+mybatisplus+mysql配置多数据源(注解版)
  6. python代码200行左右_200行Python代码实现2048
  7. poj3237 Tree
  8. linux账号和权限管理思维导图,Linux系统支持的思维导图软件有哪些?
  9. linux先安装svn server
  10. 利用PowerDesigner15在win7系统下对MySQL 进行反向project(二)
  11. 手把手教你强化学习 (八) 强化学习中的值函数近似算法
  12. 拓端tecdat|R语言社区主题检测算法应用案例
  13. 8月5日发布卡巴斯基授权许可key-卡巴斯基key
  14. 知识图谱构建流程及算法
  15. charles安装免费版
  16. 微信表情包的制作以及50*50像素图片太模糊的处理方法
  17. 挂靠其入职公司股东名下其他公司,是否有违反竞业协议?
  18. PHP的exec()函数用法详解
  19. android应用市场 更新,当贝市场(com.dangbeimarket) - 4.2.9 - 应用 - 酷安
  20. 荣耀magic5pro参数配置

热门文章

  1. MPC-HC/MPC-BE/LAV Filter等播放器相关
  2. 实验三 vi编辑器(Linux基础教程)
  3. iOS第三方库-魔窗Mlink的坑
  4. arcmap10.7打开tif文件一片空白 | 解决方法
  5. hive生产实践问题(一)在使用Hive Client跑job时,一直提示job被kill,
  6. vue3 动态获取屏幕尺寸
  7. 机器学习:数学加强(二)——条件概率、贝叶斯公式、常见分布、协方差、相关系数、切比雪夫不等式、大数定律
  8. html中图片椭圆,CSS3技巧之形状(椭圆)
  9. 企业级需求管理工具选型报告(2020年3月20日)
  10. Vulkan Samples 阅读 -- Basics(四): Texture Arrays Cube Map Textures 3D Textures