校驗短信驗證碼
接着上一篇部落格https://blog.csdn.net/qq_42981638/article/details/94656441,成功實作可以發送短信驗證碼之後,一般可以把驗證碼存放在redis中,并且設定存放時間,一般短信驗證碼都是1分鐘或者90s過期,這個看個人需求。是以我們可以利用redis的特性,設定存放時間,直接上代碼。
第一步,在pom檔案導入redis的依賴
redis.clients
jedis
2.1.0
org.apache.commons
commons-lang3
3.3.2
第二步,在配置檔案中配置好,redis的端口号,密碼
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=root
# 連接配接池最大連接配接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 連接配接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=-1
# 連接配接池中的最大空閑連接配接
spring.redis.jedis.pool.max-idle=8
# 連接配接池中的最小空閑連接配接
spring.redis.jedis.pool.min-idle=0
# 連接配接逾時時間(毫秒)
spring.redis.timeout=5000
**接着上代碼
package com.koohe.util;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class SmsSendUtil {
private static final String PRODUCT = "Dysmsapi";
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
// 簽名KEY
private static final String ACCESS_KEY_ID = "LTAIiKVKFzm3Vsri";
// 簽名密鑰
private static final String ACCESS_KEY_SECRET = "ww9nVlltvqfhjSWscfoVq04M7aItPY";
// 短信模闆ID: SMS_11480310
private static final String TEMPLATE_CODE = "SMS_11480310";
// 短信簽名
private static final String SIGN_NAME = "五子連珠";
public static boolean send(String phone, String verify){
try {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
ACCESS_KEY_ID,ACCESS_KEY_SECRET);
DefaultProfile.addEndpoint("cn-hangzhou","cn-hangzhou",
PRODUCT, DOMAIN);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
// 必填: 待發送手機号
request.setPhoneNumbers(phone);
// 必填: 短信簽名-可在短信控制台中找到
request.setSignName(SIGN_NAME);
// 必填: 短信模闆-可在短信控制台中找到
request.setTemplateCode(TEMPLATE_CODE);
request.setTemplateParam("{\"number\":\"" + verify + "\"}");
// hint 此處可能會抛出異常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse.getCode() != null &&
sendSmsResponse.getCode().equals("OK");
}catch (Exception ex){
throw new RuntimeException(ex);
}
}
}
package com.koohe.util;
import java.util.UUID;
public class Random {
public static String generateCaptcha() {
String captcha = UUID.randomUUID().toString()
.replaceAll("-", "")
.replaceAll("[a-z|A-Z]","")
.substring(0, 6);
return captcha;
}
}
package com.koohe.service;
public interface SendMessageService {
public Boolean sendMessage(String phone);
public Boolean saveCaptcha(String captcha,String phone);
public Boolean checkCaptcha(String captcha,String phone);
}
package com.koohe.service.impl;
import com.koohe.service.SendMessageService;
import com.koohe.util.Random;
import com.koohe.util.SmsSendUtil;
import io.lettuce.core.dynamic.domain.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class SendMessageServiceImpl implements SendMessageService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public Boolean sendMessage(String phone) {
try {
boolean data = SmsSendUtil.send(phone, Random.generateCaptcha());
return data;
} catch (Exception ex){
throw new RuntimeException(ex);
}
}
@Override
public Boolean saveCaptcha(String captcha, String phone) {
try {
//将驗證碼存儲在redis中,并且設定過期時間,90s
redisTemplate.boundValueOps(phone).set(captcha, 90, TimeUnit.SECONDS);
return true;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Boolean checkCaptcha(String captcha,String phone) {
try {
if (captcha.equals(redisTemplate.boundValueOps(phone).get())) {
return true;
} else {
return false;
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
package com.koohe.Controller;
import com.koohe.service.SendMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.lang.annotation.Documented;
@RestController
public class SendMessageController {
@Autowired
private SendMessageService sendMessageService;
@GetMapping("/sendCaptcha")
public Boolean sendCaptcha(String phone) {
try {
Boolean data = sendMessageService.sendMessage(phone);
return data;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
@GetMapping("/saveCaptcha")
public Boolean saveCaptcha(String captcha,String phone) {
try {
Boolean data = sendMessageService.saveCaptcha(captcha,phone);
return data;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
@GetMapping("/checkCaptcha")
public Boolean checkCaptcha(String captcha,String phone) {
try {
Boolean data = sendMessageService.checkCaptcha(captcha,phone);
return data;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
}