From ad147827023ac9a076a6ce91e09aa92073d0ae9e Mon Sep 17 00:00:00 2001 From: heshunme Date: Tue, 2 Jul 2024 02:25:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DJwtVerify=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthenticationController.java | 17 ++++++++--------- .../dto/ChangePasswordRequest.java | 3 ++- .../backend/authentication/dto/JwtRequest.java | 11 ----------- .../dto/ManageUserProfileRequest.java | 1 + 4 files changed, 11 insertions(+), 21 deletions(-) delete mode 100644 src/main/java/org/cmh/backend/authentication/dto/JwtRequest.java diff --git a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java index 1904686..8d2fdb4 100644 --- a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java +++ b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java @@ -2,6 +2,7 @@ package org.cmh.backend.authentication.controller; import org.cmh.backend.Utils.JwtUtil; +import org.cmh.backend.Utils.JwtVerify; import org.cmh.backend.authentication.dto.*; import org.cmh.backend.authentication.model.UserHS; import org.cmh.backend.authentication.service.UserService; @@ -75,23 +76,21 @@ class AuthenticationController { } @PostMapping("/manageUserProfile") + @JwtVerify public ResponseEntity manageUserProfile(@RequestBody ManageUserProfileRequest userProfileRequest) { String username = JwtUtil.extractUsername(userProfileRequest.getToken()); - if (username != null) { - boolean succeeded = userService.updateUserProfile(username, userProfileRequest); - if (succeeded) { - return new ResponseEntity<>(HttpStatus.OK); - } + boolean succeeded = userService.updateUserProfile(username, userProfileRequest); + if (succeeded) { + return new ResponseEntity<>(HttpStatus.OK); } return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } @PostMapping("/changePassword") + @JwtVerify public ResponseEntity changePassword(@RequestBody ChangePasswordRequest changePasswordRequest) { - if (JwtUtil.isTokenValid(changePasswordRequest.getToken())) { - if (userService.changePassword(JwtUtil.extractUsername(changePasswordRequest.getToken()), changePasswordRequest)) { - return new ResponseEntity<>(HttpStatus.OK); - } + if (userService.changePassword(JwtUtil.extractUsername(changePasswordRequest.getToken()), changePasswordRequest)) { + return new ResponseEntity<>(HttpStatus.OK); } return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } diff --git a/src/main/java/org/cmh/backend/authentication/dto/ChangePasswordRequest.java b/src/main/java/org/cmh/backend/authentication/dto/ChangePasswordRequest.java index 5768562..b59142a 100644 --- a/src/main/java/org/cmh/backend/authentication/dto/ChangePasswordRequest.java +++ b/src/main/java/org/cmh/backend/authentication/dto/ChangePasswordRequest.java @@ -2,10 +2,11 @@ package org.cmh.backend.authentication.dto; import lombok.Getter; import lombok.Setter; +import org.cmh.backend.Utils.JwtRequest; @Getter @Setter -public class ChangePasswordRequest extends JwtRequest{ +public class ChangePasswordRequest extends JwtRequest { private String currentPassword; private String newPassword; } diff --git a/src/main/java/org/cmh/backend/authentication/dto/JwtRequest.java b/src/main/java/org/cmh/backend/authentication/dto/JwtRequest.java deleted file mode 100644 index 05cbacf..0000000 --- a/src/main/java/org/cmh/backend/authentication/dto/JwtRequest.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.cmh.backend.authentication.dto; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class JwtRequest { - private String token; -} - diff --git a/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java b/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java index 0c1f6ee..64a41a8 100644 --- a/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java +++ b/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java @@ -2,6 +2,7 @@ package org.cmh.backend.authentication.dto; import lombok.Getter; import lombok.Setter; +import org.cmh.backend.Utils.JwtRequest; @Getter @Setter