76 lines
3.2 KiB
Java
76 lines
3.2 KiB
Java
package com.onekeycall.videotablet.controller;
|
|
|
|
import com.onekeycall.videotablet.result.Result;
|
|
import com.onekeycall.videotablet.service.UserService;
|
|
import com.onekeycall.videotablet.utils.JwtUtil;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/user")
|
|
public class UserPasswordController {
|
|
private final UserService userService;
|
|
private final AuthenticationManager authenticationManager;
|
|
|
|
@Autowired
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
@Autowired
|
|
private JwtUtil jwtUtil;
|
|
|
|
@Autowired
|
|
public UserPasswordController(UserService userService, AuthenticationManager authenticationManager) {
|
|
this.userService = userService;
|
|
this.authenticationManager = authenticationManager;
|
|
}
|
|
|
|
@PostMapping("/phone_set_password")
|
|
public Result setPasswordByPhone(
|
|
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
|
|
@RequestParam(value = "user_id") String userId,
|
|
@RequestParam String password, @RequestParam(value = "verify_password") String verifyPassword) {
|
|
|
|
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
|
return Result.error().message("Authorization header is incorrect");
|
|
}
|
|
|
|
if (!StringUtils.equals(password, verifyPassword)) {
|
|
return Result.error().message("password is not same");
|
|
}
|
|
String token = authHeader.substring(7); // 提取真正的Token
|
|
if (jwtUtil.validateAccessToken(userId, token, deviceId)) {
|
|
userService.setPasswordByUserId(userId, password);
|
|
return Result.ok().message("set password success");
|
|
} else {
|
|
return Result.error().message("token is not same");
|
|
}
|
|
}
|
|
|
|
@PostMapping("/change_password")
|
|
public Result changePassword(
|
|
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
|
|
@RequestParam(value = "user_id") String userId, @RequestParam(value = "old_password") String oldPassword,
|
|
@RequestParam String password, @RequestParam(value = "verify_password") String verifyPassword) {
|
|
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
|
return Result.error().message("Authorization header is incorrect");
|
|
}
|
|
|
|
String token = authHeader.substring(7);
|
|
if (!StringUtils.equals(password, verifyPassword)) {
|
|
return Result.error().message("password is not same");
|
|
}
|
|
if (oldPassword.equals(password)) {
|
|
return Result.error().message("The old password and the new password are the same");
|
|
|
|
}
|
|
if (jwtUtil.validateAccessToken(userId, token, deviceId)) {
|
|
return userService.changePassword(userId, oldPassword, password);
|
|
} else {
|
|
return Result.error().message("token is not same");
|
|
}
|
|
}
|
|
}
|