增加手机账号密码登录,修改状态返回

This commit is contained in:
2025-08-08 09:22:53 +08:00
parent ad85efaa74
commit 411b7d69d3
3 changed files with 35 additions and 2 deletions

View File

@@ -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") @PostMapping("/login")
public ResponseEntity<?> login( public ResponseEntity<?> login(
@RequestHeader("Device-ID") String deviceId, @RequestHeader("Device-ID") String deviceId,
@@ -126,8 +154,8 @@ public class LoginController {
} }
} }
@PostMapping("/phone_login") @PostMapping("/phone_code_login")
public Result loginByPhone( public Result loginByPhoneCode(
@RequestParam String phone, @RequestParam String code, @RequestParam String phone, @RequestParam String code,
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) { @RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) {
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone); Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);

View File

@@ -10,4 +10,5 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUserId(String userId); Optional<User> findByUserId(String userId);
boolean existsByUserId(String userId); boolean existsByUserId(String userId);
boolean existsPasswordByUserId(String userId); boolean existsPasswordByUserId(String userId);
Optional<User> findUserByPhone(String phone);
} }

View File

@@ -29,6 +29,10 @@ public class UserService implements UserDetailsService {
this.redisTemplate = redisTemplate; 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) { public User registerUser(String userId, String password) {
if (userRepository.existsByUserId(userId)) { if (userRepository.existsByUserId(userId)) {
throw new RuntimeException("Username already exists"); throw new RuntimeException("Username already exists");