短信验证重写方法修改

This commit is contained in:
syruan 2023-12-14 11:25:10 +08:00
parent 07d826f2d3
commit 8da0ba0275
2 changed files with 27 additions and 7 deletions

View File

@ -1,11 +1,9 @@
package com.bonus.sgzb.auth.controller; package com.bonus.sgzb.auth.controller;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.sgzb.auth.form.LoginBody; import com.bonus.sgzb.auth.form.LoginBody;
import com.bonus.sgzb.auth.form.RegisterBody; import com.bonus.sgzb.auth.form.RegisterBody;
import com.bonus.sgzb.auth.service.SysLoginService; import com.bonus.sgzb.auth.service.SysLoginService;
@ -39,14 +37,24 @@ public class TokenController
return R.ok(tokenService.createToken(userInfo)); return R.ok(tokenService.createToken(userInfo));
} }
@PostMapping("loginCode") @PostMapping("sendCode")
public R<?> loginCode(@RequestBody LoginBody form) { public R<?> sendCode(@RequestBody LoginBody form) {
// 用户登录 // 用户登录
LoginUser userInfo = sysLoginService.loginCode(form.getPhone(), form.getCode()); LoginUser userInfo = sysLoginService.loginCode(form.getPhone(), form.getCode());
// 获取登录token // 获取登录token
return R.ok(tokenService.createToken(userInfo)); return R.ok(tokenService.createToken(userInfo));
} }
@PostMapping("checkCode")
public R<?> checkCode(@RequestBody LoginBody form) {
// 校验验证码
boolean result = sysLoginService.checkCode(form.getPhone(), form.getCode());
if (result) {
return R.ok(null, "验证码正确");
}
return R.fail("短信验证码错误");
}
@DeleteMapping("logout") @DeleteMapping("logout")
public R<?> logout(HttpServletRequest request) public R<?> logout(HttpServletRequest request)
{ {

View File

@ -17,7 +17,6 @@ import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.system.api.RemoteUserService; import com.bonus.sgzb.system.api.RemoteUserService;
import com.bonus.sgzb.system.api.domain.SysUser; import com.bonus.sgzb.system.api.domain.SysUser;
import com.bonus.sgzb.system.api.model.LoginUser; import com.bonus.sgzb.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -151,6 +150,19 @@ public class SysLoginService
} }
} }
public boolean checkCode(String phone, String code) {
String redisCode = redisService.getCacheObject("code_" + phone);
if (StringUtils.isEmpty(redisCode)) {
throw new ServiceException("验证码失效", 403);
}
if (!StringUtils.equals(redisCode.split("_")[0], code)) {
throw new ServiceException("验证码错误", 401);
} else {
redisService.deleteObject("code_" + phone);
return true;
}
}
private void isBlackIp(String phone) { private void isBlackIp(String phone) {
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST)); String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))