From 628b219a3d63ac1968767c609696403d7b2a7747 Mon Sep 17 00:00:00 2001 From: seongjunnoh Date: Sun, 18 Aug 2024 16:46:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Hotfix=20:=20=EB=A1=9C=EC=BB=AC=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EC=84=B1=EA=B3=B5=20=EC=8B=9C=20userId=20?= =?UTF-8?q?=EA=B0=92=20response=EC=97=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 9 ++-- .../space_spring/dto/user/PostLoginDto.java | 42 +++++++++++++++++++ .../space_spring/service/UserService.java | 12 ++++-- 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/main/java/space/space_spring/dto/user/PostLoginDto.java diff --git a/src/main/java/space/space_spring/controller/UserController.java b/src/main/java/space/space_spring/controller/UserController.java index 9de56efe..dd2d405e 100644 --- a/src/main/java/space/space_spring/controller/UserController.java +++ b/src/main/java/space/space_spring/controller/UserController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*; import space.space_spring.dto.user.GetUserProfileListDto; import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; +import space.space_spring.dto.user.PostLoginDto; import space.space_spring.dto.user.request.PostUserLoginRequest; import space.space_spring.dto.user.request.PostUserSignupRequest; import space.space_spring.dto.user.response.GetSpaceInfoForUserResponse; @@ -49,15 +50,15 @@ public BaseResponse signup(@Validated @RequestBody PostUserSignupRequest * 로그인 */ @PostMapping("/login") - public BaseResponse login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult, HttpServletResponse response) { + public BaseResponse login(@Validated @RequestBody PostLoginDto.Request request, BindingResult bindingResult, HttpServletResponse response) { if (bindingResult.hasErrors()) { throw new CustomException(INVALID_USER_LOGIN, getErrorMessage(bindingResult)); } - String jwtLogin = userService.login(postUserLoginRequest); - response.setHeader("Authorization", "Bearer " + jwtLogin); + PostLoginDto login = userService.login(request); + response.setHeader("Authorization", "Bearer " + login.getJwt()); - return new BaseResponse<>("로컬 로그인 성공"); + return new BaseResponse<>(new PostLoginDto.Response(login.getUserId())); } /** diff --git a/src/main/java/space/space_spring/dto/user/PostLoginDto.java b/src/main/java/space/space_spring/dto/user/PostLoginDto.java new file mode 100644 index 00000000..ca2f6068 --- /dev/null +++ b/src/main/java/space/space_spring/dto/user/PostLoginDto.java @@ -0,0 +1,42 @@ +package space.space_spring.dto.user; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class PostLoginDto { + + private String jwt; + + private Long userId; + + @Getter + @Setter + @NoArgsConstructor + public static class Request { + // '@', '.' 이 있어야 함 + @Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$", message = "이메일 형식에 맞지 않습니다.") + @NotBlank + private String email; + + @Pattern( + regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$", + message = "8~16글자의 영문 대/소문자, 숫자, 특수문자가 포함되어야 합니다." + ) + @NotBlank + private String password; + } + + @Getter + @AllArgsConstructor + public static class Response { + + private Long userId; + } +} diff --git a/src/main/java/space/space_spring/service/UserService.java b/src/main/java/space/space_spring/service/UserService.java index ef2790d4..95439c75 100644 --- a/src/main/java/space/space_spring/service/UserService.java +++ b/src/main/java/space/space_spring/service/UserService.java @@ -6,6 +6,7 @@ import org.springframework.transaction.annotation.Transactional; import space.space_spring.dao.UserSpaceDao; import space.space_spring.dto.user.GetUserProfileListDto; +import space.space_spring.dto.user.PostLoginDto; import space.space_spring.dto.user.dto.SpaceChoiceViewDto; import space.space_spring.dto.user.request.PostUserLoginRequest; import space.space_spring.dto.user.request.PostUserSignupRequest; @@ -59,19 +60,22 @@ private void validateEmailForLocalSignup(String email) { } @Transactional - public String login(PostUserLoginRequest postUserLoginRequest) { + public PostLoginDto login(PostLoginDto.Request request) { // TODO 1. 이메일 존재 여부 확인(아이디 존재 여부 확인) - User userByEmail = userUtils.findUserByEmail(postUserLoginRequest.getEmail(), LOCAL); + User userByEmail = userUtils.findUserByEmail(request.getEmail(), LOCAL); log.info("userByEmail.getUserId: {}", userByEmail.getUserId()); // TODO 2. 비밀번호 일치 여부 확인 - validatePassword(userByEmail, postUserLoginRequest.getPassword()); + validatePassword(userByEmail, request.getPassword()); // TODO 3. JWT 발급 String jwtLogin = jwtLoginProvider.generateToken(userByEmail); log.info("jwtLogin: {}", jwtLogin); - return jwtLogin; + return new PostLoginDto( + jwtLogin, + userByEmail.getUserId() + ); } private void validatePassword(User userByEmail, String password) { From b3ba6f8eae2ee38acf0ffd597da9c79bbcf996ae Mon Sep 17 00:00:00 2001 From: seongjunnoh Date: Sun, 18 Aug 2024 17:05:04 +0900 Subject: [PATCH 2/3] =?UTF-8?q?hotfix=20:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=84=B1=EA=B3=B5=20=EC=8B=9C=20?= =?UTF-8?q?userId=20=EA=B0=92=20response=EC=97=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/space/space_spring/controller/OAuthController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/controller/OAuthController.java b/src/main/java/space/space_spring/controller/OAuthController.java index de4da080..814a2e1f 100644 --- a/src/main/java/space/space_spring/controller/OAuthController.java +++ b/src/main/java/space/space_spring/controller/OAuthController.java @@ -35,7 +35,7 @@ public class OAuthController { * 유저가 카카오 로그인 동의 시 호출될 콜백 함수 */ @GetMapping("/callback/kakao") - public BaseResponse kakaoCallback(@RequestParam(name = "code") String code, HttpServletResponse response) { + public BaseResponse kakaoCallback(@RequestParam(name = "code") String code, HttpServletResponse response) { // TODO 1. 인가코드 받기 // 카카오 인증 서버는 서비스 서버의 Redirect URI로 인가 코드를 전달함 @@ -70,6 +70,6 @@ public BaseResponse kakaoCallback(@RequestParam(name = "code") String co response.setHeader("Authorization", "Bearer " + jwtOAuthLogin); log.info("jwtOAuthLogin = {}", jwtOAuthLogin); - return new BaseResponse<>("카카오 로그인 성공"); + return new BaseResponse<>(userByOAuthInfo.getUserId()); } } From 5612a38d15c6f18a9825afe760ba765955cebf12 Mon Sep 17 00:00:00 2001 From: seongjunnoh Date: Sun, 18 Aug 2024 17:39:29 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor=20:=20=EC=86=8C=EC=85=9C=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20response=20dto=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space_spring/controller/OAuthController.java | 5 +++-- .../space_spring/dto/oAuth/OAuthLoginResponse.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/main/java/space/space_spring/dto/oAuth/OAuthLoginResponse.java diff --git a/src/main/java/space/space_spring/controller/OAuthController.java b/src/main/java/space/space_spring/controller/OAuthController.java index 814a2e1f..591a9ce4 100644 --- a/src/main/java/space/space_spring/controller/OAuthController.java +++ b/src/main/java/space/space_spring/controller/OAuthController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import space.space_spring.dto.oAuth.KakaoInfo; +import space.space_spring.dto.oAuth.OAuthLoginResponse; import space.space_spring.entity.User; import space.space_spring.response.BaseResponse; import space.space_spring.service.OAuthService; @@ -35,7 +36,7 @@ public class OAuthController { * 유저가 카카오 로그인 동의 시 호출될 콜백 함수 */ @GetMapping("/callback/kakao") - public BaseResponse kakaoCallback(@RequestParam(name = "code") String code, HttpServletResponse response) { + public BaseResponse kakaoCallback(@RequestParam(name = "code") String code, HttpServletResponse response) { // TODO 1. 인가코드 받기 // 카카오 인증 서버는 서비스 서버의 Redirect URI로 인가 코드를 전달함 @@ -70,6 +71,6 @@ public BaseResponse kakaoCallback(@RequestParam(name = "code") String code response.setHeader("Authorization", "Bearer " + jwtOAuthLogin); log.info("jwtOAuthLogin = {}", jwtOAuthLogin); - return new BaseResponse<>(userByOAuthInfo.getUserId()); + return new BaseResponse<>(new OAuthLoginResponse(userByOAuthInfo.getUserId())); } } diff --git a/src/main/java/space/space_spring/dto/oAuth/OAuthLoginResponse.java b/src/main/java/space/space_spring/dto/oAuth/OAuthLoginResponse.java new file mode 100644 index 00000000..751d4183 --- /dev/null +++ b/src/main/java/space/space_spring/dto/oAuth/OAuthLoginResponse.java @@ -0,0 +1,13 @@ +package space.space_spring.dto.oAuth; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class OAuthLoginResponse { + + private Long userId; +}