增加手机账号密码登录,修改状态返回
This commit is contained in:
@@ -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<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
|
||||
Reference in New Issue
Block a user