From 83f8f91f7f89e3b6966cc2fa2d2311f51d729786 Mon Sep 17 00:00:00 2001 From: mmihye Date: Tue, 28 May 2024 20:35:33 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[#9]=20Feat=20:=20=EB=84=A4=EC=9D=B4?= =?UTF-8?q?=EB=B2=84=20=EC=86=8C=EC=85=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/service/AuthService.java | 116 ++++++++++++++---- .../domain/member/service/MemberService.java | 1 + .../global/security/jwt/JwtFilter.java | 8 +- .../security/naver/dto/NaverProperties.java | 24 ++++ .../naver/dto/NaverTokenResponse.java | 24 ++++ .../security/naver/dto/NaverUserResponse.java | 29 +++++ 6 files changed, 174 insertions(+), 28 deletions(-) create mode 100644 src/main/java/Journey/Together/global/security/naver/dto/NaverProperties.java create mode 100644 src/main/java/Journey/Together/global/security/naver/dto/NaverTokenResponse.java create mode 100644 src/main/java/Journey/Together/global/security/naver/dto/NaverUserResponse.java diff --git a/src/main/java/Journey/Together/domain/member/service/AuthService.java b/src/main/java/Journey/Together/domain/member/service/AuthService.java index 36b826b..ef41ecf 100644 --- a/src/main/java/Journey/Together/domain/member/service/AuthService.java +++ b/src/main/java/Journey/Together/domain/member/service/AuthService.java @@ -11,9 +11,18 @@ import Journey.Together.global.security.jwt.TokenProvider; import Journey.Together.global.security.jwt.dto.TokenDto; import Journey.Together.global.security.kakao.dto.KakaoToken; +import Journey.Together.global.security.naver.dto.NaverProperties; +import Journey.Together.global.security.naver.dto.NaverTokenResponse; +import Journey.Together.global.security.naver.dto.NaverUserResponse; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; @Service @Transactional(readOnly = true) @@ -24,36 +33,95 @@ public class AuthService { private final TokenProvider tokenProvider; private final MemberRepository memberRepository; + private final RestTemplate restTemplate = new RestTemplate(); + private final NaverProperties naverProperties; + @Transactional public LoginRes signIn(String code, LoginType type) { - //Business Logic - // 카카오로 액세스 토큰 요청하기 - KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(code); - // 카카오톡에 있는 사용자 정보 반환 - KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(kakaoAccessToken); - // 반환된 정보의 이메일 기반으로 사용자 테이블에서 계정 정보 조회 진행 - Member member = memberRepository.findMemberByEmailAndDeletedAtIsNull(kakaoProfile.kakao_account().email()).orElse(null); - // 이메일 존재 시 로그인 , 존재하지 않을 경우 회원가입 진행 - if(member == null) { - Member newMember = Member.builder() - .email(kakaoProfile.kakao_account().email()) - .name(kakaoProfile.kakao_account().profile().nickname()) - .memberType("GENERAL") - .loginType("KAKAO") - .build(); - - member = memberRepository.save(newMember); + Member member = null; + TokenDto tokenDto = null; + + if(type == LoginType.KAKAO) { + //Business Logic + // 카카오로 액세스 토큰 요청하기 + KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(code); + // 카카오톡에 있는 사용자 정보 반환 + KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(kakaoAccessToken); + // 반환된 정보의 이메일 기반으로 사용자 테이블에서 계정 정보 조회 진행 + member = memberRepository.findMemberByEmailAndDeletedAtIsNull(kakaoProfile.kakao_account().email()).orElse(null); + // 이메일 존재 시 로그인 , 존재하지 않을 경우 회원가입 진행 + if (member == null) { + Member newMember = Member.builder() + .email(kakaoProfile.kakao_account().email()) + .name(kakaoProfile.kakao_account().profile().nickname()) + .memberType("GENERAL") + .loginType("KAKAO") + .build(); + + member = memberRepository.save(newMember); + } + tokenDto = tokenProvider.createToken(member); + + // RefreshToken 저장 + member.setRefreshToken(kakaoAccessToken.refresh_token()); + + // Response + return LoginRes.of(member, tokenDto); + + } else if (type == LoginType.NAVER) { + String accessToken = toRequestAccessToken(code); + NaverUserResponse.NaverUserDetail naverProfile = toRequestProfile(accessToken); + member = memberRepository.findMemberByEmailAndDeletedAtIsNull(naverProfile.getEmail()).orElse(null); + + if (member == null) { + Member newMember = Member.builder() + .email(naverProfile.getEmail()) + .profileUrl(naverProfile.getProfile_image()) + .loginType("NAVER") + .refreshToken(toRequestRefreshToken(code)) + .name(naverProfile.getName()) + .memberType("GENERAL") + .build(); + + member = memberRepository.save(newMember); + } + + tokenDto = tokenProvider.createToken(member); + } - TokenDto tokenDto = tokenProvider.createToken(member); + return LoginRes.of(member, tokenDto); + } - // RefreshToken 저장 - member.setRefreshToken(kakaoAccessToken.refresh_token()); + private String toRequestAccessToken(String code) { - // Response - return LoginRes.of(member, tokenDto); + ResponseEntity response = + restTemplate.exchange(naverProperties.getRequestURL(code), HttpMethod.GET, null, NaverTokenResponse.class); + + return response.getBody().getAccessToken(); + } + private String toRequestRefreshToken(String code) { + + ResponseEntity response = + restTemplate.exchange(naverProperties.getRequestURL(code), HttpMethod.GET, null, NaverTokenResponse.class); + + return response.getBody().getRefreshToken(); + } + + private NaverUserResponse.NaverUserDetail toRequestProfile(String accessToken) { + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(accessToken); + HttpEntity> request = new HttpEntity<>(headers); + + ResponseEntity response = + restTemplate.exchange("https://openapi.naver.com/v1/nid/me", HttpMethod.GET, request, NaverUserResponse.class); + + // Validate를 만드는 것을 추천 + + return response.getBody().getNaverUserDetail(); } + public void signOut(String token, Member member) { // Validation String accessToken = token.substring(7); @@ -61,8 +129,8 @@ public void signOut(String token, Member member) { // Business Logic - Refresh Token 삭제 및 Access Token 블랙리스트 등록 String key = member.getEmail(); - redisClient.deleteValue(key); - redisClient.setValue(accessToken, "logout", tokenProvider.getExpiration(accessToken)); +// redisClient.deleteValue(key); +// redisClient.setValue(accessToken, "logout", tokenProvider.getExpiration(accessToken)); // Response } diff --git a/src/main/java/Journey/Together/domain/member/service/MemberService.java b/src/main/java/Journey/Together/domain/member/service/MemberService.java index acf4bce..e7d8e0a 100644 --- a/src/main/java/Journey/Together/domain/member/service/MemberService.java +++ b/src/main/java/Journey/Together/domain/member/service/MemberService.java @@ -12,4 +12,5 @@ public class MemberService { private final MemberRepository memberRepository; + } diff --git a/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java b/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java index ec55cc6..e955898 100644 --- a/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java +++ b/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java @@ -46,10 +46,10 @@ private String resolveToken(HttpServletRequest request) { if (StringUtils.hasText(token) && token.startsWith("Bearer ")) { // 블랙리스트 토큰인 경우 String substringToken = token.substring(7); - String value = redisClient.getValue(substringToken); - if (value.equals("logout")) { - throw new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION); - } +// String value = redisClient.getValue(substringToken); +// if (value.equals("logout")) { +// throw new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION); +// } return substringToken; } diff --git a/src/main/java/Journey/Together/global/security/naver/dto/NaverProperties.java b/src/main/java/Journey/Together/global/security/naver/dto/NaverProperties.java new file mode 100644 index 0000000..438c61b --- /dev/null +++ b/src/main/java/Journey/Together/global/security/naver/dto/NaverProperties.java @@ -0,0 +1,24 @@ +package Journey.Together.global.security.naver.dto; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.util.UriComponentsBuilder; + +@Data +@Configuration +@ConfigurationProperties(prefix = "naver") +public class NaverProperties { + private String requestTokenUri; + private String clientId; + private String clientSecret; + + public String getRequestURL(String code) { + return UriComponentsBuilder.fromHttpUrl(requestTokenUri) + .queryParam("grant_type", "authorization_code") + .queryParam("client_id", clientId) + .queryParam("client_secret", clientSecret) + .queryParam("code", code) + .toUriString(); + } +} \ No newline at end of file diff --git a/src/main/java/Journey/Together/global/security/naver/dto/NaverTokenResponse.java b/src/main/java/Journey/Together/global/security/naver/dto/NaverTokenResponse.java new file mode 100644 index 0000000..d51504f --- /dev/null +++ b/src/main/java/Journey/Together/global/security/naver/dto/NaverTokenResponse.java @@ -0,0 +1,24 @@ +package Journey.Together.global.security.naver.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class NaverTokenResponse { + @JsonProperty("access_token") + private String accessToken; + @JsonProperty("refresh_token") + private String refreshToken; + @JsonProperty("token_type") + private String tokenType; + @JsonProperty("expires_in") + private String expiresIn; + @JsonProperty("error") + private String error; + @JsonProperty("error_description") + private String errorDescription; +} \ No newline at end of file diff --git a/src/main/java/Journey/Together/global/security/naver/dto/NaverUserResponse.java b/src/main/java/Journey/Together/global/security/naver/dto/NaverUserResponse.java new file mode 100644 index 0000000..a63dc8d --- /dev/null +++ b/src/main/java/Journey/Together/global/security/naver/dto/NaverUserResponse.java @@ -0,0 +1,29 @@ +package Journey.Together.global.security.naver.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class NaverUserResponse { + @JsonProperty("resultcode") + private String resultCode; + @JsonProperty("message") + private String message; + @JsonProperty("response") + private NaverUserDetail naverUserDetail; + + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class NaverUserDetail { + private String id; + private String name; + private String email; + private String nickname; + private String profile_image; + } +} \ No newline at end of file From 9f15eee16583581b3892739774311df44433e200 Mon Sep 17 00:00:00 2001 From: mmihye Date: Wed, 29 May 2024 03:18:33 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[#9]=20=EB=84=A4=EC=9D=B4=EB=B2=84&?= =?UTF-8?q?=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EC=86=8C=EC=85=9C=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20MERGE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/controller/AuthController.java | 5 +- .../Together/domain/member/entity/Member.java | 13 ++--- .../domain/member/service/AuthService.java | 51 +++++++------------ .../global/security/jwt/JwtFilter.java | 8 +++ 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/main/java/Journey/Together/domain/member/controller/AuthController.java b/src/main/java/Journey/Together/domain/member/controller/AuthController.java index e2ce741..e5bd580 100644 --- a/src/main/java/Journey/Together/domain/member/controller/AuthController.java +++ b/src/main/java/Journey/Together/domain/member/controller/AuthController.java @@ -29,8 +29,9 @@ public void login(HttpServletResponse response) throws IOException { @Operation(summary = "로그인 API", description = "카카오 로그인 페이지로 리다이렉트되어 카카오 로그인을 수행할 수 있도록 안x내") @PostMapping("/sign-in") - public ApiResponse signIn(@RequestParam(name = "code") String code, @RequestBody String type) { - return ApiResponse.success(Success.LOGIN_SUCCESS,authService.signIn(code,LoginType.valueOf(type))); + public ApiResponse signIn(@RequestHeader("Authorization") String token, + @RequestBody String type) { + return ApiResponse.success(Success.LOGIN_SUCCESS,authService.signIn(token,type)); } @Operation(summary = "로그아웃 API", description = "로그아웃된 JWT 블랙리스트 등록") diff --git a/src/main/java/Journey/Together/domain/member/entity/Member.java b/src/main/java/Journey/Together/domain/member/entity/Member.java index dde0d88..4976749 100644 --- a/src/main/java/Journey/Together/domain/member/entity/Member.java +++ b/src/main/java/Journey/Together/domain/member/entity/Member.java @@ -13,9 +13,10 @@ @Table(name = "member") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Member extends BaseTimeEntity { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "member_id", nullable = false, columnDefinition = "bigint") + @Column(name = "member_id", columnDefinition = "bigint") private Long memberId; // 이메일은 최대 255자 + 1자(@) + 69자해서 최대 320글자이므로, varchar(320) 사용 @@ -38,7 +39,7 @@ public class Member extends BaseTimeEntity { @Enumerated(EnumType.STRING) private LoginType loginType; - @Column(name = "blood_type", nullable = false, columnDefinition = "varchar(255)") + @Column(name = "blood_type", columnDefinition = "varchar(255)") @Enumerated(EnumType.STRING) private BloodType bloodType; @@ -62,14 +63,14 @@ public class Member extends BaseTimeEntity { private String refreshToken; @Builder - public Member(String email, String name, String phone, String profileUrl, String loginType,String bloodType,String memberType, String birth, String allergy, String medication, String refreshToken) { + public Member(String email, String name, String phone, String profileUrl, LoginType loginType,BloodType bloodType,MemberType memberType, String birth, String allergy, String medication, String refreshToken) { this.email = email; this.name = name; this.phone = phone; this.profileUrl = profileUrl; - this.loginType = LoginType.valueOf(loginType); - this.bloodType = BloodType.valueOf(bloodType); - this.memberType = MemberType.valueOf(bloodType); + this.loginType = loginType; + this.bloodType = bloodType; + this.memberType = memberType; this.birth = birth; this.allergy = allergy; this.medication=medication; diff --git a/src/main/java/Journey/Together/domain/member/service/AuthService.java b/src/main/java/Journey/Together/domain/member/service/AuthService.java index e7eac8b..a27e7fd 100644 --- a/src/main/java/Journey/Together/domain/member/service/AuthService.java +++ b/src/main/java/Journey/Together/domain/member/service/AuthService.java @@ -3,6 +3,7 @@ import Journey.Together.domain.member.dto.LoginRes; import Journey.Together.domain.member.entity.Member; import Journey.Together.domain.member.enumerate.LoginType; +import Journey.Together.domain.member.enumerate.MemberType; import Journey.Together.domain.member.repository.MemberRepository; import Journey.Together.global.exception.ApplicationException; import Journey.Together.global.exception.ErrorCode; @@ -12,7 +13,6 @@ import Journey.Together.global.security.jwt.dto.TokenDto; import Journey.Together.global.security.kakao.dto.KakaoToken; import Journey.Together.global.security.naver.dto.NaverProperties; -import Journey.Together.global.security.naver.dto.NaverTokenResponse; import Journey.Together.global.security.naver.dto.NaverUserResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpEntity; @@ -34,17 +34,16 @@ public class AuthService { private final MemberRepository memberRepository; private final RestTemplate restTemplate = new RestTemplate(); - private final NaverProperties naverProperties; @Transactional - public LoginRes signIn(String code, LoginType type) { + public LoginRes signIn(String token, String type) { Member member = null; TokenDto tokenDto = null; - if(type == LoginType.KAKAO) { + if(type == "KAKAO") { //Business Logic // 카카오로 액세스 토큰 요청하기 - KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(code); + KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(token); // 카카오톡에 있는 사용자 정보 반환 KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(kakaoAccessToken); // 반환된 정보의 이메일 기반으로 사용자 테이블에서 계정 정보 조회 진행 @@ -54,57 +53,43 @@ public LoginRes signIn(String code, LoginType type) { Member newMember = Member.builder() .email(kakaoProfile.kakao_account().email()) .name(kakaoProfile.kakao_account().profile().nickname()) - .memberType("GENERAL") - .refreshToken(kakaoAccessToken.refresh_token()) - .loginType("KAKAO") + .memberType(MemberType.GENERAL) + .loginType(LoginType.KAKAO) .build(); member = memberRepository.save(newMember); } tokenDto = tokenProvider.createToken(member); + // RefreshToken 저장 + member.setRefreshToken(tokenDto.refreshToken()); + // Response return LoginRes.of(member, tokenDto); - } else if (type == LoginType.NAVER) { - String accessToken = toRequestAccessToken(code); - NaverUserResponse.NaverUserDetail naverProfile = toRequestProfile(accessToken); + } else if (type.equals("NAVER")) { + NaverUserResponse.NaverUserDetail naverProfile = toRequestProfile(token.substring(7)); member = memberRepository.findMemberByEmailAndDeletedAtIsNull(naverProfile.getEmail()).orElse(null); if (member == null) { Member newMember = Member.builder() - .email(naverProfile.getEmail()) - .profileUrl(naverProfile.getProfile_image()) - .loginType("NAVER") - .refreshToken(toRequestRefreshToken(code)) - .name(naverProfile.getName()) - .memberType("GENERAL") + .email(naverProfile.getEmail() != null ? naverProfile.getEmail() : "Unknown") + .profileUrl(naverProfile.getProfile_image() != null ? naverProfile.getProfile_image() : "Unknown") + .name(naverProfile.getName() != null ? naverProfile.getName() : "Unknown") + .memberType(MemberType.GENERAL) + .loginType(LoginType.NAVER) .build(); member = memberRepository.save(newMember); } tokenDto = tokenProvider.createToken(member); + member.setRefreshToken(tokenDto.refreshToken()); } return LoginRes.of(member, tokenDto); } - private String toRequestAccessToken(String code) { - - ResponseEntity response = - restTemplate.exchange(naverProperties.getRequestURL(code), HttpMethod.GET, null, NaverTokenResponse.class); - - return response.getBody().getAccessToken(); - } - - private String toRequestRefreshToken(String code) { - - ResponseEntity response = - restTemplate.exchange(naverProperties.getRequestURL(code), HttpMethod.GET, null, NaverTokenResponse.class); - - return response.getBody().getRefreshToken(); - } private NaverUserResponse.NaverUserDetail toRequestProfile(String accessToken) { HttpHeaders headers = new HttpHeaders(); @@ -114,8 +99,6 @@ private NaverUserResponse.NaverUserDetail toRequestProfile(String accessToken) { ResponseEntity response = restTemplate.exchange("https://openapi.naver.com/v1/nid/me", HttpMethod.GET, request, NaverUserResponse.class); - // Validate를 만드는 것을 추천 - return response.getBody().getNaverUserDetail(); } diff --git a/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java b/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java index 162cfcb..3ce1427 100644 --- a/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java +++ b/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java @@ -21,6 +21,14 @@ public class JwtFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { + String requestURI = request.getRequestURI(); + + //jwt 유효성 검사를 하지않음 + if ("/v1/auth/sign-in".equals(requestURI)||"/actuator/health".equals(requestURI)) { + filterChain.doFilter(request, response); + return; + } + String token = resolveToken(request); // 토큰이 존재할 경우, Authentication에 인증 정보 저장 및 로그 출력 From 255265d24fadd00a5b2dbd6ce7c01246d4c83ea0 Mon Sep 17 00:00:00 2001 From: mmihye Date: Wed, 29 May 2024 03:56:04 +0900 Subject: [PATCH 3/5] =?UTF-8?q?actuator=20api=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20=EC=8B=9C=ED=81=90=EB=A6=AC=ED=8B=B0=20=EB=B0=8F=20?= =?UTF-8?q?jwt=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/Journey/Together/global/config/SecurityConfig.java | 1 + .../java/Journey/Together/global/security/jwt/JwtFilter.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/Journey/Together/global/config/SecurityConfig.java b/src/main/java/Journey/Together/global/config/SecurityConfig.java index 7c52491..9f23268 100644 --- a/src/main/java/Journey/Together/global/config/SecurityConfig.java +++ b/src/main/java/Journey/Together/global/config/SecurityConfig.java @@ -59,6 +59,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { authorize.requestMatchers("/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll() // 로그인 로직 접속 허용 .requestMatchers("/v1/auth/**", "/oauth2/**", "/login.html").permitAll() + .requestMatchers("/actuator/**").permitAll() .requestMatchers("/v1/member/**").authenticated() // 메인 페이지, 공고 페이지 등에 한해 인증 정보 없이 접근 가능 (추후 추가) // 이외의 모든 요청은 인증 정보 필요 diff --git a/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java b/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java index 3ce1427..3b52451 100644 --- a/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java +++ b/src/main/java/Journey/Together/global/security/jwt/JwtFilter.java @@ -24,7 +24,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse String requestURI = request.getRequestURI(); //jwt 유효성 검사를 하지않음 - if ("/v1/auth/sign-in".equals(requestURI)||"/actuator/health".equals(requestURI)) { + if ("/v1/auth/sign-in".equals(requestURI) || "/actuator/health".equals(requestURI)) { filterChain.doFilter(request, response); return; } From 3ca26b65c858a09a347f02b474ef2ce04b6612f0 Mon Sep 17 00:00:00 2001 From: mmihye Date: Wed, 29 May 2024 04:31:43 +0900 Subject: [PATCH 4/5] =?UTF-8?q?Fix=20:=20=EB=A8=B8=EC=A7=80=ED=95=A0?= =?UTF-8?q?=EB=95=8C=20=EC=9E=98=EB=AA=BB=EB=90=9C=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/service/AuthService.java | 14 +++++--------- .../global/security/kakao/KakaoClient.java | 4 ++-- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/java/Journey/Together/domain/member/service/AuthService.java b/src/main/java/Journey/Together/domain/member/service/AuthService.java index a27e7fd..b433d69 100644 --- a/src/main/java/Journey/Together/domain/member/service/AuthService.java +++ b/src/main/java/Journey/Together/domain/member/service/AuthService.java @@ -42,26 +42,22 @@ public LoginRes signIn(String token, String type) { if(type == "KAKAO") { //Business Logic - // 카카오로 액세스 토큰 요청하기 - KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(token); // 카카오톡에 있는 사용자 정보 반환 - KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(kakaoAccessToken); + KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(token); // 반환된 정보의 이메일 기반으로 사용자 테이블에서 계정 정보 조회 진행 member = memberRepository.findMemberByEmailAndDeletedAtIsNull(kakaoProfile.kakao_account().email()).orElse(null); // 이메일 존재 시 로그인 , 존재하지 않을 경우 회원가입 진행 - if (member == null) { + if(member == null) { Member newMember = Member.builder() .email(kakaoProfile.kakao_account().email()) .name(kakaoProfile.kakao_account().profile().nickname()) - .memberType(MemberType.GENERAL) - .loginType(LoginType.KAKAO) + .profileUrl(kakaoProfile.kakao_account().profile().profile_image_url()) + .memberType(MemberType.valueOf("GENERAL")) + .loginType(LoginType.valueOf("KAKAO")) .build(); - member = memberRepository.save(newMember); } tokenDto = tokenProvider.createToken(member); - - // RefreshToken 저장 member.setRefreshToken(tokenDto.refreshToken()); // Response diff --git a/src/main/java/Journey/Together/global/security/kakao/KakaoClient.java b/src/main/java/Journey/Together/global/security/kakao/KakaoClient.java index 74c0a41..44817f7 100644 --- a/src/main/java/Journey/Together/global/security/kakao/KakaoClient.java +++ b/src/main/java/Journey/Together/global/security/kakao/KakaoClient.java @@ -73,7 +73,7 @@ public KakaoToken getKakaoAccessToken(String code) { return kakaoToken; } - public KakaoProfile getMemberInfo(KakaoToken kakaoToken) { + public KakaoProfile getMemberInfo(String accesToken) { // 요청 기본 객체 생성 WebClient webClient = WebClient.create(kakaoUserInfoUri); @@ -81,7 +81,7 @@ public KakaoProfile getMemberInfo(KakaoToken kakaoToken) { String response = webClient.post() .uri(kakaoUserInfoUri) .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") - .header("Authorization", "Bearer " + kakaoToken.access_token()) + .header("Authorization", accesToken) .retrieve() .bodyToMono(String.class) .block(); From 13eb7fffb2aa192708a2ae3e4b31612df598bf2b Mon Sep 17 00:00:00 2001 From: mmihye Date: Wed, 29 May 2024 04:32:43 +0900 Subject: [PATCH 5/5] =?UTF-8?q?Fix=20:=20=EB=A8=B8=EC=A7=80=ED=95=A0?= =?UTF-8?q?=EB=95=8C=20=EC=9E=98=EB=AA=BB=EB=90=9C=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B022?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Journey/Together/domain/member/service/AuthService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Journey/Together/domain/member/service/AuthService.java b/src/main/java/Journey/Together/domain/member/service/AuthService.java index b433d69..6dabab2 100644 --- a/src/main/java/Journey/Together/domain/member/service/AuthService.java +++ b/src/main/java/Journey/Together/domain/member/service/AuthService.java @@ -40,7 +40,7 @@ public LoginRes signIn(String token, String type) { Member member = null; TokenDto tokenDto = null; - if(type == "KAKAO") { + if(type.equals("KAKAO")) { //Business Logic // 카카오톡에 있는 사용자 정보 반환 KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(token);