优化验证码
This commit is contained in:
@@ -7,11 +7,13 @@ import com.ttstd.videotablet.sms.SendSms;
|
|||||||
import com.ttstd.videotablet.utils.TextUtils;
|
import com.ttstd.videotablet.utils.TextUtils;
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -24,27 +26,30 @@ public class SmsController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private StringRedisTemplate stringRedisTemplate;
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@GetMapping("/verify_code")
|
@GetMapping("/verify_code")
|
||||||
public Result getMethodName(@RequestParam("number") String number) {
|
public Result getMethodName(@RequestParam("phone") String phone) {
|
||||||
if (TextUtils.isEmpty(number)) {
|
if (TextUtils.isEmpty(phone)) {
|
||||||
return Result.error().message("phone number is empty");
|
return Result.error().message("phone number is empty");
|
||||||
}
|
}
|
||||||
String oldCode = stringRedisTemplate.opsForValue().get(number);
|
String oldCode = stringRedisTemplate.opsForValue().get(phone);
|
||||||
if (TextUtils.isEmpty(oldCode)) {
|
if (TextUtils.isEmpty(oldCode)) {
|
||||||
String code = SendSms.generatedcode(6);
|
String code = SendSms.generatedcode(6);
|
||||||
return sendCode(number, code);
|
return sendCode(phone, code);
|
||||||
} else {
|
} else {
|
||||||
return sendCode(number, oldCode);
|
return sendCode(phone, oldCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Result sendCode(String number, String code) {
|
private Result sendCode(String phone, String code) {
|
||||||
try {
|
try {
|
||||||
com.aliyun.dysmsapi20170525.Client client = createClient();
|
com.aliyun.dysmsapi20170525.Client client = createClient();
|
||||||
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
||||||
.setSignName("同同的小站")
|
.setSignName("同同的小站")
|
||||||
.setTemplateCode("SMS_468370574")
|
.setTemplateCode("SMS_468370574")
|
||||||
.setPhoneNumbers(number)
|
.setPhoneNumbers(phone)
|
||||||
.setTemplateParam("{\"code\":\"" + code + "\"}");
|
.setTemplateParam("{\"code\":\"" + code + "\"}");
|
||||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
@@ -54,11 +59,15 @@ public class SmsController {
|
|||||||
// System.out.println(sendSmsResponse.getBody().getCode());
|
// System.out.println(sendSmsResponse.getBody().getCode());
|
||||||
if ("OK".equals(sendSmsResponse.getBody().getCode())) {
|
if ("OK".equals(sendSmsResponse.getBody().getCode())) {
|
||||||
String randomString = RandomStringUtils.randomAlphanumeric(32);
|
String randomString = RandomStringUtils.randomAlphanumeric(32);
|
||||||
|
long expireAt = System.currentTimeMillis() + Duration.ofMinutes(5).toMillis();
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("verify_key", randomString);
|
map.put("phone", phone);
|
||||||
|
map.put("code", code);
|
||||||
map.put("sms", sendSmsResponse.getBody().getMessage());
|
map.put("sms", sendSmsResponse.getBody().getMessage());
|
||||||
|
map.put("verifyKey", randomString);
|
||||||
|
map.put("expireAt", expireAt);
|
||||||
//4.保存验证码到Redis,并且设置有效期5分钟
|
//4.保存验证码到Redis,并且设置有效期5分钟
|
||||||
stringRedisTemplate.opsForValue().set(number, code, LOGIN_CODE_TTL, TimeUnit.MINUTES);
|
redisTemplate.opsForValue().set(phone, map, Duration.ofMinutes(5));
|
||||||
return Result.ok().data(map);
|
return Result.ok().data(map);
|
||||||
} else {
|
} else {
|
||||||
return Result.error().message(sendSmsResponse.getBody().getMessage());
|
return Result.error().message(sendSmsResponse.getBody().getMessage());
|
||||||
|
|||||||
30
src/main/java/com/ttstd/videotablet/redis/RedisConfig.java
Normal file
30
src/main/java/com/ttstd/videotablet/redis/RedisConfig.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.ttstd.videotablet.redis;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
|
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||||
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
|
||||||
|
// Key序列化为字符串
|
||||||
|
template.setKeySerializer(new StringRedisSerializer());
|
||||||
|
// Value序列化为JSON
|
||||||
|
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||||
|
|
||||||
|
// 对于Hash结构,根据需要配置
|
||||||
|
template.setHashKeySerializer(new StringRedisSerializer());
|
||||||
|
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user