Skip to content

Commit ae487de

Browse files
authored
[Feat] 카카오 로그인 시 동적 redirect URI 허용 및 검증 추가 (#67)
1 parent 437f9d0 commit ae487de

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

src/main/java/com/moa/moa_server/domain/auth/controller/AuthController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ public class AuthController {
3333
@PostMapping("/login/oauth")
3434
public ResponseEntity<ApiResponse> oAuthLogin(
3535
@RequestBody LoginRequest request,
36+
@RequestHeader("X-Redirect-Uri") String redirectUri,
3637
HttpServletResponse response
3738
) {
3839
// OAuth 로그인 서비스 로직 수행
39-
LoginResult dto = authService.login(request.provider(), request.code());
40+
LoginResult dto = authService.login(request.provider(), request.code(), redirectUri);
4041
LoginResponse loginResponseDto = dto.loginResponseDto();
4142
String refreshToken = dto.refreshToken();
4243

src/main/java/com/moa/moa_server/domain/auth/handler/AuthErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public enum AuthErrorCode implements BaseErrorCode {
1313
INVALID_PROVIDER(HttpStatus.BAD_REQUEST),
1414
KAKAO_TOKEN_FAILED(HttpStatus.UNAUTHORIZED),
1515
KAKAO_USERINFO_FAILED(HttpStatus.UNAUTHORIZED),
16-
OAUTH_NOT_FOUND(HttpStatus.NOT_FOUND),;
16+
OAUTH_NOT_FOUND(HttpStatus.NOT_FOUND),
17+
INVALID_REDIRECT_URI(HttpStatus.BAD_REQUEST),;
1718

1819
private final HttpStatus status;
1920

src/main/java/com/moa/moa_server/domain/auth/service/AuthService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,35 @@
1414
import org.springframework.transaction.annotation.Transactional;
1515

1616
import java.util.Map;
17+
import java.util.Set;
1718

1819
@Slf4j
1920
@Service
2021
@RequiredArgsConstructor
2122
public class AuthService {
2223

24+
private static final Set<String> ALLOWED_REDIRECT_URIS = Set.of(
25+
"http://localhost:5173/auth/callback",
26+
"https://b4z.moagenda.com/auth/callback",
27+
"http://localhost:8080/api/v1/auth/login/oauth"
28+
);
29+
2330
private final Map<String, OAuthLoginStrategy> strategies;
2431
private final JwtTokenService jwtTokenService;
2532
private final RefreshTokenService refreshTokenService;
2633

27-
public LoginResult login(String provider, String code) {
34+
public LoginResult login(String provider, String code, String redirectUri) {
2835
if (!OAuth.ProviderCode.isSupported(provider)) {
2936
throw new AuthException(AuthErrorCode.INVALID_PROVIDER);
3037
}
3138

39+
if (!ALLOWED_REDIRECT_URIS.contains(redirectUri)) {
40+
log.error("redirectUri: {} is not allowed. ALLOWED_REDIRECT_URIS: {}", redirectUri, ALLOWED_REDIRECT_URIS);
41+
throw new AuthException(AuthErrorCode.INVALID_REDIRECT_URI);
42+
}
43+
3244
OAuthLoginStrategy strategy = strategies.get(provider.toLowerCase());
33-
return strategy.login(code);
45+
return strategy.login(code, redirectUri);
3446
}
3547

3648
@Transactional(readOnly = true)

src/main/java/com/moa/moa_server/domain/auth/service/strategy/KakaoOAuthLoginStrategy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public class KakaoOAuthLoginStrategy implements OAuthLoginStrategy {
6060

6161
@Transactional
6262
@Override
63-
public LoginResult login(String code) {
63+
public LoginResult login(String code, String redirectUri) {
6464
// 인가코드로 카카오 액세스 토큰 요청
65-
String kakaoAccessToken = getAccessToken(code);
65+
String kakaoAccessToken = getAccessToken(code, redirectUri);
6666

6767
// 카카오 액세스 토큰으로 사용자 정보 요청
6868
Long kakaoId = getUserInfo(kakaoAccessToken);
@@ -95,15 +95,15 @@ public LoginResult login(String code) {
9595
return new LoginResult(loginResponseDto, refreshToken);
9696
}
9797

98-
private String getAccessToken(String code) {
98+
private String getAccessToken(String code, String redirectUri) {
9999

100100
HttpHeaders headers = new HttpHeaders();
101101
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
102102

103103
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
104104
body.add("grant_type", "authorization_code");
105105
body.add("client_id", kakaoClientId);
106-
body.add("redirect_uri", kakaoRedirectUri);
106+
body.add("redirect_uri", redirectUri);
107107
body.add("code", code);
108108

109109
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);

src/main/java/com/moa/moa_server/domain/auth/service/strategy/OAuthLoginStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import com.moa.moa_server.domain.auth.dto.model.LoginResult;
44

55
public interface OAuthLoginStrategy {
6-
LoginResult login(String code);
6+
LoginResult login(String code, String redirectUri);
77
void unlink(Long oauthId);
88
}

src/main/resources/application-dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ mock:
2727

2828
logging:
2929
level:
30+
root: INFO
3031
org.springframework.security: DEBUG

0 commit comments

Comments
 (0)