Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 카카오 로그인 Redirect URI 동적 할당 #62

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/project/backend/business/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.backend.business.auth.implement.KakaoLoginManager;
import project.backend.business.auth.request.KakaoLoginServiceRequest;
import project.backend.business.user.implement.UserManager;
import project.backend.business.user.implement.UserReader;
import project.backend.entity.token.BlacklistToken;
Expand All @@ -35,8 +36,8 @@ public class AuthService {
private final BlacklistTokenRedisRepository blacklistTokenRedisRepository;

@Transactional
public TokenServiceResponse kakaoLogin(String code) throws JsonProcessingException {
String token = kakaoLoginManager.getKakaoToken(code);
public TokenServiceResponse kakaoLogin(KakaoLoginServiceRequest serviceRequest) throws JsonProcessingException {
String token = kakaoLoginManager.getKakaoToken(serviceRequest.getCode(), serviceRequest.getRedirectUri());
User user = kakaoLoginManager.getKakaoUser(token);

TokenServiceResponse tokenServiceResponse = tokenProvider.createToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public class KakaoLoginManager {
@Value("${spring.security.oauth2.client.registration.kakao.client-secret}")
private String kakaoClientSecret;

@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}")
private String redirectUri;

public String getKakaoToken(String code) throws JsonProcessingException {
public String getKakaoToken(String code, String redirectUri) throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package project.backend.business.auth.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class KakaoLoginServiceRequest {
private final String code;
private final String redirectUri;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import project.backend.business.auth.AuthService;
import project.backend.business.auth.request.KakaoLoginServiceRequest;
import project.backend.business.auth.request.TokenServiceRequest;
import project.backend.business.auth.response.TokenServiceResponse;
import project.backend.presentation.auth.docs.AuthControllerDocs;
import project.backend.presentation.auth.util.KakaoLoginRequestConverter;
import project.backend.presentation.auth.util.TokenCookieManager;
import project.backend.presentation.auth.util.TokenExtractor;
import project.backend.security.aop.AssignCurrentUserInfo;
Expand All @@ -29,12 +31,15 @@ public class AuthController implements AuthControllerDocs {
private final AuthService authService;
private final TokenExtractor tokenExtractor;
private final TokenCookieManager tokenCookieManager;
private final KakaoLoginRequestConverter kakaoLoginRequestConverter;

@PostMapping("/login/kakao")
public ResponseEntity<TokenServiceResponse> loginKakao(
@RequestParam(name = "code") String code,
HttpServletRequest request,
HttpServletResponse response) throws JsonProcessingException {
TokenServiceResponse tokenServiceResponse = authService.kakaoLogin(code);
KakaoLoginServiceRequest kakaoLoginServiceRequest = kakaoLoginRequestConverter.convertKakaoLoginRequest(request, code);
TokenServiceResponse tokenServiceResponse = authService.kakaoLogin(kakaoLoginServiceRequest);

tokenCookieManager.addRefreshTokenCookie(response, tokenServiceResponse.getRefreshToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public interface AuthControllerDocs {

@Operation(summary = "카카오 로그인 API", description = "카카오 계정을 이용하여 로그인.")
ResponseEntity<TokenServiceResponse> loginKakao(String code, HttpServletResponse response)
ResponseEntity<TokenServiceResponse> loginKakao(String code, HttpServletRequest request, HttpServletResponse response)
throws JsonProcessingException;

@Operation(summary = "토큰 갱신 API", description = "set-cookie에 있는 refresh token을 사용하여 Access "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package project.backend.presentation.auth.util;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import project.backend.business.auth.request.KakaoLoginServiceRequest;

@Component
public class KakaoLoginRequestConverter {

@Value("http://localhost:3000/oauth/kakao/callback")
private String localRedirectUri;

@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}")
private String productionRedirectUri;

public KakaoLoginServiceRequest convertKakaoLoginRequest(HttpServletRequest request, String code) {
String redirectUri = productionRedirectUri;
if (request.getServerName().equals("localhost")) {
redirectUri = localRedirectUri;
}
return KakaoLoginServiceRequest.builder()
.code(code)
.redirectUri(redirectUri)
.build();
}
}
Loading