移動端獲取短信驗證碼java實現——阿里雲短信服務

需求:移動端輸入手機號,獲取驗證碼。點擊登陸,驗證驗證碼是否輸入錯誤、是否超時等狀況,一旦校驗經過,將用戶數據保存到數據中(業務邏輯)。html

前提:註冊阿里用戶,開通短信服務,申請key、祕鑰、簽名、短信模板。參考:https://help.aliyun.com/document_detail/55284.html?spm=a2c4g.11174283.3.2.8d482c42qxWRYAjava

第一部分:準備工做android

一、pom.xml文件添加阿里雲短信服務的依賴web

<!--阿里雲短信發送start-->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.0.6</version>
    </dependency>
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
      <version>1.1.0</version>
    </dependency>

    <!--阿里雲短信發送end-->

此處可能會報錯,官網也說了,好像這個版本的不是很穩定吧。異常多是:spring

Java.lang.NoClassDefFoundError:com/aliyuncs/exceptions/ClientException

  解決:數據庫

(1)方法一:檢查lib文件下是否有aliyun-java-sdk-core包,沒有的話就手動先添加上吧。apache

(2)方法二:將版本號改成3.3.1,即低版本json

<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>3.3.1</version>
    </dependency>

二、下載官方demo,這個是發送短信的工具。api

