优化验证码注册逻辑

This commit is contained in:
2025-08-05 16:20:51 +08:00
parent 69700c8fe1
commit 7bc94795e5
5 changed files with 117 additions and 37 deletions

View File

@@ -1,7 +1,9 @@
package com.onekeycall.videotablet.controller;
import com.onekeycall.videotablet.dto.TokenPair;
import com.onekeycall.videotablet.entity.User;
import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.utils.JwtUtil;
import com.onekeycall.videotablet.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
@@ -10,6 +12,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@@ -24,6 +27,8 @@ public class LoginController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private JwtUtil jwtUtil;
@Autowired
public LoginController(UserService userService, AuthenticationManager authenticationManager) {
@@ -72,25 +77,27 @@ public class LoginController {
}
@PostMapping("/public/registerByPhone")
public ResponseEntity<?> registerByPhone(@RequestBody PhoneRequest request) {
String requestVerifyKey = request.getVerifyKey();
if (TextUtils.isEmpty(requestVerifyKey)) {
public ResponseEntity<?> registerByPhone(
@RequestParam String phone, @RequestParam String code,
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) {
if (TextUtils.isEmpty(verifyKey)) {
return new ResponseEntity<>("verify key is empty", HttpStatus.BAD_REQUEST);
}
String phone = request.getPhone();
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
if (map != null) {
String verifyKey = (String) map.get("verifyKey");
if (!Objects.equals(verifyKey, requestVerifyKey)) {
String redisVerifyKey = (String) map.get("verifyKey");
if (!Objects.equals(redisVerifyKey, verifyKey)) {
return new ResponseEntity<>("verify key is not same", HttpStatus.BAD_REQUEST);
}
String code = map.get("code").toString();
if (!Objects.equals(code, request.getCode())) {
String redisCode = map.get("code").toString();
if (!Objects.equals(redisCode, code)) {
return new ResponseEntity<>("code is not same", HttpStatus.BAD_REQUEST);
}
try {
User user = userService.registerByPhone(request.getPhone(), request.getCode(), new Date());
return new ResponseEntity<>(user, HttpStatus.CREATED);
User user = userService.registerByPhone(phone, code, deviceId,new Date());
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
return new ResponseEntity<>(tokenPair, HttpStatus.CREATED);
} catch (RuntimeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}