优化验证码注册逻辑

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

View File

@@ -17,22 +17,30 @@ public class User implements UserDetails {
@Column(name = "id") @Column(name = "id")
private Long id; private Long id;
@Column(name = "user_id", unique = true, nullable = false)
private String userId;
@Column(unique = true) @Column(unique = true)
private String username; private String nickname;
@Column() @Column()
private String password; private String password;
@Column(unique = true)
private String email;
@Column(unique = true, nullable = false) @Column(unique = true, nullable = false)
private String phone; private String phone;
@Column(name = "create_time", unique = true, nullable = false) @Column(name = "create_time", unique = true, nullable = false)
private Date creatTime; private Date creatTime;
// Getters and Setters @Column(name = "last_login_time", unique = true, nullable = false)
private Date lastLoginTime;
@Column(name = "update_time", unique = true, nullable = false)
private Date updateTime;
@Column(name = "device_id", unique = true, nullable = false)
private String deviceId;
@Override @Override
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.emptyList(); return Collections.emptyList();
@@ -67,12 +75,28 @@ public class User implements UserDetails {
this.id = id; this.id = id;
} }
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getUsername() { public String getUsername() {
return username; return nickname;
} }
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.nickname = username;
} }
public String getPassword() { public String getPassword() {
@@ -83,14 +107,6 @@ public class User implements UserDetails {
this.password = password; this.password = password;
} }
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() { public String getPhone() {
return phone; return phone;
} }
@@ -106,4 +122,28 @@ public class User implements UserDetails {
public void setCreatTime(Date creatTime) { public void setCreatTime(Date creatTime) {
this.creatTime = creatTime; this.creatTime = creatTime;
} }
public Date getLastLoginTime() {
return lastLoginTime;
}
public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
} }

View File

@@ -5,9 +5,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional; import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> { public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
boolean existsByUsername(String username);
boolean existsByEmail(String email);
Optional<User> findByPhone(String phone); Optional<User> findByPhone(String phone);
boolean existsByPhone(String phone); boolean existsByPhone(String phone);
Optional<User> findByUserId(String userId);
boolean existsByUserId(String userId);
} }

View File

@@ -2,6 +2,7 @@ package com.onekeycall.videotablet.service;
import com.onekeycall.videotablet.entity.User; import com.onekeycall.videotablet.entity.User;
import com.onekeycall.videotablet.repository.UserRepository; import com.onekeycall.videotablet.repository.UserRepository;
import com.onekeycall.videotablet.utils.SecureIdGenerator;
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.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@@ -27,19 +28,19 @@ public class UserService implements UserDetailsService {
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
} }
public User registerUser(String username, String password) { public User registerUser(String userId, String password) {
if (userRepository.existsByUsername(username)) { if (userRepository.existsByUserId(userId)) {
throw new RuntimeException("Username already exists"); throw new RuntimeException("Username already exists");
} }
User user = new User(); User user = new User();
user.setUsername(username); user.setUsername(userId);
user.setPassword(passwordEncoder.encode(password)); user.setPassword(passwordEncoder.encode(password));
return userRepository.save(user); return userRepository.save(user);
} }
public User registerByPhone(String phone, String code, Date createTime) { public User registerByPhone(String phone, String code, String deviceId, Date createTime) {
// 1. 验证验证码 // 1. 验证验证码
Map<String, Object> codeMap = (Map<String, Object>) redisTemplate.opsForValue().get(phone); Map<String, Object> codeMap = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
if (codeMap == null || !code.equals(codeMap.get("code").toString())) { if (codeMap == null || !code.equals(codeMap.get("code").toString())) {
@@ -55,7 +56,11 @@ public class UserService implements UserDetailsService {
User user = new User(); User user = new User();
user.setPhone(phone); user.setPhone(phone);
user.setCreatTime(createTime); user.setCreatTime(createTime);
user.setLastLoginTime(createTime);
user.setUpdateTime(createTime);
user.setUserId(SecureIdGenerator.generateSecureId(12));
user.setUsername(SecureIdGenerator.generateSecureUserName(8));
user.setDeviceId(deviceId);
return userRepository.save(user); return userRepository.save(user);
} }
@@ -72,8 +77,8 @@ public class UserService implements UserDetailsService {
} }
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
return userRepository.findByUsername(username) return userRepository.findByUserId(userId)
.orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username)); .orElseThrow(() -> new UsernameNotFoundException("User not found with userId: " + userId));
} }
} }

View File

@@ -0,0 +1,29 @@
package com.onekeycall.videotablet.utils;
import java.security.SecureRandom;
public class SecureIdGenerator {
private static final String CHAR_POOL = "abcdefghijklmnopqrstuvwxyz0123456789";
public static String generateSecureId(int length) {
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder("kcid_");
for (int i = 0; i < length; i++) {
int index = random.nextInt(CHAR_POOL.length());
sb.append(CHAR_POOL.charAt(index));
}
return sb.toString();
}
private static final String MUNBER_POOL = "0123456789";
public static String generateSecureUserName(int length) {
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder("用户");
for (int i = 0; i < length; i++) {
int index = random.nextInt(MUNBER_POOL.length());
sb.append(MUNBER_POOL.charAt(index));
}
return sb.toString();
}
}