-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 카카오 로그인 중 사용자 정보 확인 로직을 안드로이드에서 백엔드로 이관 (#404)
* feat: 카카오 로그인 API 구현 * feat: providerId를 loginId로 수정 * feat: 소셜 로그인 시 랜덤 생성된 비밀번호 사용 * refactor: 불필요한 api 제거 Co-authored-by: fromitive <46563149+fromitive@users.noreply.github.com> Co-authored-by: SCY <helenason@naver.com> * test: 로그인 로직 변경 Co-authored-by: fromitive <46563149+fromitive@users.noreply.github.com> Co-authored-by: SCY <helenason@naver.com> * test: MemberFixture 불필요한 함수 제거 및 통일 Co-authored-by: fromitive <fromitive@gmail.com> Co-authored-by: Dora Choo <choo000407@naver.com> * refactor: 불필요한 정보 제거 Co-authored-by: fromitive <fromitive@gmail.com> Co-authored-by: Dora Choo <choo000407@naver.com> * feat: 카카오 로그인 에러 핸들러 추가 Co-authored-by: fromitive <fromitive@gmail.com> Co-authored-by: Dora Choo <choo000407@naver.com> * feat: 민감 정보 로깅에서 제외 Co-authored-by: fromitive <fromitive@gmail.com> Co-authored-by: Dora Choo <choo000407@naver.com> --------- Co-authored-by: fromitive <46563149+fromitive@users.noreply.github.com> Co-authored-by: SCY <helenason@naver.com> Co-authored-by: fromitive <fromitive@gmail.com>
- Loading branch information
1 parent
6accb7a
commit 28fa741
Showing
28 changed files
with
353 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/com/zzang/chongdae/auth/config/AuthClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.zzang.chongdae.auth.config; | ||
|
||
import com.zzang.chongdae.auth.service.AuthClient; | ||
import java.time.Duration; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.ClientHttpRequestFactories; | ||
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Configuration | ||
public class AuthClientConfig { | ||
|
||
@Value("${auth.connect-timeout-length}") | ||
private Duration connectTimeoutLength; | ||
|
||
@Value("${auth.read-timeout-length}") | ||
private Duration readTimeoutLength; | ||
|
||
@Bean | ||
public AuthClient authClient() { | ||
return new AuthClient(createRestClient()); | ||
} | ||
|
||
private RestClient createRestClient() { | ||
return RestClient.builder() | ||
.requestFactory(createRequestFactory()) | ||
.requestInterceptor(new AuthClientTimeoutInterceptor()) | ||
.build(); | ||
} | ||
|
||
private ClientHttpRequestFactory createRequestFactory() { | ||
ClientHttpRequestFactorySettings requestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS | ||
.withConnectTimeout(connectTimeoutLength) | ||
.withReadTimeout(readTimeoutLength); | ||
return ClientHttpRequestFactories.get(requestFactorySettings); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/zzang/chongdae/auth/config/AuthClientTimeoutInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.zzang.chongdae.auth.config; | ||
|
||
import com.zzang.chongdae.auth.exception.AuthErrorCode; | ||
import com.zzang.chongdae.global.exception.MarketException; | ||
import java.io.IOException; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
public class AuthClientTimeoutInterceptor implements ClientHttpRequestInterceptor { | ||
|
||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) { | ||
try { | ||
return execution.execute(request, body); | ||
} catch (IOException exception) { | ||
throw new MarketException(AuthErrorCode.CLIENT_TIME_OUT); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/com/zzang/chongdae/auth/exception/KakaoLoginExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.zzang.chongdae.auth.exception; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.zzang.chongdae.auth.service.dto.KakaoLoginFailResponseDto; | ||
import com.zzang.chongdae.global.exception.MarketException; | ||
import java.io.IOException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.web.client.ResponseErrorHandler; | ||
|
||
@Slf4j | ||
public class KakaoLoginExceptionHandler implements ResponseErrorHandler { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public boolean hasError(ClientHttpResponse response) throws IOException { | ||
return response.getStatusCode().isError(); | ||
} | ||
|
||
@Override | ||
public void handleError(ClientHttpResponse response) throws IOException { | ||
throw new MarketException(getKakaoLoginErrorCode(response)); | ||
} | ||
|
||
private AuthErrorCode getKakaoLoginErrorCode(final ClientHttpResponse response) throws IOException { | ||
KakaoLoginFailResponseDto kakaoLoginFailResponse = objectMapper.readValue( | ||
response.getBody(), KakaoLoginFailResponseDto.class); | ||
log.error(kakaoLoginFailResponse.toString()); | ||
return AuthErrorCode.KAKAO_LOGIN_INTERNAL_SERVER_ERROR; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/com/zzang/chongdae/auth/service/AuthClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.zzang.chongdae.auth.service; | ||
|
||
import com.zzang.chongdae.auth.exception.KakaoLoginExceptionHandler; | ||
import com.zzang.chongdae.auth.service.dto.KakaoLoginResponseDto; | ||
import com.zzang.chongdae.member.domain.AuthProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@RequiredArgsConstructor | ||
public class AuthClient { | ||
|
||
private static final String BEARER_HEADER_FORMAT = "Bearer %s"; | ||
private static final String GET_KAKAO_USER_INFO_URI = "https://kapi.kakao.com/v2/user/me"; | ||
|
||
private final RestClient restClient; | ||
|
||
public String getKakaoUserInfo(String accessToken) { | ||
KakaoLoginResponseDto responseDto = restClient.get() | ||
.uri(GET_KAKAO_USER_INFO_URI) | ||
.header(HttpHeaders.AUTHORIZATION, createAuthorization(accessToken)) | ||
.retrieve() | ||
.onStatus(new KakaoLoginExceptionHandler()) | ||
.body(KakaoLoginResponseDto.class); | ||
return AuthProvider.KAKAO.buildLoginId(responseDto.id().toString()); // TODO: NPE 처리 고려하기 | ||
} | ||
|
||
private String createAuthorization(String accessToken) { | ||
return BEARER_HEADER_FORMAT.formatted(accessToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
backend/src/main/java/com/zzang/chongdae/auth/service/dto/KakaoLoginFailResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.zzang.chongdae.auth.service.dto; | ||
|
||
public record KakaoLoginFailResponseDto(String msg, | ||
Long code) { | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/zzang/chongdae/auth/service/dto/KakaoLoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.zzang.chongdae.auth.service.dto; | ||
|
||
public record KakaoLoginRequest(String accessToken) { | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/zzang/chongdae/auth/service/dto/KakaoLoginResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.zzang.chongdae.auth.service.dto; | ||
|
||
public record KakaoLoginResponseDto(Long id) { | ||
} |
6 changes: 0 additions & 6 deletions
6
backend/src/main/java/com/zzang/chongdae/auth/service/dto/LoginRequest.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
backend/src/main/java/com/zzang/chongdae/auth/service/dto/SignupRequest.java
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
backend/src/main/java/com/zzang/chongdae/auth/service/dto/SignupResponse.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/com/zzang/chongdae/logging/config/LoggingMasked.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.zzang.chongdae.logging.config; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface LoggingMasked { | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/zzang/chongdae/member/domain/AuthProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.zzang.chongdae.member.domain; | ||
|
||
public enum AuthProvider { | ||
|
||
KAKAO; | ||
|
||
public String buildLoginId(String loginId) { | ||
return this.name() + loginId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.