diff --git a/src/main/java/com/example/sinitto/auth/controller/AuthController.java b/src/main/java/com/example/sinitto/auth/controller/AuthController.java index 88bab91c..009c3b9e 100644 --- a/src/main/java/com/example/sinitto/auth/controller/AuthController.java +++ b/src/main/java/com/example/sinitto/auth/controller/AuthController.java @@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -41,8 +42,8 @@ public ResponseEntity refreshToken(@RequestBody TokenRefreshReque @Operation(summary = "Oauth 카카오 인증페이지 리다이렉트", description = "카카오 로그인 화면으로 이동한다.", security = @SecurityRequirement(name = "JWT제외")) @GetMapping("/oauth/kakao") - public ResponseEntity redirectToKakaoAuth() { - String url = kakaoApiService.getAuthorizationUrl(); + public ResponseEntity redirectToKakaoAuth(HttpServletRequest httpServletRequest) { + String url = kakaoApiService.getAuthorizationUrl(httpServletRequest); HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(url)); return new ResponseEntity<>(headers, HttpStatus.FOUND); @@ -50,8 +51,8 @@ public ResponseEntity redirectToKakaoAuth() { @Operation(summary = "Oauth 카카오 로그인 콜백", description = "카카오 로그인 이후 발생하는 인가코드를 통해 AccessToken과 RefreshToken을 발급한다.", security = @SecurityRequirement(name = "JWT제외")) @GetMapping("/oauth/kakao/callback") - public ResponseEntity kakaoCallback(@RequestParam("code") String code) { - LoginResponse loginResponse = memberService.kakaoLogin(code); + public ResponseEntity kakaoCallback(@RequestParam("code") String code, HttpServletRequest httpServletRequest) { + LoginResponse loginResponse = memberService.kakaoLogin(code, httpServletRequest); return ResponseEntity.ok().body(loginResponse); } diff --git a/src/main/java/com/example/sinitto/auth/service/KakaoApiService.java b/src/main/java/com/example/sinitto/auth/service/KakaoApiService.java index 22514abd..6b5b16ef 100644 --- a/src/main/java/com/example/sinitto/auth/service/KakaoApiService.java +++ b/src/main/java/com/example/sinitto/auth/service/KakaoApiService.java @@ -2,8 +2,10 @@ import com.example.sinitto.auth.dto.KakaoTokenResponse; import com.example.sinitto.auth.dto.KakaoUserResponse; +import com.example.sinitto.common.exception.BadRequestException; import com.example.sinitto.common.exception.NotFoundException; import com.example.sinitto.common.properties.KakaoProperties; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; @@ -25,20 +27,42 @@ public KakaoApiService(RestTemplate restTemplate, KakaoProperties kakaoPropertie this.kakaoProperties = kakaoProperties; } - public String getAuthorizationUrl() { + public String getAuthorizationUrl(HttpServletRequest httpServletRequest) { + String requestUrl = httpServletRequest.getHeader("Referer"); + String redirectUri; + + if (requestUrl.contains("localhost:5173")) { + redirectUri = kakaoProperties.devRedirectUri(); + } else if (requestUrl.contains("sinitto.s3-website.ap-northeast-2.amazonaws.com")) { + redirectUri = kakaoProperties.redirectUri(); + } else { + throw new BadRequestException("해당 도메인에서는 카카오 로그인이 불가합니다. requestUrl : " + requestUrl); + } + return KAKAO_AUTH_BASE_URL + "/authorize?response_type=code&client_id=" - + kakaoProperties.clientId() + "&redirect_uri=" + kakaoProperties.redirectUri(); + + kakaoProperties.clientId() + "&redirect_uri=" + redirectUri; } - public KakaoTokenResponse getAccessToken(String authorizationCode) { + public KakaoTokenResponse getAccessToken(String authorizationCode, HttpServletRequest httpServletRequest) { String url = KAKAO_AUTH_BASE_URL + "/token"; HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); + String requestUrl = httpServletRequest.getHeader("Origin"); + String redirectUri; + + if (requestUrl.contains("localhost:5173")) { + redirectUri = kakaoProperties.devRedirectUri(); + } else if (requestUrl.contains("sinitto.s3-website.ap-northeast-2.amazonaws.com")) { + redirectUri = kakaoProperties.redirectUri(); + } else { + throw new BadRequestException("해당 도메인에서는 카카오 로그인이 불가합니다. requestUrl : " + requestUrl); + } + LinkedMultiValueMap body = new LinkedMultiValueMap<>(); body.add("grant_type", "authorization_code"); body.add("client_id", kakaoProperties.clientId()); - body.add("redirect_uri", kakaoProperties.redirectUri()); + body.add("redirect_uri", redirectUri); body.add("code", authorizationCode); RequestEntity> request = new RequestEntity<>(body, diff --git a/src/main/java/com/example/sinitto/common/properties/KakaoProperties.java b/src/main/java/com/example/sinitto/common/properties/KakaoProperties.java index 42161ffd..0f95d9b6 100644 --- a/src/main/java/com/example/sinitto/common/properties/KakaoProperties.java +++ b/src/main/java/com/example/sinitto/common/properties/KakaoProperties.java @@ -5,6 +5,7 @@ @ConfigurationProperties(prefix = "kakao") public record KakaoProperties( String clientId, - String redirectUri + String redirectUri, + String devRedirectUri ) { } diff --git a/src/main/java/com/example/sinitto/member/service/MemberService.java b/src/main/java/com/example/sinitto/member/service/MemberService.java index ffa871b1..773f4cdb 100644 --- a/src/main/java/com/example/sinitto/member/service/MemberService.java +++ b/src/main/java/com/example/sinitto/member/service/MemberService.java @@ -14,6 +14,7 @@ import com.example.sinitto.member.repository.MemberRepository; import com.example.sinitto.point.entity.Point; import com.example.sinitto.point.repository.PointRepository; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @@ -47,8 +48,8 @@ public Long getMemberIdByToken(String token) { return member.getId(); } - public LoginResponse kakaoLogin(String authorizationCode) { - KakaoTokenResponse kakaoTokenResponse = kakaoApiService.getAccessToken(authorizationCode); + public LoginResponse kakaoLogin(String authorizationCode, HttpServletRequest httpServletRequest) { + KakaoTokenResponse kakaoTokenResponse = kakaoApiService.getAccessToken(authorizationCode, httpServletRequest); KakaoUserResponse kakaoUserResponse = kakaoApiService.getUserInfo(kakaoTokenResponse.accessToken()); String email = kakaoUserResponse.kakaoAccount().email();