This commit is contained in:
wangjinlei
2023-11-07 17:14:15 +08:00
parent 5b71036241
commit a152865cfe
10 changed files with 42 additions and 29 deletions

View File

@@ -17,14 +17,9 @@ import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.book.to.PageIdDto;
import com.peanut.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -173,6 +168,7 @@ public class MyUserController {
@RequestMapping("/sms/sendcode")
public R registerSms(@RequestParam("phone") String phone) throws Exception {
//验证一分钟内是否已经发过
String redisCode = redisTemplate.opsForValue().get("RegistCode" + phone);
if (!StringUtils.isEmpty(redisCode)) {
long l = Long.parseLong(redisCode.split("_")[1]);
@@ -181,20 +177,21 @@ public class MyUserController {
return R.error(500,"短信验证码频率过高,请稍后再试!");
}
}
//生成随机五位数
Random random = new Random();
String i = random.nextInt(99999) + "";//生成字符串
String i = random.nextInt(99999) + "";
StringBuffer sb = new StringBuffer();
for (int j = 0; j < 5 - i.length(); j++) {
sb.append("0");//不足5位就行补0
sb.append("0");
}
i = sb.toString() + i;
// String code = UUID.randomUUID().toString().substring(0,5)+"_"+System.currentTimeMillis();
String code = i + "_"+System.currentTimeMillis();
//redis 缓存验证码
redisTemplate.opsForValue().set("RegistCode"+phone,code,5, TimeUnit.MINUTES);
//防止同一个手机号 60s 内再次发送
//发送
userService.sendCodeForRegister(phone,code);
return R.ok();