package com.alicom.dysms.api; 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.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; /** * Created on 17/6/7. * 短信API產品的DEMO程序,工程中包含了一個SmsDemo類,直接經過 * 執行main函數便可體驗短信產品API功能(只須要將AK替換成開通了雲通訊-短信產品功能的AK便可) * 工程依賴了2個jar包(存放在工程的libs目錄下) * 1:aliyun-java-sdk-core.jar * 2:aliyun-java-sdk-dysmsapi.jar * * 備註:Demo工程編碼採用UTF-8 * 國際短信發送請勿參照此DEMO */ public class SmsDemo { //產品名稱:雲通訊短信API產品,開發者無需替換 static final String product = "Dysmsapi"; //產品域名,開發者無需替換 static final String domain = "dysmsapi.aliyuncs.com"; // TODO 此處須要替換成開發者本身的AK(在阿里雲訪問控制檯尋找) static final String accessKeyId = "yourAccessKeyId"; static final String accessKeySecret = "yourAccessKeySecret"; public static SendSmsResponse sendSms() 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("15000000000"); //必填:短信簽名-可在短信控制檯中找到 request.setSignName("雲通訊"); //必填:短信模板-可在短信控制檯中找到 request.setTemplateCode("SMS_1000000"); //可選:模板中的變量替換JSON串,如模板內容爲"親愛的${name},您的驗證碼爲${code}"時,此處的值爲 request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}"); //選填-上行短信擴展碼(無特殊需求用戶請忽略此字段) //request.setSmsUpExtendCode("90997"); //可選:outId爲提供給業務方擴展字段,最終在短信回執消息中將此值帶回給調用者 request.setOutId("yourOutId"); //hint 此處可能會拋出異常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; } public static QuerySendDetailsResponse querySendDetails(String bizId) 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("15000000000"); //可選-流水號  request.setBizId(bizId); //必填-發送日期 支持30天內記錄查詢,格式yyyyMMdd SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd"); request.setSendDate(ft.format(new Date())); //必填-頁大小 request.setPageSize(10L); //必填-當前頁碼從1開始計數 request.setCurrentPage(1L); //hint 此處可能會拋出異常,注意catch QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request); return querySendDetailsResponse; } public static void main(String[] args) throws ClientException, InterruptedException { //發短信 SendSmsResponse response = sendSms(); 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()); System.out.println("短信明細查詢接口返回數據----------------"); System.out.println("Code=" + querySendDetailsResponse.getCode()); System.out.println("Message=" + querySendDetailsResponse.getMessage()); int i = 0; for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs()) { System.out.println("SmsSendDetailDTO["+i+"]:"); System.out.println("Content=" + smsSendDetailDTO.getContent()); 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()); } } }

第二部分:建立本身的MySessionContext安全

注意:因爲移動端沒有sessionId,咱們須要本身傳輸sessionId獲取session值。但出於安全性考慮,jdk如今去掉了經過sessionId獲取session的方法。

解決:自建工具類,經過sessionId獲取session。

一、建立一個MySessionContext.java類

public class MySessionContext { private static MySessionContext instance; private HashMap<String,HttpSession> sessionMap; private MySessionContext() { sessionMap = new HashMap<String,HttpSession>(); } public static MySessionContext getInstance() { if (instance == null) { instance = new MySessionContext(); } return instance; } public synchronized void addSession(HttpSession session) { if (session != null) { sessionMap.put(session.getId(), session); } } public synchronized void delSession(HttpSession session) { if (session != null) { sessionMap.remove(session.getId()); } } public synchronized HttpSession getSession(String sessionID) { if (sessionID == null) { return null; } return sessionMap.get(sessionID); } } 

二、而後創建session監聽,要實現HttpSessionListener接口

public class SessionListener implements HttpSessionListener { private MySessionContext myc = MySessionContext.getInstance(); public void sessionCreated(HttpSessionEvent httpSessionEvent) { HttpSession session = httpSessionEvent.getSession(); myc.addSession(session); } public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { HttpSession session = httpSessionEvent.getSession(); myc.delSession(session); } } 

三、接着,在web.xml中配置session監聽器

<listener>  
        <listener-class>com.chinapost.manager.utils.SessionListener</listener-class>  
    </listener>

參考地址:http://www.javashuo.com/article/p-xplucsym-mo.html

第三部分:實現業務

一、controller層

/**
   * app登陸,附加:驗證碼時效性
   * @param conditionStr
   * @return
   */ @RequestMapping(value = "/anLogin", method = RequestMethod.POST) @ResponseBody public ResponseModel anLogin(@RequestBody String conditionStr){ return androidLoginService.anLogin(conditionStr); } /** * 點擊發送驗證碼 * @param conditionStr * @return */ @RequestMapping(value = "/getVerificationCode", method = RequestMethod.POST) @ResponseBody public ResponseModel getVerificationCode(@RequestBody String conditionStr) { return androidLoginService.getVerificationCode(conditionStr); }

二、service實現層

package com.test.service.androidService.impl; import com.alibaba.fastjson.JSON; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.test.dao.CarDriverMapper; import com.test.entity.business.CarDriver; import com.test.entity.common. ResponseModel; import com.test.service.androidService.AndroidLoginService; import com.test.service.common.impl.BaseServiceImpl; import com.test.util.MySessionContext; import com.test.util.SessionUtil; import com.test.util.SmsCodeUtil; import com.test.util.SmsSendUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class AndroidLoginServiceImpl extends BaseServiceImpl implements AndroidLoginService { public static final Logger logger = LoggerFactory.getLogger(AndroidLoginServiceImpl.class); @Resource private CarDriverMapper carDriverMapper; @Override public ResponseModel anLogin(String conditionStr) { set ResponseModel(new ResponseModel()); setErrorMsg(""); setSuccess(false); Map<String,Object> resultMap = new HashMap<>(); try { CarDriver carDriver = JSON.parseObject(conditionStr, CarDriver.class); String tel_param = carDriver.getTel(); String verificationCode_param = carDriver.getVerificationCode(); String sessionId_param = carDriver.getSessionId(); //1-校驗手機號和驗證碼不能爲空
      if(StringUtils.isBlank(tel_param)){ setErrorMsg("手機號不能爲空!"); return get ResponseModel(); } if(StringUtils.isBlank(verificationCode_param)){ setErrorMsg("驗證碼不能爲空!"); return get ResponseModel(); } //2-校驗是否獲取驗證碼
      MySessionContext myc= MySessionContext.getInstance(); HttpSession session = myc.getSession(sessionId_param); //獲取session中登陸手機號對應的驗證碼
      Object verificationCode = session.getAttribute(tel_param+"driverAppLogin"); if(verificationCode==null){ setErrorMsg("請先獲取驗證碼!"); return get ResponseModel(); } //3-校驗驗證碼是否相等
      if(verificationCode_param.equals(verificationCode)){ //4-校驗驗證碼是否過時(1*60s)
        Object firstTime = session.getAttribute(tel_param+"driverAppLoginTime"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); boolean bool = overtimeCode(firstTime.toString(),sdf.format(new Date()),1*60); //bool等於true,則超時
        if(!bool){ //5-業務邏輯 //5-1 校驗手機號在數據庫中是否存在
          Integer id = this.carDriverMapper.selectByTel(carDriver); if(id != null){ carDriver = this.carDriverMapper.selectByPrimaryKey(id); resultMap.put("name",carDriver.getName()); } else{ this.carDriverMapper.insertSelective(carDriver); if(carDriver.getId() == null){ setErrorMsg("新增駕駛員帳號失敗!"); throw new Exception(getErrorMsg()); } } resultMap.put("id",carDriver.getId()); resultMap.put("tel",carDriver.getTel()); resultMap.put("level",carDriver.getLevel()); setSuccess(true); }else{ setErrorMsg("驗證碼獲取超時,請從新獲取!"); return get ResponseModel(); } }else{ setErrorMsg("驗證碼輸入不正確!"); return get ResponseModel(); } }catch (Exception e){ e.printStackTrace(); logger.error(e.getMessage()); }finally { get ResponseModel().setData(resultMap); get ResponseModel().setSuccess(isSuccess()); get ResponseModel().setErrorMsg(getErrorMsg()); } return get ResponseModel(); } @Override public ResponseModel getVerificationCode(String conditionStr) { set ResponseModel(new ResponseModel()); setErrorMsg(""); setSuccess(false); CarDriver carDriver = null; try { carDriver = JSON.parseObject(conditionStr, CarDriver.class); //1-校驗手機號不能爲空
      String tel = carDriver.getTel(); if(StringUtils.isBlank(tel)){ setErrorMsg("手機號不能爲空!"); return get ResponseModel(); } //2-發送短信驗證碼 //生成隨機碼
      String code = SmsCodeUtil.getRandom()+""; //發送驗證碼
      SendSmsResponse sendSmsResponse  = SmsSendUtil.sendSms(carDriver.getTel(),"app",code); //若是發送成功則保存驗證碼和時間到session中
      if(sendSmsResponse.getCode()!=null && sendSmsResponse.getCode().equals("OK")){ HttpSession session = SessionUtil.getSession(); session.setAttribute(tel+"driverAppLogin", code); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); session.setAttribute(tel+"driverAppLoginTime", sdf.format(new Date())); //獲取sessionid
        String sessionId = session.getId(); carDriver.setSessionId(sessionId); //設置session最長存在時間60分鐘 //SessionUtil.getSession().setMaxInactiveInterval(60*60);
        setSuccess(true); } else{ setErrorMsg(sendSmsResponse.getMessage()); return get ResponseModel(); } } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } finally { get ResponseModel().setData(carDriver); get ResponseModel().setSuccess(isSuccess()); get ResponseModel().setErrorMsg(getErrorMsg()); } return get ResponseModel(); } /** * 短信驗證碼有效期,若是true則過時了 * @param firstTime * @param lastTime * @param overtime * @return */
  private static boolean overtimeCode(String firstTime,String lastTime,long overtime){ boolean flag=false; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date one; Date two; try { one = df.parse(firstTime); two = df.parse(lastTime); long time1 = one.getTime(); long time2 = two.getTime(); long diff = time2 - time1; if(diff/1000>overtime){ flag=true; } } catch (ParseException e) { e.printStackTrace(); } return flag; } }

 附加:獲取隨機碼工具類(我是投機取巧,拷貝騰訊雲短信服務的demo中的)

