<a name='MsgCodeUtil.java'></a> MsgCodeUtil.java
1package com.hg.util; 2 3import com.soyea.enums.ResultEnum; 4import com.soyea.exception.BizException; 5import org.apache.axis.client.Call; 6import org.apache.axis.client.Service; 7import org.apache.commons.lang3.StringUtils; 8import org.slf4j.Logger; 9import org.slf4j.LoggerFactory; 10import sun.misc.BASE64Encoder; 11 12import javax.servlet.http.HttpSession; 13import javax.xml.namespace.QName; 14import javax.xml.rpc.ServiceException; 15import java.io.UnsupportedEncodingException; 16import java.net.MalformedURLException; 17import java.rmi.RemoteException; 18import java.util.regex.Matcher; 19import java.util.regex.Pattern; 20 21/** 22 * 短信验证码工具 23 */ 24public class MsgCodeUtil { 25 26 /* 27 resultEnum中的参数 28 ERROR_MOBILE(1001002,"请填写正确的手机号码"), 29 MESSAGE_TIME_ERROR(1001003,"60秒内只能发送一次短信,请稍后再试"), 30 MESSAGE_SEND_ERROR(1001004,"发送短信出错,请稍后再试"), 31 MOBILE_NOT_SEND_CODE(1001005,"该手机号未获取验证码,请先获取验证码"), 32 MOBILE_CAN_NOT_CHANGE(1001006,"此手机号与获取验证码的手机号不一致"), 33 MESSAGE_CODE_INVALID(1001007,"此验证码失效,请重新获取验证码"), 34 MESSAGE_CODE_ERROR(1001008,"验证码错误"), 35 36 */ 37 38 private static Logger log = LoggerFactory.getLogger(MsgCodeUtil.class); 39 40 private static final String MSG_START_TIME = "msg_start_time"; //短信发送时间 41 private static final String MOBILE_MSG_MOBILE = "mobile_msg_mobile"; //手机号 42 private static final String MOBILE_MSG_CODE = "mobile_msg_code"; //验证码 43 private static final Integer CODE_VALID_TIME = 5; //验证码有效时间,单位:分钟 /** 44 * 发送验证码 45 * @param msgCode 验证码 46 * @param phone 电话号码 47 * @param session 48 */ 49 public static void sendCode(String msgCode, String phone, HttpSession session) { 50 //获取当前时间 51 long startTime = System.currentTimeMillis(); 52 long startTimefromSessioin = (long) (httpSession.getAttribute(MSG_START_TIME) == null ? 0L : httpSession.getAttribute(MSG_START_TIME)); 53 //验证码获取一次最少60秒 为了防止网络延迟 设置成55 54 if (((startTime - startTimefromSessioin) / 1000) <= (long) 55) { 55 //发送验证码太频繁 56 throw new BizException(ResultEnum.MESSAGE_TIME_ERROR.getMsg()); 57 } 58 //发送短信 59 //todo 封装短信内容 60 String message = "验证码:" + msgCode + ","+CODE_VALID_TIME+"分钟内有效"; 61 //todo 调用第三方接口发送短信 62// Boolean sendSuccess = axisSendMsg(message, phone); 63 Boolean sendSuccess = true; 64 if (sendSuccess) { 65 //短信发送成功,设置属性到session 66 saveCode(session,phone,msgCode); 67 } else { 68 //短信发送失败,请稍后再试 69 throw new BizException(ResultEnum.MESSAGE_SEND_ERROR.getMsg()); 70 } 71 72 } //校验验证码 73 public static boolean validateCode(String mobile, String code, HttpSession session) { 74 //判断该手机号是否发送过验证码 75 String mobileFromSession = (String) session.getAttribute(MOBILE_MSG_MOBILE); 76 if (StringUtils.isBlank(mobileFromSession)) { 77 //未发送过验证码 78 throw new BizException(ResultEnum.MESSAGE_CODE_NO_SEND.getMsg()); 79 } 80 81 //判断手机号是否是发送验证码的手机号 82 if (!mobile.equals(mobileFromSession)) 83 throw new BizException(ResultEnum.MOBILE_CAN_NOT_CHANGE.getMsg()); 84 85 //获取短信发送时间 86 long startTimeFromSession = (long) session.getAttribute(MSG_START_TIME); 87 //判断当前验证码是否失效 88 if (((System.currentTimeMillis() - startTimeFromSession) / 1000) > ((long) CODE_VALID_TIME * 60)) { 89 throw new BizException(ResultEnum.MESSAGE_CODE_ERROR.getMsg()); 90 } 91 92 //判断验证码是否输入正确 93 if (!code.equals((String) session.getAttribute(MOBILE_MSG_CODE))) { 94 throw new BizException(ResultEnum.MESSAGE_CODE_IS_ERROR.getMsg()); 95 } 96 97 //校验通过 98 return true; 99 } /** 100 * 短信发送成功后,保存验证码 101 */ 102 public static void saveCode(HttpSession session,String phone,String msgCode) { 103 session.setAttribute(MSG_START_TIME, System.currentTimeMillis()); 104 session.setAttribute(MOBILE_MSG_MOBILE, phone); 105 session.setAttribute(MOBILE_MSG_CODE, msgCode); 106 } 107 108 /** 109 * 使用验证码后,销毁验证码 110 */ 111 public static void consumeCode(HttpSession session) { 112 session.removeAttribute(MOBILE_MSG_MOBILE); 113 session.removeAttribute(MSG_START_TIME); 114 session.removeAttribute(MOBILE_MSG_CODE); 115 } 116 117 /** 118 * 生成4位数字验证码 119 */ 120 public static String createRandomVcode() { 121 String vcode = ""; 122 for (int i = 0; i < 4; i++) { 123 vcode = vcode + (int) (Math.random() * 9); 124 } 125 return vcode; 126 } /** 127 * 校验手机号是否合法 128 */ 129 public static Boolean isMobile(String number) { 130 boolean flag = false; 131 try { 132 Pattern p = Pattern.compile("^(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$"); 133 Matcher m = p.matcher(number); 134 flag = m.matches(); 135 } catch (Exception e) { 136 flag = false; 137 } 138 return flag; 139 } 140 141}