一、首先需要去阿里云注册账号并登录

二、注册阿里云的短信服务 会有100条免费短信 获取自己的AccessKey(accessKeyId,accessKeySecret)

三、在阿里云短信服务 注册短信的 签名  和 模板

阿里云短信服务参考地址:https://www.aliyun.com/ss/?k=%E7%9F%AD%E4%BF%A1%E6%9C%8D%E5%8A%A1&accounttraceid=b95a6b72bbdb45f6942490006a7dd4ebvvkl

maven

      <dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.0.8</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>1.1.0</version></dependency>

以下为实现的代码demo 框架为SSM

NoteController

package com.yannis.controller;import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
import com.yannis.json.AjaxJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** @Author: ysw* @Date: 2020/9/7 10:59*/
@Controller
@RequestMapping("/note")
public class NoteController {//产品名称:云通信短信API产品,开发者无需替换static final String product = "Dysmsapi";//产品域名,开发者无需替换static final String domain = "dysmsapi.aliyuncs.com";// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)static final String accessKeyId = "";static final String accessKeySecret = "";@RequestMapping("adduser")public String adduser(HttpServletRequest request,HttpServletResponse response){request.setAttribute("view","1");return "/adduser";}@RequestMapping("/sendNote")@ResponseBodypublic AjaxJson sendNote(String phone, HttpServletRequest res, HttpServletResponse req){AjaxJson j =new AjaxJson();LinkedHashMap<String,Object> hashMap = new LinkedHashMap<>();//发短信try {SendSmsResponse   response = sendSms(phone,"","");System.out.println("短信接口返回的数据----------------");System.out.println("Code=" + response.getCode());System.out.println("Message=" + response.getMessage());System.out.println("RequestId=" + response.getRequestId());System.out.println("BizId=" + response.getBizId());Thread.sleep(3000L);if(response.getCode() != null && response.getCode().equals("OK")) {QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId(),phone);System.out.println("短信明细查询接口返回数据----------------");System.out.println("Code=" + querySendDetailsResponse.getCode());System.out.println("Message=" + querySendDetailsResponse.getMessage());int i = 0;String content="";for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs()) {System.out.println("SmsSendDetailDTO["+i+"]:");content = smsSendDetailDTO.getContent();System.out.println("Content=" + content);System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());System.out.println("OutId=" + smsSendDetailDTO.getOutId());System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());}System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());String yzmFromSms = getYzmFromSms(content);HttpSession session = res.getSession();session.setAttribute("code",yzmFromSms);session.setAttribute("phone",phone);session.setAttribute("msg","验证码发送成功");//定时5分钟删除验证码removeAttrbute(session, "code");removeAttrbute(session, "phone");removeAttrbute(session, "msg");j.setSuccess(true);}} catch (ClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}return j;}/*** phone 手机号* singName 申请的签名* templateCode  模板code* *///发送短信public static SendSmsResponse sendSms(String phone,String singName,String templateCode) throws ClientException {//可自助调整超时时间System.setProperty("sun.net.client.defaultConnectTimeout", "10000");System.setProperty("sun.net.client.defaultReadTimeout", "10000");//初始化acsClient,暂不支持region化IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);IAcsClient acsClient = new DefaultAcsClient(profile);//组装请求对象-具体描述见控制台-文档部分内容SendSmsRequest request = new SendSmsRequest();//必填:待发送手机号request.setPhoneNumbers(phone);         //必填:短信签名-可在短信控制台中找到request.setSignName(singName);          //必填:短信模板-可在短信控制台中找到request.setTemplateCode(templateCode);  //可选:模板中的变量替换JSON串,如模板内容为"${code}"时,此处的值为request.setTemplateParam("{'code': '"+randomSix()+"'}");//选填-上行短信扩展码(无特殊需求用户请忽略此字段)//request.setSmsUpExtendCode("验证码");//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者request.setOutId("yourOutId");//hint 此处可能会抛出异常,注意catchSendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);return sendSmsResponse;}//查询发送短信的信息public static QuerySendDetailsResponse querySendDetails(String bizId,String phone) throws ClientException {//可自助调整超时时间System.setProperty("sun.net.client.defaultConnectTimeout", "10000");System.setProperty("sun.net.client.defaultReadTimeout", "10000");//初始化acsClient,暂不支持region化IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);IAcsClient acsClient = new DefaultAcsClient(profile);//组装请求对象QuerySendDetailsRequest request = new QuerySendDetailsRequest();//必填-号码request.setPhoneNumber(phone);//可选-流水号request.setBizId(bizId);//必填-发送日期 支持30天内记录查询,格式yyyyMMddSimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");request.setSendDate(ft.format(new Date()));//必填-页大小request.setPageSize(10L);//必填-当前页码从1开始计数request.setCurrentPage(1L);//hint 此处可能会抛出异常,注意catchQuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);return querySendDetailsResponse;}//生成6位验证码public static int  randomSix(){//随机生成六位随机数StringBuffer stringBuffer=new StringBuffer();for (int x=0;x<=5;x++) {int random = (int) (Math.random() * (10 - 1));stringBuffer.append(random);}String string = stringBuffer.toString();System.out.println("生成的六位验证码  :"+Integer.parseInt(string));return Integer.parseInt(string);}//提取6位验证码private String getYzmFromSms(String smsBody) {Pattern pattern = Pattern.compile("\\d{6}");Matcher matcher = pattern.matcher(smsBody);if (matcher.find()) {return matcher.group();}return null;}/*** 设置5分钟后删除session中的验证码* @param session* @param attrName*/private void removeAttrbute(final HttpSession session, final String attrName) {final Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {// 删除session中存的验证码session.removeAttribute(attrName);timer.cancel();}}, 5 * 60 * 1000);}}

adduser.jsp 登录页面  jquery 换成自己的路径

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>手机号登录</title><style type="text/css">
.main {/*设置外边距,下面有30px距离*/margin: 0px 0px 30px 0px;/*背景色*/background-color: #ffffff;/*字体大小*/font-size: 12px;/*文字颜色*/color: #000000;/*文本对齐方式*/text-align: center;/*垂直对齐方式*/vertical-align: top;/*设置行高*/line-height: 150%;font-family: Arial, Helvetica, sans-serif;
}
/*设置a标签 字体12px 颜色 #0066FF*/
a {font-size: 12px;color: #0066FF; /*#1E33F7*/
}
/*设置未访问的超连接无下划线*/
a:link {text-decoration: none;
}
/*设置访问后的连接颜色为#0066ff 也无下划线*/
a:visited {text-decoration: none;color: #0066FF;
}
/*设置鼠标移到超连接上无下划线颜色为#990099*/
a:hover {text-decoration: none;color: #990099; /*颜色变换*/
}
/*设置选定的超连接无下划线,颜色为#0066ff*/
a:active {text-decoration: none;color: #0066FF;
}/*设置所有td标签*/
td {text-align: left;vertical-align: top;font-size: 12px;
}
/*设置所有的hr标签*/
hr {/*颜色*/color: #cccccc;/*高度*/height: 1px;/*上外边距5px*/margin-top: 5px;/*下外边距5px*/margin-bottom: 5px;/*分隔线如果超出框架部分隐藏*/overflow: hidden;
}
/*设置h1*/
h1 {font-size: 14px;color: #000000;margin-bottom: 5px;font-weight: bold;display: inline;text-align: center;
}#head {/*设置head这个div宽度为100%*/width: 100%;
}.headtab {width: 100%;
}.headta td {/*设置td内边距上10px 右50px 下25px 左135px*/padding: 10px 50px 25px 135px;
}#menu {width: 100%;background-color: #1c3f09; /*设置背景色*/border-top-width: 4px; /*设置上边框宽度4px*/border-top-style: solid; /*设置上边框样式 实线*/border-top-color: #82b211; /*设置上边框的颜色 #82b211*/text-align: center;font-size: 14px;padding: 10px 0px 10px 0px;
}#menu td {text-align: center;
}#menu a {font-size: 14px;color: #ffffff; /*超连接文字颜色*/font-weight: bold; /*超连接字体加粗*/padding: 10px 10px 10px 10px; /*超连接内边距*/
}#menu a:link {text-decoration: none;font-weight: bold;
}#menu a:visited {text-decoration: none;color: #FFFFFF;font-weight: bold;
}#menu a:hover {text-decoration: none;color: #999999; /*颜色变换*/font-weight: bold;
}#menu a:active {text-decoration: none;color: #FFFFFF;font-weight: bold;
}#search {width: 100%;text-align: center;color: #ffffff;font-weight: bold;padding: 5px 0px 5px 0px;background-color: #b6b684;
}.inputtxt {width: 130px;height: 18px;border: 1px solid #999999;
}
/*注册页面content部分整体设计*/
#content {width: 900px;background-color: #fcfdef;border: 1px solid #eeeddb;margin-right: auto;margin-left: auto;
}
/*设置边框*/
.upline {border-bottom-width: 1px;border-bottom-style: dashed;border-bottom-color: #b0bec7;padding-top: 5px;padding-bottom: 5px;margin: 20px;
}
/*设置文本框样式*/
.txtinput {margin-left: 20px;font-size: 15px;width: 200px;height: 25px;border: 1px solid #a4b4bd;
}
/*设置文本域样式*/
.txtarea {margin-left: 20px;font-size: 15px;width: 350px;height: 75px;border: 1px solid #a4b4bd;
}#foot {width: 100%;background-color: #efeedc;margin-top: 15px;
}
</style><script src="../js/jquery.min.js"></script></head>
<body class="main"><div id="content"><form  id="myForm" method="post" onsubmit="return reg(this);"><table  width="850px" border="0" cellspacing="4"><tr><td style="padding: 30px"><h1>手机号登录</h1><table width="100%" border="0" cellspacing="2" class="upline"><tr><td width="30%" style="text-align: right;"><font size="2px">手机号</font></td><td width="30%"><input type="text" name="phone"  id ="phone" class="txtinput" value="${phone}">  </td><td width="40%"><font color="#999999">* 请填写您的手机号&nbsp;&nbsp;&nbsp;                         <span id="tise"></span></font></td>              </tr><tr><td style="text-align: right;"><font size="2px">验证码</font></td><td><input type="text" name="code" class="txtinput" id="code" placeholder="请输短信验证码"/></td><td><input id="btn" type="button" onclick="noteTime(this)" value="免费获取验证码"><span id ="error_msg">${msg}</span></td></tr> <%--<tr>--%><%--<td style="text-align: right;"><font size="2px">密码</font></td>--%><%--<td><input type="password" name="password" class="txtinput" id="password" onchange="checkpwd()"/></td>--%><%--<td><font color="#999999">* 密码必须6-18位</font></td>--%><%--</tr>--%><%--<tr>--%><%--<td style="text-align: right;"><font size="2px">重复密码</font></td>--%><%--<td><input type="password" name="repassword"--%><%--class="txtinput"></td>--%><%--<td><font color="#999999">* 两次密码需要一致</font></td>--%><%--<td>&nbsp;</td>--%><%--</tr>      --%><%----%></table><table width="100%" border="0" cellspacing="4"><tr><td width="51%" style="text-align:right"><input id="sub" type="button" value="确定"><FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;</FONT><td width="49%" >  <INPUT type="button" onclick="history.go(-1)" value="返回" /> <spanid="Label1"> </span></td></tr>       </table></form></div><script>$(function () {var view = "${view}";if (view == "1"){var obj = $("#btn");settime(obj);}});function reg(form) {if (form.phone.value == "") {alert("用户不能为空!");return false;} else if (form.password.value == "") {alert("密码不能为空!");return false;} else if (form.repassword.value == "") {alert("确认密码不能为空!");return false;}else if (form.code.value == "") {alert("验证码不能为空!");return false;}  else if (form.repassword.value != form.password.value ) {alert("两次密码不相同!");return false;} else if(!checkpwd()){return false;} else if(!confirm('注册信息确认提交?')){return false;}}//验证密码位数function checkpwd() {var check;var reg = /[^A-Za-z0-9_]+/;var regs = /^[a-zA-Z0-9_\u4e00-\u9fa5] + $ /;var password = document.getElementById("password").value;if (password.length < 6 || password.length > 18 || regs.test(password)) {alert("密码输入不合法,请重新输入!");document.getElementById("password").focus();check = false;} else {check = true;}return check;}</script><script>//定时60秒重新发送var countdown=60;function noteTime(){var phone = $("#phone").val();if (phone!=""){$.ajax({type:'post',url:"${pageContext.request.contextPath}/note/sendNote?phone="+phone,dataType: "json",success: function(data){window.location.href = "${pageContext.request.contextPath}/note/adduser";}});}else{alert("请输入手机号");}}function settime(obj) { //发送验证码倒计时if (countdown == 0) {obj.attr('disabled',false);obj.val("免费获取验证码");countdown = 60;return;} else {obj.attr('disabled',true);obj.val("重新发送(" + countdown + ")");countdown--;}//定时setTimeout(function() {settime(obj)},1000);}</script><script>$("#sub").click(function () {var code = "${code}";var codes = $("#code").val();if (codes != ""){if (code == codes){alert("验证成功");alert(code+"  " +codes);window.location.href = "../index.jsp";}else{alert("验证失败");}} else{alert("请输入验证码");}<%--$.ajax({--%><%--type:'post',--%><%--url:"${pageContext.request.contextPath}/note/selectNote"--%><%--});--%>//$("#myForm").submit();});
</script>
</body>
</html>