package com.test.util; import java.lang.StringBuffer; import java.util.Random; import java.util.ArrayList; import org.apache.commons.codec.digest.DigestUtils; public class SmsCodeUtil { public static boolean isNotEmpty(String s) { if (s == null || s.isEmpty()){ return false; } return true; } public static long getCurrentTime() { return System.currentTimeMillis() / 1000; } public static long getRandom() { return (new Random(SmsCodeUtil.getCurrentTime())).nextInt(900000) + 100000; } public static String calculateSignature(String appkey, long random, long time, String phoneNumber) { StringBuffer buffer = new StringBuffer("appkey=") .append(appkey) .append("&random=") .append(random) .append("&time=") .append(time) .append("&mobile=") .append(phoneNumber); return sha256(buffer.toString()); } public static String calculateSignature(String appkey, long random, long time, String[] phoneNumbers) { StringBuffer buffer = new StringBuffer("appkey=") .append(appkey) .append("&random=") .append(random) .append("&time=") .append(time) .append("&mobile="); if (phoneNumbers.length > 0) { buffer.append(phoneNumbers[0]); for (int i = 1; i < phoneNumbers.length; i++) { buffer.append(","); buffer.append(phoneNumbers[i]); } } return sha256(buffer.toString()); } public static String calculateSignature(String appkey, long random, long time, ArrayList<String> phoneNumbers) { return calculateSignature(appkey, random, time, phoneNumbers.toArray(new String[0])); } public static String calculateSignature(String appkey, long random, long time) { StringBuffer buffer = new StringBuffer("appkey=") .append(appkey) .append("&random=") .append(random) .append("&time=") .append(time); return sha256(buffer.toString()); } public static String calculateFStatusSignature(String appkey, long random, long time, String fid) { StringBuffer buffer = new StringBuffer("appkey=") .append(appkey) .append("&random=") .append(random) .append("&time=") .append(time) .append("&fid=") .append(fid); return sha256(buffer.toString()); } public static String calculateAuth(String appkey, long random, long time, String fileSha1Sum) { StringBuffer buffer = new StringBuffer("appkey=") .append(appkey) .append("&random=") .append(random) .append("&time=") .append(time) .append("&content-sha1=") .append(fileSha1Sum); return sha256(buffer.toString()); } public static String sha1sum(String rawString) { return DigestUtils.sha1Hex(rawString); } public static String sha1sum(byte[] bytes) { return DigestUtils.sha1Hex(bytes); } public static String sha256(String rawString) { return DigestUtils.sha256Hex(rawString); } }

 SessionUtil.java

