From 7a1e9e2df6ebcac4433a95dcedb808ffb135b338 Mon Sep 17 00:00:00 2001 From: gunsight1 Date: Thu, 22 Feb 2024 19:00:28 +0900 Subject: [PATCH 1/6] =?UTF-8?q?*=20=EC=8B=A4=EC=A0=9C=20=ED=86=A0=ED=81=B0?= =?UTF-8?q?=EA=B0=92=20=EB=B0=9B=EA=B8=B0=20=EC=A0=84=201=EC=B0=A8=20?= =?UTF-8?q?=EC=9E=91=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/controller/MemberController.java | 11 +++ .../kernel360/member/dto/KakaoUserDto.java | 22 +++++ .../kernel360/member/enumset/AgeForKakao.java | 24 ++++++ .../member/enumset/GenderForKakao.java | 21 +++++ .../member/service/KakaoRequest.java | 86 +++++++++++++++++++ .../member/service/MemberService.java | 30 ++++++- .../auth/controller/AuthControllerTest.java | 4 + 7 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java create mode 100644 module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java create mode 100644 module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java create mode 100644 module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java diff --git a/module-api/src/main/java/com/kernel360/member/controller/MemberController.java b/module-api/src/main/java/com/kernel360/member/controller/MemberController.java index d23820fb..c2682b85 100644 --- a/module-api/src/main/java/com/kernel360/member/controller/MemberController.java +++ b/module-api/src/main/java/com/kernel360/member/controller/MemberController.java @@ -1,6 +1,7 @@ package com.kernel360.member.controller; +import com.fasterxml.jackson.core.JsonProcessingException; import com.kernel360.carinfo.entity.CarInfo; import com.kernel360.exception.BusinessException; import com.kernel360.member.code.MemberBusinessCode; @@ -86,4 +87,14 @@ public ResponseEntity> sendMemberIdByEmail(@RequestBody Find return ApiResponse.toResponseEntity( MemberBusinessCode.SUCCESS_REQUEST_FIND_MEMBER_ID); // 아이디는 이메일로 보내고, 외부 노출 X } + + @GetMapping("/login/forKakao") + public ResponseEntity> loginForKakao(String code) throws JsonProcessingException { + + MemberDto memberInfo = memberService.loginForKakao(code); + + //부가정보가 입력 되어있는가 > 차량정보, 세차정보, boolean (감싸서 보내든 말든 노상관) + + return ApiResponse.toResponseEntity(SUCCESS_REQUEST_LOGIN_MEMBER, memberInfo); + } } diff --git a/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java b/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java new file mode 100644 index 00000000..e614f39b --- /dev/null +++ b/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java @@ -0,0 +1,22 @@ +package com.kernel360.member.dto; + +public record KakaoUserDto( + String id, + String email, + int age, + int gender +) { + public static KakaoUserDto of( + String id, + String email, + int age, + int gender + ){ + return new KakaoUserDto( + id, + email, + age, + gender + ); + } +} diff --git a/module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java b/module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java new file mode 100644 index 00000000..005a38c8 --- /dev/null +++ b/module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java @@ -0,0 +1,24 @@ +package com.kernel360.member.enumset; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public enum AgeForKakao { + TWENTY("20~29", 40), + THIRTY("30~39", 30), + FORTY("40~49", 40), + FIFTY("50~59", 50), + SIXTY("60~69", 60); + + private final String avg; + private final int value; + + public static AgeForKakao fromString(String text) { + for (AgeForKakao value : AgeForKakao.values()) { + if (value.avg.equalsIgnoreCase(text)) { + return value; + } + } + throw new IllegalArgumentException("No constant with text " + text + " found"); + } +} diff --git a/module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java b/module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java new file mode 100644 index 00000000..fa09c0c0 --- /dev/null +++ b/module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java @@ -0,0 +1,21 @@ +package com.kernel360.member.enumset; + +import com.kernel360.exception.BusinessException; +import com.kernel360.member.code.MemberErrorCode; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public enum GenderForKakao { + male(0), + female(1); + + private final int value; + public static String ordinalToName(int value) { + for (GenderForKakao gender : values()) { + if (gender.ordinal() == value) { + return gender.name(); + } + } + throw new BusinessException(MemberErrorCode.FAILED_NOT_MAPPING_ORDINAL_TO_NAME); + } +} diff --git a/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java b/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java new file mode 100644 index 00000000..93554aaa --- /dev/null +++ b/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java @@ -0,0 +1,86 @@ +package com.kernel360.member.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.kernel360.member.dto.KakaoUserDto; +import com.kernel360.member.enumset.AgeForKakao; +import com.kernel360.member.enumset.GenderForKakao; +import com.kernel360.washzone.dto.KakaoMapDto; +import org.springframework.http.*; +import org.springframework.stereotype.Component; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class KakaoRequest { + + public String getKakaoTokenByAuthcode(String authorizeCode) { + + String url = "https://kauth.kakao.com/oauth/token"; + + RestTemplate restTemplate = new RestTemplate(); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + headers.add("Accept", "application/json"); + headers.set("charset", "utf-8"); + + MultiValueMap params = new LinkedMultiValueMap<>(); + params.add("grant_type", "authorization_code"); + params.add("client_id", "416fb028180bc7dfd9412cfd7be160bc"); + params.add("redirect_uri", "http://10.230.120.21:8080"); + params.add("client_secret", "c5F0SbaddaJVUsj5e6nmNd7ExnuZO5mN"); + params.add("code", authorizeCode); + + HttpEntity> request = new HttpEntity<>(params, headers); + + ResponseEntity responseEntity = restTemplate.postForEntity(url, request, String.class); + + System.err.println(" access_token >>>>>>>>> " + responseEntity.getBody()); + + return responseEntity.getBody(); + } + + + public KakaoUserDto getKakaoUserByToken(String accessToken) { + + String url = "https://kapi.kakao.com/v2/user/me"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + headers.set("Authorization", "Bearer "+accessToken); + headers.set("charset", "utf-8"); + + RestTemplate restTemplate = new RestTemplate(); + + System.err.println("URL :: " + url); + System.err.println("headers :: " + headers); + + ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); + + ObjectMapper mapper = new ObjectMapper(); + Map kakaoResponse = new HashMap<>(); + + try { + kakaoResponse = mapper.readValue(responseEntity.getBody(), HashMap.class); + }catch (Exception e){ + System.err.println("Exception : " + e); + } + Map kakaoAccount = mapper.convertValue(kakaoResponse.get("kakao_account"), HashMap.class); + + KakaoUserDto dto = KakaoUserDto.of( + kakaoResponse.get("id").toString(), + kakaoAccount.get("email").toString(), + AgeForKakao.valueOf(kakaoAccount.get("age_range").toString()).ordinal(), + GenderForKakao.valueOf(kakaoAccount.get("gender").toString()).ordinal() + ); + + System.out.println("Response Body : " + dto); + + return dto; + } + + +} diff --git a/module-api/src/main/java/com/kernel360/member/service/MemberService.java b/module-api/src/main/java/com/kernel360/member/service/MemberService.java index 8bfff52f..db282524 100644 --- a/module-api/src/main/java/com/kernel360/member/service/MemberService.java +++ b/module-api/src/main/java/com/kernel360/member/service/MemberService.java @@ -6,10 +6,7 @@ import com.kernel360.commoncode.service.CommonCodeService; import com.kernel360.exception.BusinessException; import com.kernel360.member.code.MemberErrorCode; -import com.kernel360.member.dto.CarInfoDto; -import com.kernel360.member.dto.MemberDto; -import com.kernel360.member.dto.MemberInfo; -import com.kernel360.member.dto.WashInfoDto; +import com.kernel360.member.dto.*; import com.kernel360.member.entity.Member; import com.kernel360.member.enumset.Age; import com.kernel360.member.enumset.Gender; @@ -39,6 +36,7 @@ public class MemberService { private final CommonCodeService commonCodeService; private final CarInfoRepository carInfoRepository; private final WashInfoRepository washInfoRepository; + private final KakaoRequest kakaoRequest; @Transactional public void joinMember(MemberDto requestDto) { @@ -182,4 +180,28 @@ public MemberDto findByEmail(String email) { return MemberDto.from(member); } + + @Transactional + public MemberDto loginForKakao(String accessToken) { + + //TODO 카카오로 API 발신 + KakaoUserDto kakaoUser = kakaoRequest.getKakaoUserByToken(accessToken); + + //TODO entity에 바인딩, gender와 age_range를 우리쪽에 맞게 변경 + Member kakao = Member.createJoinMember(kakaoUser.id(), kakaoUser.email(), "", kakaoUser.age(), kakaoUser.gender()); + + MemberDto dto = MemberDto.from(memberRepository.findOneById(kakao.getId())); + + //TODO ID는 실제 결과 값을 보고 해싱할지 그냥쓸지 결정. + if(Objects.isNull(dto)){ + memberRepository.save(kakao); + dto.from(kakao); + } + + String loginToken = jwt.generateToken(dto.id()); + + authService.saveAuthByMember(kakao.getMemberNo(), ConvertSHA256.convertToSHA256(loginToken)); + + return dto; + } } diff --git a/module-api/src/test/java/com/kernel360/auth/controller/AuthControllerTest.java b/module-api/src/test/java/com/kernel360/auth/controller/AuthControllerTest.java index aaf111b7..5616379f 100644 --- a/module-api/src/test/java/com/kernel360/auth/controller/AuthControllerTest.java +++ b/module-api/src/test/java/com/kernel360/auth/controller/AuthControllerTest.java @@ -8,6 +8,8 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.restdocs.payload.JsonFieldType; +import static com.kernel360.common.utils.RestDocumentUtils.getDocumentRequest; +import static com.kernel360.common.utils.RestDocumentUtils.getDocumentResponse; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.*; @@ -36,6 +38,8 @@ class AuthControllerTest extends ControllerTest { .andExpect(jsonPath("$.code").value("BAC001")) .andExpect(jsonPath("$.message").value("JWT 토큰 재발급 성공")) .andDo(document("auth/reissuanceJWT", + getDocumentRequest(), + getDocumentResponse(), responseFields( fieldWithPath("status").description("상태 코드"), fieldWithPath("message").description("응답 메시지"), From 3945ae5da9fe0c18216acee40082e4772864815f Mon Sep 17 00:00:00 2001 From: gunsight1 Date: Thu, 22 Feb 2024 20:46:05 +0900 Subject: [PATCH 2/6] =?UTF-8?q?*=20=EC=8B=A4=EC=A0=9C=20=ED=86=A0=ED=81=B0?= =?UTF-8?q?=EA=B0=92=20=EB=B0=9B=EA=B8=B0=20=EC=A0=84=201=EC=B0=A8=20?= =?UTF-8?q?=EC=9E=91=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/controller/MemberController.java | 8 +++----- .../java/com/kernel360/member/dto/MemberDto.java | 16 ++++++++++++++++ .../kernel360/member/service/MemberService.java | 9 +++------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/module-api/src/main/java/com/kernel360/member/controller/MemberController.java b/module-api/src/main/java/com/kernel360/member/controller/MemberController.java index c2682b85..a558e062 100644 --- a/module-api/src/main/java/com/kernel360/member/controller/MemberController.java +++ b/module-api/src/main/java/com/kernel360/member/controller/MemberController.java @@ -89,12 +89,10 @@ public ResponseEntity> sendMemberIdByEmail(@RequestBody Find } @GetMapping("/login/forKakao") - public ResponseEntity> loginForKakao(String code) throws JsonProcessingException { + public ResponseEntity> loginForKakao(String code) { - MemberDto memberInfo = memberService.loginForKakao(code); + MemberDto member = memberService.loginForKakao(code); - //부가정보가 입력 되어있는가 > 차량정보, 세차정보, boolean (감싸서 보내든 말든 노상관) - - return ApiResponse.toResponseEntity(SUCCESS_REQUEST_LOGIN_MEMBER, memberInfo); + return ApiResponse.toResponseEntity(SUCCESS_REQUEST_LOGIN_MEMBER, member); } } diff --git a/module-api/src/main/java/com/kernel360/member/dto/MemberDto.java b/module-api/src/main/java/com/kernel360/member/dto/MemberDto.java index a580385f..7f7bee9e 100644 --- a/module-api/src/main/java/com/kernel360/member/dto/MemberDto.java +++ b/module-api/src/main/java/com/kernel360/member/dto/MemberDto.java @@ -66,6 +66,22 @@ public static MemberDto from(Member entity) { ); } + public static MemberDto fromKakao(MemberDto dto, String token) { + return MemberDto.of( + dto.memberNo(), + dto.id(), + dto.email(), + dto.password(), + dto.gender(), + dto.age(), + dto.createdAt(), + dto.createdBy(), + dto.modifiedAt(), + dto.modifiedBy(), + token + ); + } + public Member toEntity() { return Member.of( this.memberNo(), diff --git a/module-api/src/main/java/com/kernel360/member/service/MemberService.java b/module-api/src/main/java/com/kernel360/member/service/MemberService.java index db282524..04e998d8 100644 --- a/module-api/src/main/java/com/kernel360/member/service/MemberService.java +++ b/module-api/src/main/java/com/kernel360/member/service/MemberService.java @@ -193,15 +193,12 @@ public MemberDto loginForKakao(String accessToken) { MemberDto dto = MemberDto.from(memberRepository.findOneById(kakao.getId())); //TODO ID는 실제 결과 값을 보고 해싱할지 그냥쓸지 결정. - if(Objects.isNull(dto)){ - memberRepository.save(kakao); - dto.from(kakao); - } + if(Objects.isNull(dto)){ dto.from(memberRepository.save(kakao)); } String loginToken = jwt.generateToken(dto.id()); - authService.saveAuthByMember(kakao.getMemberNo(), ConvertSHA256.convertToSHA256(loginToken)); + authService.saveAuthByMember(dto.memberNo(), ConvertSHA256.convertToSHA256(loginToken)); - return dto; + return MemberDto.fromKakao(dto, loginToken); } } From 2332b67647505a44ea56fb3d5da4e775065ece3f Mon Sep 17 00:00:00 2001 From: gunsight1 Date: Fri, 23 Feb 2024 21:30:23 +0900 Subject: [PATCH 3/6] =?UTF-8?q?*=20=EC=8B=A4=EC=A0=9C=20=ED=86=A0=ED=81=B0?= =?UTF-8?q?=20=EB=B0=9B=EC=9D=80=20=ED=9B=84=20=EA=B0=80=EC=9E=85=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/controller/MemberController.java | 4 +- .../kernel360/member/dto/KakaoUserDto.java | 12 ++--- .../member/service/KakaoRequest.java | 46 +++---------------- .../member/service/MemberService.java | 21 ++++----- .../com/kernel360/member/entity/Member.java | 6 ++- 5 files changed, 27 insertions(+), 62 deletions(-) diff --git a/module-api/src/main/java/com/kernel360/member/controller/MemberController.java b/module-api/src/main/java/com/kernel360/member/controller/MemberController.java index a558e062..77037d86 100644 --- a/module-api/src/main/java/com/kernel360/member/controller/MemberController.java +++ b/module-api/src/main/java/com/kernel360/member/controller/MemberController.java @@ -89,9 +89,9 @@ public ResponseEntity> sendMemberIdByEmail(@RequestBody Find } @GetMapping("/login/forKakao") - public ResponseEntity> loginForKakao(String code) { + public ResponseEntity> loginForKakao(@RequestHeader("Authorization") String accessToken) { - MemberDto member = memberService.loginForKakao(code); + MemberDto member = memberService.loginForKakao(accessToken); return ApiResponse.toResponseEntity(SUCCESS_REQUEST_LOGIN_MEMBER, member); } diff --git a/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java b/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java index e614f39b..c9c76c33 100644 --- a/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java +++ b/module-api/src/main/java/com/kernel360/member/dto/KakaoUserDto.java @@ -2,21 +2,15 @@ public record KakaoUserDto( String id, - String email, - int age, - int gender + String email ) { public static KakaoUserDto of( String id, - String email, - int age, - int gender + String email ){ return new KakaoUserDto( id, - email, - age, - gender + email ); } } diff --git a/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java b/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java index 93554aaa..b48add89 100644 --- a/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java +++ b/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java @@ -17,48 +17,23 @@ @Component public class KakaoRequest { - public String getKakaoTokenByAuthcode(String authorizeCode) { - - String url = "https://kauth.kakao.com/oauth/token"; - - RestTemplate restTemplate = new RestTemplate(); - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.add("Accept", "application/json"); - headers.set("charset", "utf-8"); - - MultiValueMap params = new LinkedMultiValueMap<>(); - params.add("grant_type", "authorization_code"); - params.add("client_id", "416fb028180bc7dfd9412cfd7be160bc"); - params.add("redirect_uri", "http://10.230.120.21:8080"); - params.add("client_secret", "c5F0SbaddaJVUsj5e6nmNd7ExnuZO5mN"); - params.add("code", authorizeCode); - - HttpEntity> request = new HttpEntity<>(params, headers); - - ResponseEntity responseEntity = restTemplate.postForEntity(url, request, String.class); - - System.err.println(" access_token >>>>>>>>> " + responseEntity.getBody()); - - return responseEntity.getBody(); - } - - public KakaoUserDto getKakaoUserByToken(String accessToken) { String url = "https://kapi.kakao.com/v2/user/me"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.set("Authorization", "Bearer "+accessToken); headers.set("charset", "utf-8"); + HttpEntity requestEntity = new HttpEntity<>(headers); + RestTemplate restTemplate = new RestTemplate(); - System.err.println("URL :: " + url); - System.err.println("headers :: " + headers); + ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); - ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class); + System.err.println("response :: " + responseEntity); ObjectMapper mapper = new ObjectMapper(); Map kakaoResponse = new HashMap<>(); @@ -70,14 +45,7 @@ public KakaoUserDto getKakaoUserByToken(String accessToken) { } Map kakaoAccount = mapper.convertValue(kakaoResponse.get("kakao_account"), HashMap.class); - KakaoUserDto dto = KakaoUserDto.of( - kakaoResponse.get("id").toString(), - kakaoAccount.get("email").toString(), - AgeForKakao.valueOf(kakaoAccount.get("age_range").toString()).ordinal(), - GenderForKakao.valueOf(kakaoAccount.get("gender").toString()).ordinal() - ); - - System.out.println("Response Body : " + dto); + KakaoUserDto dto = KakaoUserDto.of(kakaoResponse.get("id").toString(),kakaoAccount.get("email").toString()); return dto; } diff --git a/module-api/src/main/java/com/kernel360/member/service/MemberService.java b/module-api/src/main/java/com/kernel360/member/service/MemberService.java index 04e998d8..af63a98c 100644 --- a/module-api/src/main/java/com/kernel360/member/service/MemberService.java +++ b/module-api/src/main/java/com/kernel360/member/service/MemberService.java @@ -1,5 +1,6 @@ package com.kernel360.member.service; +import com.kernel360.auth.entity.Auth; import com.kernel360.auth.service.AuthService; import com.kernel360.carinfo.entity.CarInfo; import com.kernel360.carinfo.repository.CarInfoRepository; @@ -23,6 +24,7 @@ import java.util.Map; import java.util.Objects; +import java.util.Optional; @Slf4j @@ -184,21 +186,18 @@ public MemberDto findByEmail(String email) { @Transactional public MemberDto loginForKakao(String accessToken) { - //TODO 카카오로 API 발신 KakaoUserDto kakaoUser = kakaoRequest.getKakaoUserByToken(accessToken); + System.err.println("kakaoUser >>>> " + kakaoUser); + if(Objects.isNull(memberRepository.findOneById(kakaoUser.id()))){ + memberRepository.save(Member.createForKakao(kakaoUser.id(), kakaoUser.email(), "kakao", 0, 0)); + } - //TODO entity에 바인딩, gender와 age_range를 우리쪽에 맞게 변경 - Member kakao = Member.createJoinMember(kakaoUser.id(), kakaoUser.email(), "", kakaoUser.age(), kakaoUser.gender()); - - MemberDto dto = MemberDto.from(memberRepository.findOneById(kakao.getId())); - - //TODO ID는 실제 결과 값을 보고 해싱할지 그냥쓸지 결정. - if(Objects.isNull(dto)){ dto.from(memberRepository.save(kakao)); } + MemberDto memberDto = MemberDto.from(memberRepository.findOneById(kakaoUser.id())); - String loginToken = jwt.generateToken(dto.id()); + String loginToken = jwt.generateToken(memberDto.id()); - authService.saveAuthByMember(dto.memberNo(), ConvertSHA256.convertToSHA256(loginToken)); + authService.saveAuthByMember(memberDto.memberNo(), ConvertSHA256.convertToSHA256(loginToken)); - return MemberDto.fromKakao(dto, loginToken); + return MemberDto.fromKakao(memberDto, loginToken); } } diff --git a/module-domain/src/main/java/com/kernel360/member/entity/Member.java b/module-domain/src/main/java/com/kernel360/member/entity/Member.java index 44866933..08fd49cc 100644 --- a/module-domain/src/main/java/com/kernel360/member/entity/Member.java +++ b/module-domain/src/main/java/com/kernel360/member/entity/Member.java @@ -41,7 +41,6 @@ public class Member extends BaseEntity { @Column(name = "age") private int age; - public static Member of(Long memberNo, String id, String email, String password, int gender, int age) { return new Member(memberNo, id, email, password, gender, age); @@ -125,4 +124,9 @@ public void updateCarInfo(CarInfo carInfo) { this.carInfo = carInfo; } + public static Member createForKakao(String id, String email, String password, int gender, int age) { + + return new Member(id, email, password, gender, age); + } + } \ No newline at end of file From e124698574188a3551bdd69d9141c8811c93035a Mon Sep 17 00:00:00 2001 From: gunsight1 Date: Sun, 25 Feb 2024 14:33:58 +0900 Subject: [PATCH 4/6] =?UTF-8?q?*=20=EA=B0=80=EC=9E=85=20=EB=B0=8F=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C,=20=EC=84=B1=EB=B3=84=20=EB=82=98=EC=9D=B4=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=ED=95=84=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/code/MemberBusinessCode.java | 3 ++- .../member/code/MemberErrorCode.java | 3 ++- .../kernel360/member/enumset/AgeForKakao.java | 24 ------------------- .../member/enumset/GenderForKakao.java | 21 ---------------- .../member/service/KakaoRequest.java | 15 ++++-------- .../member/service/MemberService.java | 3 +-- .../com/kernel360/member/entity/Member.java | 13 ++++++++-- 7 files changed, 20 insertions(+), 62 deletions(-) delete mode 100644 module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java delete mode 100644 module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java diff --git a/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java b/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java index c0c8cb41..a62518f8 100644 --- a/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java +++ b/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java @@ -17,7 +17,8 @@ public enum MemberBusinessCode implements BusinessCode { SUCCESS_VALIDATE_PASSWORD_MEMBER(HttpStatus.OK.value(), "BMC008", "비밀번호가 확인 되었습니다."), SUCCESS_REQUEST_UPDATE_WASH_INFO_MEMBER(HttpStatus.OK.value(), "BMC009", "WashInfo 정보가 변경 되었습니다."), SUCCESS_REQUEST_UPDATE_CAR_INFO_MEMBER(HttpStatus.OK.value(), "BMC010", "CarInfo 정보가 변경 되었습니다."), - SUCCESS_REQUEST_FIND_MEMBER_ID(HttpStatus.OK.value(), "BMC011","회원 아이디 찾기 메일이 발송되었습니다."); + SUCCESS_REQUEST_FIND_MEMBER_ID(HttpStatus.OK.value(), "BMC011","회원 아이디 찾기 메일이 발송되었습니다."), + SUCCESS_REQUEST_LOGIN_MEMBER_KAKAO(HttpStatus.OK.value(), "BMC012", "로그인 성공"); private final int status; diff --git a/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java b/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java index 2aecc075..8a02a7cd 100644 --- a/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java +++ b/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java @@ -15,7 +15,8 @@ public enum MemberErrorCode implements ErrorCode { FAILED_GENERATE_JOIN_MEMBER_INFO(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMC004", "회원가입에 필요한 정보 생성 실패"), FAILED_GENERATE_LOGIN_REQUEST_INFO(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMC005", "정보 불일치로 인한 로그인 정보 생성 실패"), FAILED_REQUEST_LOGIN(HttpStatus.BAD_REQUEST.value(), "EMC006", "정보 불일치로 인한 로그인 실패"), - FAILED_FIND_MEMBER_INFO(HttpStatus.BAD_REQUEST.value(), "EMC007", "요청 회원정보가 존재하지 않습니다."); + FAILED_FIND_MEMBER_INFO(HttpStatus.BAD_REQUEST.value(), "EMC007", "요청 회원정보가 존재하지 않습니다."), + FAILED_REQUEST_LOGIN_FOR_KAKAO(HttpStatus.BAD_REQUEST.value(), "EMC008", "카카오 로그인 정보를 찾을 수 없습니다."); private final int status; diff --git a/module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java b/module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java deleted file mode 100644 index 005a38c8..00000000 --- a/module-api/src/main/java/com/kernel360/member/enumset/AgeForKakao.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.kernel360.member.enumset; - -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -public enum AgeForKakao { - TWENTY("20~29", 40), - THIRTY("30~39", 30), - FORTY("40~49", 40), - FIFTY("50~59", 50), - SIXTY("60~69", 60); - - private final String avg; - private final int value; - - public static AgeForKakao fromString(String text) { - for (AgeForKakao value : AgeForKakao.values()) { - if (value.avg.equalsIgnoreCase(text)) { - return value; - } - } - throw new IllegalArgumentException("No constant with text " + text + " found"); - } -} diff --git a/module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java b/module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java deleted file mode 100644 index fa09c0c0..00000000 --- a/module-api/src/main/java/com/kernel360/member/enumset/GenderForKakao.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.kernel360.member.enumset; - -import com.kernel360.exception.BusinessException; -import com.kernel360.member.code.MemberErrorCode; -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -public enum GenderForKakao { - male(0), - female(1); - - private final int value; - public static String ordinalToName(int value) { - for (GenderForKakao gender : values()) { - if (gender.ordinal() == value) { - return gender.name(); - } - } - throw new BusinessException(MemberErrorCode.FAILED_NOT_MAPPING_ORDINAL_TO_NAME); - } -} diff --git a/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java b/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java index b48add89..15164ff7 100644 --- a/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java +++ b/module-api/src/main/java/com/kernel360/member/service/KakaoRequest.java @@ -1,14 +1,11 @@ package com.kernel360.member.service; import com.fasterxml.jackson.databind.ObjectMapper; +import com.kernel360.exception.BusinessException; +import com.kernel360.member.code.MemberErrorCode; import com.kernel360.member.dto.KakaoUserDto; -import com.kernel360.member.enumset.AgeForKakao; -import com.kernel360.member.enumset.GenderForKakao; -import com.kernel360.washzone.dto.KakaoMapDto; import org.springframework.http.*; import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.HashMap; @@ -28,20 +25,16 @@ public KakaoUserDto getKakaoUserByToken(String accessToken) { headers.set("charset", "utf-8"); HttpEntity requestEntity = new HttpEntity<>(headers); - RestTemplate restTemplate = new RestTemplate(); - ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); - System.err.println("response :: " + responseEntity); - ObjectMapper mapper = new ObjectMapper(); - Map kakaoResponse = new HashMap<>(); + Map kakaoResponse; try { kakaoResponse = mapper.readValue(responseEntity.getBody(), HashMap.class); }catch (Exception e){ - System.err.println("Exception : " + e); + throw new BusinessException(MemberErrorCode.FAILED_REQUEST_LOGIN_FOR_KAKAO); } Map kakaoAccount = mapper.convertValue(kakaoResponse.get("kakao_account"), HashMap.class); diff --git a/module-api/src/main/java/com/kernel360/member/service/MemberService.java b/module-api/src/main/java/com/kernel360/member/service/MemberService.java index af63a98c..56fd3da1 100644 --- a/module-api/src/main/java/com/kernel360/member/service/MemberService.java +++ b/module-api/src/main/java/com/kernel360/member/service/MemberService.java @@ -187,9 +187,8 @@ public MemberDto findByEmail(String email) { public MemberDto loginForKakao(String accessToken) { KakaoUserDto kakaoUser = kakaoRequest.getKakaoUserByToken(accessToken); - System.err.println("kakaoUser >>>> " + kakaoUser); if(Objects.isNull(memberRepository.findOneById(kakaoUser.id()))){ - memberRepository.save(Member.createForKakao(kakaoUser.id(), kakaoUser.email(), "kakao", 0, 0)); + memberRepository.save(Member.createForKakao(kakaoUser.id(), kakaoUser.email(), "kakao")); } MemberDto memberDto = MemberDto.from(memberRepository.findOneById(kakaoUser.id())); diff --git a/module-domain/src/main/java/com/kernel360/member/entity/Member.java b/module-domain/src/main/java/com/kernel360/member/entity/Member.java index 08fd49cc..48436608 100644 --- a/module-domain/src/main/java/com/kernel360/member/entity/Member.java +++ b/module-domain/src/main/java/com/kernel360/member/entity/Member.java @@ -36,9 +36,12 @@ public class Member extends BaseEntity { private String password; @Column(name = "gender") + @Enumerated(EnumType.STRING) private int gender; + @Column(name = "age") + @Enumerated(EnumType.STRING) private int age; public static Member of(Long memberNo, String id, String email, String password, int gender, int age) { @@ -124,9 +127,15 @@ public void updateCarInfo(CarInfo carInfo) { this.carInfo = carInfo; } - public static Member createForKakao(String id, String email, String password, int gender, int age) { + public static Member createForKakao(String id, String email, String password) { - return new Member(id, email, password, gender, age); + return new Member(id, email, password); + } + + private Member(String id, String email, String password) { + this.id = id; + this.email = email; + this.password = password; } } \ No newline at end of file From 02f966b1ce8847f855d6b715da229da21b7a2e99 Mon Sep 17 00:00:00 2001 From: gunsight1 Date: Sun, 25 Feb 2024 21:05:49 +0900 Subject: [PATCH 5/6] =?UTF-8?q?*=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=201=EC=B0=A8=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/kernel360/member/enumset/Age.java | 3 ++- .../java/com/kernel360/member/enumset/Gender.java | 5 +++-- .../com/kernel360/member/service/MemberService.java | 4 +--- .../member/controller/MemberControllerTest.java | 2 +- .../java/com/kernel360/member/entity/Member.java | 12 ++---------- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/module-api/src/main/java/com/kernel360/member/enumset/Age.java b/module-api/src/main/java/com/kernel360/member/enumset/Age.java index d1af5a2d..239bddc1 100644 --- a/module-api/src/main/java/com/kernel360/member/enumset/Age.java +++ b/module-api/src/main/java/com/kernel360/member/enumset/Age.java @@ -10,7 +10,8 @@ public enum Age { AGE_30(30), AGE_40(40), AGE_50(50), - AGE_60(60); + AGE_60(60), + AGE_99(99); private final int value; diff --git a/module-api/src/main/java/com/kernel360/member/enumset/Gender.java b/module-api/src/main/java/com/kernel360/member/enumset/Gender.java index 0988155f..9ae46a1d 100644 --- a/module-api/src/main/java/com/kernel360/member/enumset/Gender.java +++ b/module-api/src/main/java/com/kernel360/member/enumset/Gender.java @@ -6,8 +6,9 @@ @RequiredArgsConstructor public enum Gender { - man(0), - woman(1); + MALE(0), + FEMALE(1), + OTHERS(99); private final int value; public static String ordinalToName(int value) { diff --git a/module-api/src/main/java/com/kernel360/member/service/MemberService.java b/module-api/src/main/java/com/kernel360/member/service/MemberService.java index 56fd3da1..01ff2206 100644 --- a/module-api/src/main/java/com/kernel360/member/service/MemberService.java +++ b/module-api/src/main/java/com/kernel360/member/service/MemberService.java @@ -1,6 +1,5 @@ package com.kernel360.member.service; -import com.kernel360.auth.entity.Auth; import com.kernel360.auth.service.AuthService; import com.kernel360.carinfo.entity.CarInfo; import com.kernel360.carinfo.repository.CarInfoRepository; @@ -24,7 +23,6 @@ import java.util.Map; import java.util.Objects; -import java.util.Optional; @Slf4j @@ -188,7 +186,7 @@ public MemberDto loginForKakao(String accessToken) { KakaoUserDto kakaoUser = kakaoRequest.getKakaoUserByToken(accessToken); if(Objects.isNull(memberRepository.findOneById(kakaoUser.id()))){ - memberRepository.save(Member.createForKakao(kakaoUser.id(), kakaoUser.email(), "kakao")); + memberRepository.save(Member.createForKakao(kakaoUser.id(), kakaoUser.email(), "kakao", Gender.OTHERS.ordinal(), Age.AGE_99.ordinal())); } MemberDto memberDto = MemberDto.from(memberRepository.findOneById(kakaoUser.id())); diff --git a/module-api/src/test/java/com/kernel360/member/controller/MemberControllerTest.java b/module-api/src/test/java/com/kernel360/member/controller/MemberControllerTest.java index ec7f7466..6ab56c1f 100644 --- a/module-api/src/test/java/com/kernel360/member/controller/MemberControllerTest.java +++ b/module-api/src/test/java/com/kernel360/member/controller/MemberControllerTest.java @@ -21,7 +21,7 @@ class MemberControllerTest extends ControllerTest { void 회원가입요청() throws Exception { /** given 목데이터 세팅 **/ - MemberDto memberDto = MemberDto.of("testID", "gunsight777@naver.com", "testPassword", "man", "30"); + MemberDto memberDto = MemberDto.of("testID", "gunsight777@naver.com", "testPassword", "MALE", "30"); ObjectMapper objectMapper = new ObjectMapper(); String param = objectMapper.writeValueAsString(memberDto); diff --git a/module-domain/src/main/java/com/kernel360/member/entity/Member.java b/module-domain/src/main/java/com/kernel360/member/entity/Member.java index 48436608..99eaf22b 100644 --- a/module-domain/src/main/java/com/kernel360/member/entity/Member.java +++ b/module-domain/src/main/java/com/kernel360/member/entity/Member.java @@ -36,12 +36,9 @@ public class Member extends BaseEntity { private String password; @Column(name = "gender") - @Enumerated(EnumType.STRING) private int gender; - @Column(name = "age") - @Enumerated(EnumType.STRING) private int age; public static Member of(Long memberNo, String id, String email, String password, int gender, int age) { @@ -127,15 +124,10 @@ public void updateCarInfo(CarInfo carInfo) { this.carInfo = carInfo; } - public static Member createForKakao(String id, String email, String password) { + public static Member createForKakao(String id, String email, String password, int gender, int age) { - return new Member(id, email, password); + return new Member(id, email, password, gender, age); } - private Member(String id, String email, String password) { - this.id = id; - this.email = email; - this.password = password; - } } \ No newline at end of file From 9588a52abc1714f9d83c139752583159aed216a8 Mon Sep 17 00:00:00 2001 From: gunsight1 Date: Mon, 26 Feb 2024 10:53:32 +0900 Subject: [PATCH 6/6] =?UTF-8?q?*=20=EA=B9=83=20=EB=A8=B8=EC=A7=80=20?= =?UTF-8?q?=EC=B6=A9=EB=8F=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/code/MemberBusinessCode.java | 5 ++++- .../kernel360/member/code/MemberErrorCode.java | 3 ++- .../kernel360/member/service/MemberService.java | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java b/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java index a62518f8..c8845cc4 100644 --- a/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java +++ b/module-api/src/main/java/com/kernel360/member/code/MemberBusinessCode.java @@ -18,7 +18,10 @@ public enum MemberBusinessCode implements BusinessCode { SUCCESS_REQUEST_UPDATE_WASH_INFO_MEMBER(HttpStatus.OK.value(), "BMC009", "WashInfo 정보가 변경 되었습니다."), SUCCESS_REQUEST_UPDATE_CAR_INFO_MEMBER(HttpStatus.OK.value(), "BMC010", "CarInfo 정보가 변경 되었습니다."), SUCCESS_REQUEST_FIND_MEMBER_ID(HttpStatus.OK.value(), "BMC011","회원 아이디 찾기 메일이 발송되었습니다."), - SUCCESS_REQUEST_LOGIN_MEMBER_KAKAO(HttpStatus.OK.value(), "BMC012", "로그인 성공"); + SUCCESS_REQUEST_SEND_RESET_PASSWORD_EMAIL(HttpStatus.OK.value(), "BMC012", "회원 비밀번호 초기화 메일이 발송되었습니다."), + SUCCESS_REQUEST_RESET_PASSWORD_PAGE(HttpStatus.FOUND.value(), "BMC013", "비밀번호 초기화 토큰이 유효하므로 비밀번호 초기화 페이지로 접근합니다."), + SUCCESS_REQUEST_RESET_PASSWORD(HttpStatus.OK.value(), "BMC014", "비밀번호가 초기화되었습니다."), + SUCCESS_REQUEST_LOGIN_MEMBER_KAKAO(HttpStatus.OK.value(), "BMC015", "로그인 성공"); private final int status; diff --git a/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java b/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java index 8a02a7cd..377517d3 100644 --- a/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java +++ b/module-api/src/main/java/com/kernel360/member/code/MemberErrorCode.java @@ -16,7 +16,8 @@ public enum MemberErrorCode implements ErrorCode { FAILED_GENERATE_LOGIN_REQUEST_INFO(HttpStatus.INTERNAL_SERVER_ERROR.value(), "EMC005", "정보 불일치로 인한 로그인 정보 생성 실패"), FAILED_REQUEST_LOGIN(HttpStatus.BAD_REQUEST.value(), "EMC006", "정보 불일치로 인한 로그인 실패"), FAILED_FIND_MEMBER_INFO(HttpStatus.BAD_REQUEST.value(), "EMC007", "요청 회원정보가 존재하지 않습니다."), - FAILED_REQUEST_LOGIN_FOR_KAKAO(HttpStatus.BAD_REQUEST.value(), "EMC008", "카카오 로그인 정보를 찾을 수 없습니다."); + EXPIRED_PASSWORD_RESET_TOKEN(HttpStatus.NOT_FOUND.value(), "EMC008", "유효하지 않은 비밀번호 초기화 토큰입니다"), + FAILED_REQUEST_LOGIN_FOR_KAKAO(HttpStatus.BAD_REQUEST.value(), "EMC009", "카카오 로그인 정보를 찾을 수 없습니다."); private final int status; diff --git a/module-api/src/main/java/com/kernel360/member/service/MemberService.java b/module-api/src/main/java/com/kernel360/member/service/MemberService.java index 01ff2206..5eef8f33 100644 --- a/module-api/src/main/java/com/kernel360/member/service/MemberService.java +++ b/module-api/src/main/java/com/kernel360/member/service/MemberService.java @@ -181,6 +181,23 @@ public MemberDto findByEmail(String email) { return MemberDto.from(member); } + public MemberDto findByMemberId(String memberId) { + Member member = memberRepository.findOneById(memberId); + if (member == null) { + throw new BusinessException(MemberErrorCode.FAILED_FIND_MEMBER_INFO); + } + + return MemberDto.from(member); + } + @Transactional + public void resetPasswordByMemberId(String memberId, String newPassword) { + Member member = memberRepository.findOneById(memberId); + if (member == null) { + throw new BusinessException(MemberErrorCode.FAILED_FIND_MEMBER_INFO); + } + + member.updatePassword(ConvertSHA256.convertToSHA256(newPassword)); + } @Transactional public MemberDto loginForKakao(String accessToken) {