index.jsp 模拟成功页面

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<h2>手机登录成功</h2></body>
</html>

程序页面

利用支付宝短信服务接口 实现手机号 验证码登录Demo相关推荐

  1. 用户注册调用短信服务接口实现手机号注册

    一般现在平台的用户在注册时都是使用手机号注册,所以就需要用到手机介绍短信验证码实现注册,在这里我调用的是榛子云短信平台第三方提供的短信服务发送验证码. 短信验证码实现流程 1.构造手机验证码,生成一个 ...

  2. C/C++开发,阿里云短信服务接口的c++实现

    目录 一.阿里云短信服务接口 二.逻辑代码实现 三.编译测试效果 一.阿里云短信服务接口 最近在调用阿里云短信服务接口发现并没有c++的资料,网上查询发现也是零星描述,由于自身项目基础累积了C++的太 ...

  3. java对接阿里云短信服务详解(验证码,推广短信,通知短信)

    前言 小前提: - java:springboot框架,maven版本管理. - 阿里云:有账号,已经进行实名认证. java对接阿里云短信服务详解(验证码,推广短信,通知短信) 前言 1. 登录阿里 ...

  4. JAVA 短信API接口调用 附 文档 Demo

    JAVA 短信API接口调用 附 文档 Demo 1.请求地址 http://host:port/sms 请求方式可以 POST 和 GET方式,建议采用POST方式 2.参数说明 参数需要 URLE ...

  5. 创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全

    创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全 创宇滤镜|API防刷|短信邮件接口防刷|验证码防刷|搜索防刷 - 知道创宇云安全 posted on 2017-02- ...

  6. 阿里云短信服务接口返回: 只能向已回复授权信息的手机号发送

    项目场景: 在进入阿里云短信服务时,调用 OpenAPI-发送短信接口 , 返回异常情况及其解决. 问题描述 在进入阿里云短信服务时, 调用测试签名和测试短信模板后, 返回只能向已回复授权信息的手机号 ...

  7. 最新阿里云短信服务接口类【亲测成功】

    阿里大于 最新的阿里云短信接口,适用于阿里大于搬家以后的情况. 之前一直用阿里大于的短信接口,最近上项目时发现阿里大于悄悄地搬家到了阿里云!阿里云的SDK文件繁多,看得一头雾水!下面代码是最新的可适用 ...

  8. 关于调用阿里云短信服务接口实现短信验证码的过程

    为了实现给手机发送短信来进行验证码验证,所以需要调用此接口来进行测试 登陆阿里云 首先需要有阿里云的账号,进行注册申请,进去控制台,然后找到短信服务 创建签名和模板 选中国内消息,然后看到右边有签名管 ...

  9. 怎么样可以调用阿里云短信服务接口实现短信验证码

    为了实现给手机发送短信来进行验证码验证,所以需要调用此接口来进行测试 登陆阿里云 首先需要有阿里云的账号,进行注册申请,进去控制台,然后找到短信服务 创建签名和模板 选中国内消息,然后看到右边有签名管 ...

  10. 调用阿里云短信服务接口实现短信验证码

    为了实现给手机发送短信来进行验证码验证,所以需要调用此接口来进行测试 登陆阿里云 首先需要有阿里云的账号,进行注册申请,进去控制台,然后找到短信服务 创建签名和模板 选中国内消息,然后看到右边有签名管 ...

