一、要找一个提供短信接口的第三方平台,这里我使用的是榛子云
二、在注册后,就可以使用了
三、首先是在pom.xml中添加依赖
<!-- fastjosn --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.4</version> </dependency> <dependency> <groupid>com.zhenzikj</groupid> <artifactid>zhenzisms</artifactid> <version>1.0.2</version> </dependency>
1.验证码发送的controller
package com.foreknow.controller; import com.alibaba.fastjson.jsonobject; import com.foreknow.model.member; import com.foreknow.service.memberservice; import com.zhenzi.sms.zhenzismsclient; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.responsebody; import javax.servlet.http.httpsession; import java.util.random; @controller public class codecontroller { //短信平台相关参数 //这个不用改 private string apiurl = "https://sms_developer.zhenzikj.com"; //榛子云系统上获取 private string appid = "100862"; private string appsecret = "62358d10-bc0e-4152-a52c-578a8debc9b9"; @responsebody @getmapping("/fitness/code") public boolean getcode(@requestparam("memphone") string memphone, httpsession httpsession){ try { jsonobject json = null; //随机生成验证码 string code = string.valueof(new random().nextint(999999)); //将验证码通过榛子云接口发送至手机 zhenzismsclient client = new zhenzismsclient(apiurl, appid, appsecret); string result = client.send(memphone, "您的验证码为:" + code + ",该码有效期为5分钟,该码只能使用一次!"); json = jsonobject.parseobject(result); if (json.getintvalue("code")!=0){//发送短信失败 return false; } //将验证码存到session中,同时存入创建时间 //以json存放,这里使用的是阿里的fastjson json = new jsonobject(); json.put("memphone",memphone); json.put("code",code); json.put("createtime",system.currenttimemillis()); // 将认证码存入session httpsession.setattribute("code",json); return true; } catch (exception e) { e.printstacktrace(); return false; } } }
其中的局部变量是在榛子云的个人中心获取:
登录时从session中获取刚刚发送到手机的验证码对象:
jsonobject usercode = (jsonobject)session.getattribute("code"); //验证码 usercode .get("code"); //手机号 usercode.get("memphone");
前端限制60秒只能获取一次验证码的效果实现:
<div id="model2"> <div class="layui-form-item input-item"> <label for="username">手机号</label> <input type="text" placeholder="请输入手机号" autocomplete="off" id="memphone" name="memphone" class="layui-input"> </div> <div class="layui-form-item input-item"> <label for="username">验证码</label> <input type="text" placeholder="请输入验证码" autocomplete="off" id="code" name="code" maxlength="6" class="layui-input" style="width: 50%;display: inline"> <input type="button" class="layui-btn layui-btn-primary" value="获取验证码" id="sendbtn" style="width:41%;margin-left: 18px;border-color:#1e9fff !important;" onclick="sendcode(this)"></input> </div> </div> function sendcode(){ var memphone = $("#memphone").val(); console.log(memphone.length); if(memphone == '' || memphone.length != 11){ layer.msg("请输入正确的手机号!"); return; }else{ $.ajax({ type: 'get', url: '[[${basepath}]]/fitness/code', data: { memphone : memphone }, datatype: 'json', success: function(data) { if(data){ timer(); }else{ layer.msg("获取验证码失败"); } }, error: function(data) { layer.msg('连接超时!'); }, }); } } var wait = 60; //倒计时 function timer() { if(wait == 0){ $("#sendbtn").val("获取验证码"); $("#sendbtn").removeattr("disabled"); $("#sendbtn").css("border-color","1e9fff").css("background", "#ffffff").css("cursor", "pointer"); wait = 60; }else{ $("#sendbtn").attr("disabled","true"); $("#sendbtn").css("border-color","fbfbfb").css("background", "#ccc").css("cursor", "not-allowed"); $("#sendbtn").val(wait + "秒后重发"); wait--; settimeout(function() {timer()}, 1000); } }
到此这篇关于springboot实现短信验证码登录的文章就介绍到这了,更多相关springboot短信验证码登录内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论