package com.test.util; import com.test.enums.SysConstants; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * Created by test on 2017/9/21. * */ public class SessionUtil { /** * 清空session */ public static void cleanSession(){ Enumeration em = getSession().getAttributeNames(); while(em.hasMoreElements()){ getSession().removeAttribute(em.nextElement().toString()); } } /** * 獲取上一個訪問鏈接 */ public static String getBefUrl(){ Enumeration e = getRequest().getHeaders("Referer"); String befUrl; if(e.hasMoreElements()){ befUrl = (String)e.nextElement(); }else{ befUrl = ""; } return befUrl; } /** * 獲取request */ @SuppressWarnings("UnnecessaryLocalVariable") public static HttpServletRequest getRequest() { ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); return request; } /** * 獲取response */ @SuppressWarnings("UnnecessaryLocalVariable") public static HttpServletResponse getResponse() { HttpServletResponse httpServletResponse = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); return httpServletResponse; } /** * 獲取session */ public static HttpSession getSession() { return getRequest().getSession(); } /** * 驗證是否重複提交 */ public static boolean checkSubmitRepeat(String jspToken) { String sessionToken = (String) getSession().getAttribute(SysConstants.SUBMITTOKEN.getName()); return StringUtils.isBlank(jspToken) || StringUtils.isBlank(sessionToken)||!sessionToken.equals(jspToken); } }

 完畢!

相關文章
相關標籤/搜索