最新文章

  1. 深度学习4:使用MNIST数据集(tensorflow)
  2. python乘法口诀编程-中年大叔学编程-用Python打印个九九乘法表
  3. Html.BeginForm 与Section、Partial View 和 Child Action
  4. 小人大作战v0.02原型(单机)发布
  5. 谷歌又放大招:视觉效果完胜其他SOTA的风格迁移网络,手机端可达实时4K
  6. 4.6 Kaggle房价预测
  7. Spring5的通信报文
  8. 48个越吃越瘦的诀窍 - 生活至上,美容至尚!
  9. 三维视觉前沿进展年度报告
  10. DEAP2.1软件与Malmquist指数操作说明
  11. 微信模拟器不显示鼠标解决办法
  12. java 机器学习库_7个最好的Java机器学习开发库
  13. dev chart 绘制图形
  14. 二维码在线生成 扫描二维码打电话 扫码拨号
  15. 历史经验之QT在WIN32下编译环境配置步骤
  16. 将MATLAB任务栏变成白色的图标恢复
  17. 数据库基础_关系与关系模式
  18. Html5游戏封包,学习JavaScript-10-基本封包类型
  19. RRT算法(快速拓展随机树)的Python实现
  20. unicode 生僻字_[微软拼音小技巧] 如何用Unicode输入生僻字

热门文章

  1. unapp Error: Unbalanced delimiter found in string
  2. 易中天品汉代风云人物06:韩信身世之谜
  3. 实践:Linux上安装nginx后同一服务器进行多域名反向代理
  4. Windows配置域名
  5. 人生哲理看完醍醐灌顶茅塞顿开(大部分技术都不懂所以爬不上去)
  6. Android studio Android源码开发环境搭建
  7. Python--邮件处理
  8. 作为IT售前专家,如何真正调研清楚项目需求?(第二篇)
  9. 1012: 求绝对值 C语言
  10. 北漂真的是你的归宿吗?