From ef7e374449cd526e9025728428ac540a7e10b4cc Mon Sep 17 00:00:00 2001 From: koungq Date: Mon, 15 Jul 2024 23:17:33 +0900 Subject: [PATCH 1/9] =?UTF-8?q?Chore:=20=EB=B3=80=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20phoneNumber=20->=20tel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/leets/weeth/domain/user/entity/User.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/leets/weeth/domain/user/entity/User.java b/src/main/java/leets/weeth/domain/user/entity/User.java index 605caf6b..e6f0b81b 100644 --- a/src/main/java/leets/weeth/domain/user/entity/User.java +++ b/src/main/java/leets/weeth/domain/user/entity/User.java @@ -30,7 +30,7 @@ public class User extends BaseEntity { private String studentId; - private String phoneNumber; + private String tel; @Enumerated(EnumType.STRING) private Position position; From 4413d1bb1d637ab6fccc016808070d324a64302f Mon Sep 17 00:00:00 2001 From: koungq Date: Mon, 15 Jul 2024 23:22:59 +0900 Subject: [PATCH 2/9] =?UTF-8?q?Feat:=20@CheckPassword=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=ED=95=9C=20=EB=B9=84=EB=B0=80=EB=B2=88=ED=98=B8=20=EC=9D=BC?= =?UTF-8?q?=EC=B9=98=20=EC=97=AC=EB=B6=80=20=EA=B2=80=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/leets/weeth/domain/user/dto/UserDTO.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/leets/weeth/domain/user/dto/UserDTO.java b/src/main/java/leets/weeth/domain/user/dto/UserDTO.java index 4c901d72..17a5ff2e 100644 --- a/src/main/java/leets/weeth/domain/user/dto/UserDTO.java +++ b/src/main/java/leets/weeth/domain/user/dto/UserDTO.java @@ -3,6 +3,7 @@ import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import leets.weeth.domain.user.annotation.CheckPassword; import leets.weeth.domain.user.entity.enums.Department; import leets.weeth.domain.user.entity.enums.Position; @@ -12,8 +13,9 @@ public record SignUp ( @NotBlank String name, @Email @NotBlank String email, @NotBlank String password, + @NotBlank @CheckPassword String passwordConfirm, @NotBlank String studentId, - @NotBlank String phoneNumber, + @NotBlank String tel, @NotNull Position position, @NotNull Department department, @NotNull Integer cardinal @@ -23,6 +25,7 @@ public record Response( Integer id, String name, String studentId, + String tel, Department department, String email, Integer cardinal, From 80b3546ed1822850703e108ed248173968a498e6 Mon Sep 17 00:00:00 2001 From: koungq Date: Mon, 15 Jul 2024 23:23:20 +0900 Subject: [PATCH 3/9] =?UTF-8?q?Feat:=20Validation=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EB=A9=94=EC=84=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weeth/domain/user/repository/UserRepository.java | 6 ++++++ .../error/exception/custom/EmailExistsException.java | 9 --------- 2 files changed, 6 insertions(+), 9 deletions(-) delete mode 100644 src/main/java/leets/weeth/global/common/error/exception/custom/EmailExistsException.java diff --git a/src/main/java/leets/weeth/domain/user/repository/UserRepository.java b/src/main/java/leets/weeth/domain/user/repository/UserRepository.java index ebcf5ace..4e5ae4ef 100644 --- a/src/main/java/leets/weeth/domain/user/repository/UserRepository.java +++ b/src/main/java/leets/weeth/domain/user/repository/UserRepository.java @@ -9,5 +9,11 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); + boolean existsByEmail(String email); + + boolean existsByStudentId(String studentId); + + boolean existsByTel(String tel); + Optional findByRefreshToken(String refreshToken); } diff --git a/src/main/java/leets/weeth/global/common/error/exception/custom/EmailExistsException.java b/src/main/java/leets/weeth/global/common/error/exception/custom/EmailExistsException.java deleted file mode 100644 index 66e52dd7..00000000 --- a/src/main/java/leets/weeth/global/common/error/exception/custom/EmailExistsException.java +++ /dev/null @@ -1,9 +0,0 @@ -package leets.weeth.global.common.error.exception.custom; - -import jakarta.persistence.EntityNotFoundException; - -public class EmailExistsException extends EntityNotFoundException { - public EmailExistsException() { - super("이미 사용 중인 이메일입니다."); - } -} \ No newline at end of file From aae6f81af42e37b302d9f68f0c29edd3fefd1da9 Mon Sep 17 00:00:00 2001 From: koungq Date: Mon, 15 Jul 2024 23:23:33 +0900 Subject: [PATCH 4/9] =?UTF-8?q?Feat:=20Validation=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/weeth/domain/user/service/UserService.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/leets/weeth/domain/user/service/UserService.java b/src/main/java/leets/weeth/domain/user/service/UserService.java index 1fab9250..2db27625 100644 --- a/src/main/java/leets/weeth/domain/user/service/UserService.java +++ b/src/main/java/leets/weeth/domain/user/service/UserService.java @@ -5,8 +5,8 @@ import leets.weeth.domain.user.mapper.UserMapper; import leets.weeth.domain.user.repository.UserRepository; import leets.weeth.global.common.error.exception.custom.BusinessLogicException; -import leets.weeth.global.common.error.exception.custom.EmailExistsException; import leets.weeth.global.common.error.exception.custom.InvalidAccessException; +import leets.weeth.global.common.error.exception.custom.UserExistsException; import leets.weeth.global.common.error.exception.custom.UserNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.security.crypto.password.PasswordEncoder; @@ -31,10 +31,10 @@ public class UserService { private final PasswordEncoder passwordEncoder; public void signUp(UserDTO.SignUp requestDto) { - if (userRepository.findByEmail(requestDto.email()).isPresent()) - throw new EmailExistsException(); - - // 수정: 아이디 이외 중복 처리 + if(userRepository.existsByEmail(requestDto.email()) || // 이메일 중복 + userRepository.existsByStudentId(requestDto.studentId()) || // 학번 중복 + userRepository.existsByTel(requestDto.tel())) // 전화번호 중복 + throw new UserExistsException(); User user = mapper.from(requestDto, passwordEncoder); userRepository.save(user); From 371aa34ba999938e097c8d76e083cb2aa4dfd3a9 Mon Sep 17 00:00:00 2001 From: koungq Date: Mon, 15 Jul 2024 23:23:46 +0900 Subject: [PATCH 5/9] Feat: Custom Exception --- .../error/exception/custom/UserExistsException.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/leets/weeth/global/common/error/exception/custom/UserExistsException.java diff --git a/src/main/java/leets/weeth/global/common/error/exception/custom/UserExistsException.java b/src/main/java/leets/weeth/global/common/error/exception/custom/UserExistsException.java new file mode 100644 index 00000000..c19ba702 --- /dev/null +++ b/src/main/java/leets/weeth/global/common/error/exception/custom/UserExistsException.java @@ -0,0 +1,9 @@ +package leets.weeth.global.common.error.exception.custom; + +import jakarta.persistence.EntityNotFoundException; + +public class UserExistsException extends EntityNotFoundException { + public UserExistsException() { + super("이미 가입된 사용자입니다."); + } +} \ No newline at end of file From f9af19498896dcae917486419fbb209d6153a5c8 Mon Sep 17 00:00:00 2001 From: koungq Date: Mon, 15 Jul 2024 23:24:12 +0900 Subject: [PATCH 6/9] =?UTF-8?q?Feat:=20=EB=B9=84=EB=B0=80=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9D=BC=EC=B9=98=20=EC=97=AC=EB=B6=80=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/annotation/CheckPassword.java | 25 +++++++++++++++++++ .../user/validator/PasswordValidator.java | 19 ++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java create mode 100644 src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java diff --git a/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java b/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java new file mode 100644 index 00000000..2eec79ea --- /dev/null +++ b/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java @@ -0,0 +1,25 @@ +package leets.weeth.domain.user.annotation; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +import leets.weeth.domain.user.validator.PasswordValidator; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = PasswordValidator.class) +public @interface CheckPassword { + + String message() default "비밀번호가 일치하지 않습니다."; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} + diff --git a/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java b/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java new file mode 100644 index 00000000..3be3beca --- /dev/null +++ b/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java @@ -0,0 +1,19 @@ +package leets.weeth.domain.user.validator; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import leets.weeth.domain.user.annotation.CheckPassword; +import leets.weeth.domain.user.dto.UserDTO; + +public class PasswordValidator implements ConstraintValidator { + + @Override + public void initialize(CheckPassword constraintAnnotation) { + ConstraintValidator.super.initialize(constraintAnnotation); + } + + @Override + public boolean isValid(UserDTO.SignUp dto, ConstraintValidatorContext context) { + return !dto.password().equals(dto.passwordConfirm()); + } +} From 197210090640ff423fb7b98be98bbab7e61dbb01 Mon Sep 17 00:00:00 2001 From: koungq Date: Tue, 16 Jul 2024 00:15:07 +0900 Subject: [PATCH 7/9] =?UTF-8?q?Feat:=20=EB=B9=84=EB=B0=80=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=9D=BC=EC=B9=98=20=EC=97=AC=EB=B6=80=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/weeth/domain/user/annotation/CheckPassword.java | 5 ++--- src/main/java/leets/weeth/domain/user/dto/UserDTO.java | 3 ++- .../leets/weeth/domain/user/validator/PasswordValidator.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java b/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java index 2eec79ea..9cff507a 100644 --- a/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java +++ b/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java @@ -7,10 +7,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; -import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; -@Target({FIELD}) +@Target({TYPE}) @Retention(RUNTIME) @Constraint(validatedBy = PasswordValidator.class) public @interface CheckPassword { @@ -20,6 +20,5 @@ Class[] groups() default {}; Class[] payload() default {}; - } diff --git a/src/main/java/leets/weeth/domain/user/dto/UserDTO.java b/src/main/java/leets/weeth/domain/user/dto/UserDTO.java index 17a5ff2e..4fe5bdfb 100644 --- a/src/main/java/leets/weeth/domain/user/dto/UserDTO.java +++ b/src/main/java/leets/weeth/domain/user/dto/UserDTO.java @@ -9,11 +9,12 @@ public class UserDTO { + @CheckPassword public record SignUp ( @NotBlank String name, @Email @NotBlank String email, @NotBlank String password, - @NotBlank @CheckPassword String passwordConfirm, + @NotBlank String passwordConfirm, @NotBlank String studentId, @NotBlank String tel, @NotNull Position position, diff --git a/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java b/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java index 3be3beca..4f4a49e2 100644 --- a/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java +++ b/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java @@ -14,6 +14,6 @@ public void initialize(CheckPassword constraintAnnotation) { @Override public boolean isValid(UserDTO.SignUp dto, ConstraintValidatorContext context) { - return !dto.password().equals(dto.passwordConfirm()); + return dto.password().equals(dto.passwordConfirm()); } } From b6230451c86f7d31f9e6dfbae9f1b31983b9f202 Mon Sep 17 00:00:00 2001 From: koungq Date: Tue, 16 Jul 2024 00:17:04 +0900 Subject: [PATCH 8/9] =?UTF-8?q?Feat:=20@Valid=20Exception=20=ED=83=90?= =?UTF-8?q?=EC=A7=80=20=EA=B5=AC=EC=B2=B4=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/common/error/CommonExceptionController.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java b/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java index 9619fc94..2fcf16a4 100644 --- a/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java +++ b/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.validation.BindException; import org.springframework.web.ErrorResponse; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -27,6 +28,13 @@ public CommonResponse handle(Exception ex) { return CommonResponse.createFailure(status, ex.getMessage()); } + @ExceptionHandler(MethodArgumentNotValidException.class) // BindException == @ModelAttribute 어노테이션으로 받은 파라미터의 @Valid 통해 발생한 Exception + public CommonResponse handle(MethodArgumentNotValidException e) { // 클라이언트의 오류일 경우 + int status = 400; // 파라미터 값 실수이므로 4XX + log.error("Error", e); + return CommonResponse.createFailure(status, e.getAllErrors().get(0).getDefaultMessage()); // 디폴트 메세지 가져오기 + } + @ExceptionHandler(BindException.class) // BindException == @ModelAttribute 어노테이션으로 받은 파라미터의 @Valid 통해 발생한 Exception public CommonResponse handle(BindException e) { // 클라이언트의 오류일 경우 int status = 400; // 파라미터 값 실수이므로 4XX From 1d955f992087abcc9836e368c7b7ad2132874b37 Mon Sep 17 00:00:00 2001 From: koungq Date: Tue, 16 Jul 2024 13:19:31 +0900 Subject: [PATCH 9/9] RollBack: @CheckPassword --- .../domain/user/annotation/CheckPassword.java | 24 ------------------- .../leets/weeth/domain/user/dto/UserDTO.java | 2 -- .../user/validator/PasswordValidator.java | 19 --------------- .../error/CommonExceptionController.java | 8 ------- 4 files changed, 53 deletions(-) delete mode 100644 src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java delete mode 100644 src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java diff --git a/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java b/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java deleted file mode 100644 index 9cff507a..00000000 --- a/src/main/java/leets/weeth/domain/user/annotation/CheckPassword.java +++ /dev/null @@ -1,24 +0,0 @@ -package leets.weeth.domain.user.annotation; - -import jakarta.validation.Constraint; -import jakarta.validation.Payload; -import leets.weeth.domain.user.validator.PasswordValidator; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -@Target({TYPE}) -@Retention(RUNTIME) -@Constraint(validatedBy = PasswordValidator.class) -public @interface CheckPassword { - - String message() default "비밀번호가 일치하지 않습니다."; - - Class[] groups() default {}; - - Class[] payload() default {}; -} - diff --git a/src/main/java/leets/weeth/domain/user/dto/UserDTO.java b/src/main/java/leets/weeth/domain/user/dto/UserDTO.java index 4fe5bdfb..9601aec3 100644 --- a/src/main/java/leets/weeth/domain/user/dto/UserDTO.java +++ b/src/main/java/leets/weeth/domain/user/dto/UserDTO.java @@ -3,13 +3,11 @@ import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; -import leets.weeth.domain.user.annotation.CheckPassword; import leets.weeth.domain.user.entity.enums.Department; import leets.weeth.domain.user.entity.enums.Position; public class UserDTO { - @CheckPassword public record SignUp ( @NotBlank String name, @Email @NotBlank String email, diff --git a/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java b/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java deleted file mode 100644 index 4f4a49e2..00000000 --- a/src/main/java/leets/weeth/domain/user/validator/PasswordValidator.java +++ /dev/null @@ -1,19 +0,0 @@ -package leets.weeth.domain.user.validator; - -import jakarta.validation.ConstraintValidator; -import jakarta.validation.ConstraintValidatorContext; -import leets.weeth.domain.user.annotation.CheckPassword; -import leets.weeth.domain.user.dto.UserDTO; - -public class PasswordValidator implements ConstraintValidator { - - @Override - public void initialize(CheckPassword constraintAnnotation) { - ConstraintValidator.super.initialize(constraintAnnotation); - } - - @Override - public boolean isValid(UserDTO.SignUp dto, ConstraintValidatorContext context) { - return dto.password().equals(dto.passwordConfirm()); - } -} diff --git a/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java b/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java index 2fcf16a4..9619fc94 100644 --- a/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java +++ b/src/main/java/leets/weeth/global/common/error/CommonExceptionController.java @@ -5,7 +5,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.validation.BindException; import org.springframework.web.ErrorResponse; -import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -28,13 +27,6 @@ public CommonResponse handle(Exception ex) { return CommonResponse.createFailure(status, ex.getMessage()); } - @ExceptionHandler(MethodArgumentNotValidException.class) // BindException == @ModelAttribute 어노테이션으로 받은 파라미터의 @Valid 통해 발생한 Exception - public CommonResponse handle(MethodArgumentNotValidException e) { // 클라이언트의 오류일 경우 - int status = 400; // 파라미터 값 실수이므로 4XX - log.error("Error", e); - return CommonResponse.createFailure(status, e.getAllErrors().get(0).getDefaultMessage()); // 디폴트 메세지 가져오기 - } - @ExceptionHandler(BindException.class) // BindException == @ModelAttribute 어노테이션으로 받은 파라미터의 @Valid 통해 발생한 Exception public CommonResponse handle(BindException e) { // 클라이언트의 오류일 경우 int status = 400; // 파라미터 값 실수이므로 4XX