验证码+ redis

//短信内容发送
private static final String CONTENT_URL = "http://sapi.appsms.cn:8088/msgHttp/json/mt";
private static final String CONTENT_URL_MUL = "http://sapi.appsms.cn:8088/msgHttp/json/multimt";
/**
 * 发送成功计数
 */
private static final String SEND_MOBILE_COUNT_KEY = "LP_APP_CUSTOMER_GET_MOBILE_CODE_COUNT:{}";
/**
 * 验证码Key
 */
private static final String SEND_MOBILE_CODE_KEY = "LP_APP_CUSTOMER_GET_MOBILE_CODE:{}";
 //发送验证码
 public String getPhoneVerificationCode(String phone) {
     Long id = LpCommonWebUtil.getCurrentUserInfo().getId();
     if (StringUtils.isEmpty(phone)) {
         throw new BusinessException(ResponseEnum.INFO.getValue(), "手机号码不能为空", null);
     }
     // 记录发送次数key
     String sendMobileCountKey = StrUtil.format(SEND_MOBILE_COUNT_KEY, DateUtil.today(), id);
     // 校验发送次数
     Object count = redisTemplate.opsForValue().get(sendMobileCountKey);
     if (count != null && Integer.parseInt(count.toString()) >= 10) {
         throw BusinessException.error("验证码发送次数已达上限,请明日再试");
     }
   获取短信验证码
     String smsVerificationCode = RandomUtil.randomNumbers(6);
     if (!"prods".equals(profilesActive)) {
         smsVerificationCode = "111111";
     }
     send("您的验证码为:" + smsVerificationCode + " ,为了您的账户安全,请切勿告知他人,请在页面中输入以完成验证,如非本人操作,请忽略!", phone);
     redisTemplate.opsForValue().set(StrUtil.format(SEND_MOBILE_CODE_KEY, id), smsVerificationCode, 1, TimeUnit.MINUTES);
     redisTemplate.opsForValue().increment(sendMobileCountKey);
     redisTemplate.expire(sendMobileCountKey, 1, TimeUnit.DAYS);
     return smsVerificationCode;
 }
*//**
 * @param content    短信通知内容
 * @param sendMobile 手机号
 * @author Y.linjun
 * @date 2021/8/9 16:55
 *//*
public void send(String content, String... sendMobile) {
    String mobile = "";
    mobile = "15635877540";
    if (profilesActive.equals("prods")) {
        mobile = CollUtil.join(CollUtil.newArrayList(sendMobile), ",");
    } else if (profilesActive.equals("dev")) {
        mobile = "15635877540";
    }
    if (StrUtil.isBlank(mobile)) {
        throw new BusinessException("电话号码不能为空");
    }
    log.info("短信调用开始,手机号:{},内容:{}", mobile, content);
    String resultContent = "";
    //接口账号-待确认是否存数据库维护?
    String account = "caifu106";
    //接口密码-待确认是否存数据库维护?
    String password = "GWLTGq6J";
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    //短信发送地址
    HttpPost httpPost = new HttpPost(CONTENT_URL);
    List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
    long timestamps = System.currentTimeMillis();
    formparams.add(new BasicNameValuePair("account", account));
    //数字签名:(接口密码、手机号、时间戳32位MD5加密生成)
    formparams.add(new BasicNameValuePair("password", getMD532Str(password + mobile + timestamps)));
    //必填 手机号码,逗号隔开 :135123456**,136123456**
    formparams.add(new BasicNameValuePair("mobile", mobile));
    //必填 发送内容
    formparams.add(new BasicNameValuePair("content", content));
    //时间戳,短信发送当前时间毫秒数,生成数字签名用
    formparams.add(new BasicNameValuePair("timestamps", timestamps + ""));

    UrlEncodedFormEntity uefEntity;
    try {
        uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        log.info("短信开始发送");
        httpPost.setEntity(uefEntity);
        response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        //响应格式
        if (entity != null) {
            resultContent = EntityUtils.toString(entity, "UTF-8");
            log.info("第三方短信平台响应结果:{}", resultContent);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //关闭连接,释放资源
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

*//**
 * @param sourceStr 接口密码、手机号、时间戳组合
 * @description MD5 32位加密
 * @author zhusongtao
 * @date 2021/07/16 15:18
 *//*
public static String getMD532Str(String sourceStr) {
    String result = "";
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(sourceStr.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        //result = buf.toString().substring(8, 24);
        result = buf.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return result;
}*/

/**
 * 校验验证码
 *
 * @return
 */
private void checkCode(String codes) {

    Object code = redisTemplate.opsForValue().get(StrUtil.format(SEND_MOBILE_CODE_KEY, LpCommonWebUtil.getCurrentUserInfo().getId()));
    if (code == null) {
        throw BusinessException.error("验证码已过期,请重新获取");
    }
    if (!code.toString().equals(codes)) {
        throw BusinessException.error("验证码错误");
    }
    redisTemplate.delete(StrUtil.format(SEND_MOBILE_CODE_KEY, LpCommonWebUtil.getCurrentUserInfo().getId()));

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值