完成/changePassword,测试通过
This commit is contained in:
parent
cbe7ec9a24
commit
c03b190aba
@ -1,9 +1,6 @@
|
||||
package org.cmh.backend.authentication.controller;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.cmh.backend.Utils.JwtUtil;
|
||||
import org.cmh.backend.authentication.dto.*;
|
||||
import org.cmh.backend.authentication.model.UserHS;
|
||||
@ -13,8 +10,6 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@RestController
|
||||
class AuthenticationController {
|
||||
|
||||
@ -86,5 +81,15 @@ class AuthenticationController {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@PostMapping("/changePassword")
|
||||
public ResponseEntity<Object> changePassword(@RequestBody ChangePasswordRequest changePasswordRequest) {
|
||||
if (JwtUtil.isTokenValid(changePasswordRequest.getToken())) {
|
||||
if (userService.changePassword(JwtUtil.extractUsername(changePasswordRequest.getToken()), changePasswordRequest)) {
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
package org.cmh.backend.authentication.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class ChangePasswordRequest extends JwtRequest{
|
||||
private String currentPassword;
|
||||
private String newPassword;
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package org.cmh.backend.authentication.service;
|
||||
|
||||
import org.cmh.backend.authentication.dto.ChangePasswordRequest;
|
||||
import org.cmh.backend.authentication.dto.ManageUserProfileRequest;
|
||||
import org.cmh.backend.authentication.model.UserHS;
|
||||
import org.cmh.backend.authentication.repository.UserRepository;
|
||||
@ -69,8 +70,7 @@ public class UserService {
|
||||
user.setRole(request.getRole());
|
||||
try {
|
||||
userRepository.save(user);
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -79,6 +79,25 @@ public class UserService {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean changePassword(String username, ChangePasswordRequest request) {
|
||||
UserHS user = userRepository.findByUsername(username);
|
||||
if (user != null) {
|
||||
if (passwordEncoder.matches(request.getCurrentPassword(), user.getPassword())) {
|
||||
if (isValidPassword(request.getNewPassword())) {
|
||||
String encodedPassword = passwordEncoder.encode(request.getNewPassword());
|
||||
user.setPassword(encodedPassword);
|
||||
try {
|
||||
userRepository.save(user);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 验证用户名格式
|
||||
private boolean isValidUsername(String username) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user