diff --git a/src/main/java/com/onekeycall/videotablet/controller/LoginController.java b/src/main/java/com/onekeycall/videotablet/controller/LoginController.java index 47f192d..27fea31 100644 --- a/src/main/java/com/onekeycall/videotablet/controller/LoginController.java +++ b/src/main/java/com/onekeycall/videotablet/controller/LoginController.java @@ -46,6 +46,34 @@ public class LoginController { } } + @PostMapping("/phone_login") + public Result phoneLogin( + @RequestHeader("Device-ID") String deviceId, + @RequestParam String phone, @RequestParam String password) { + User user = userService.getUserByPhone(phone); + if (user == null) { + return Result.error().message("User not found with phone: " + phone); + } + String userId = user.getUserId(); + + // 1. 创建认证令牌 + Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userId, password); + + try { + // 2. 使用 AuthenticationManager 进行认证(核心步骤) + Authentication authentication = authenticationManager.authenticate(authenticationToken); + + // 3. 认证成功后生成 JWT + User userDetails = (User) authentication.getPrincipal(); + TokenPair tokenPair = jwtUtil.generateTokenPair(userDetails.getUserId(), deviceId); + // 4. 返回 Token + return Result.ok().data(Collections.singletonMap("token", tokenPair.toMap())); + } catch (Exception e) { + e.printStackTrace(); + return Result.error().message("登录失败"); + } + } + @PostMapping("/login") public ResponseEntity login( @RequestHeader("Device-ID") String deviceId, @@ -126,8 +154,8 @@ public class LoginController { } } - @PostMapping("/phone_login") - public Result loginByPhone( + @PostMapping("/phone_code_login") + public Result loginByPhoneCode( @RequestParam String phone, @RequestParam String code, @RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) { Map map = (Map) redisTemplate.opsForValue().get(phone); diff --git a/src/main/java/com/onekeycall/videotablet/repository/UserRepository.java b/src/main/java/com/onekeycall/videotablet/repository/UserRepository.java index 8e7ee4d..9cb1dd2 100644 --- a/src/main/java/com/onekeycall/videotablet/repository/UserRepository.java +++ b/src/main/java/com/onekeycall/videotablet/repository/UserRepository.java @@ -10,4 +10,5 @@ public interface UserRepository extends JpaRepository { Optional findByUserId(String userId); boolean existsByUserId(String userId); boolean existsPasswordByUserId(String userId); + Optional findUserByPhone(String phone); } \ No newline at end of file diff --git a/src/main/java/com/onekeycall/videotablet/service/UserService.java b/src/main/java/com/onekeycall/videotablet/service/UserService.java index 3cf87ca..ae36159 100644 --- a/src/main/java/com/onekeycall/videotablet/service/UserService.java +++ b/src/main/java/com/onekeycall/videotablet/service/UserService.java @@ -29,6 +29,10 @@ public class UserService implements UserDetailsService { this.redisTemplate = redisTemplate; } + public User getUserByPhone(String phone) { + return userRepository.findUserByPhone(phone).orElseThrow(() -> new RuntimeException("User not found with phone: " + phone)); + } + public User registerUser(String userId, String password) { if (userRepository.existsByUserId(userId)) { throw new RuntimeException("Username already exists");