Skip to content

Commit 35fa1c3

Browse files
authored
Feature/261 리디렉션 수정 (#264)
* feat(redirection): 로그인 리디렉션 수정 - 로직 개선 - 실패 케이스 추가 * refactor: 패키지 이동 * refactor: 깃허브 경고 개행
1 parent 6b8c263 commit 35fa1c3

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.somemore.global.auth.oauth.handler.failure;
1+
package com.somemore.global.auth.oauth.handler;
22

3+
import com.somemore.global.auth.redirect.RedirectUseCase;
34
import jakarta.servlet.http.HttpServletRequest;
45
import jakarta.servlet.http.HttpServletResponse;
56
import lombok.RequiredArgsConstructor;
@@ -13,11 +14,13 @@
1314
@Slf4j
1415
public class CustomOAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {
1516

17+
private final RedirectUseCase redirectUseCase;
18+
19+
private static final String FAILURE_PATH = "/error/login";
20+
1621
@Override
1722
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
18-
// TODO 프론트엔드와 협의
19-
log.error("안녕 난 말하는 감자야");
20-
log.error(exception.getMessage());
21-
log.error(exception.getCause().getMessage());
23+
redirectUseCase.redirect(request, response, FAILURE_PATH);
24+
log.error("OAuth 로그인 실패: {}", exception.getMessage());
2225
}
2326
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.somemore.global.auth.oauth.handler.success;
1+
package com.somemore.global.auth.oauth.handler;
22

33
import com.somemore.global.auth.cookie.CookieUseCase;
44
import com.somemore.global.auth.jwt.domain.EncodedToken;

src/main/java/com/somemore/global/auth/redirect/RedirectService.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.servlet.http.HttpServletResponse;
55
import lombok.RequiredArgsConstructor;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.security.web.RedirectStrategy;
89
import org.springframework.stereotype.Service;
910

@@ -14,10 +15,33 @@
1415
@Slf4j
1516
public class RedirectService implements RedirectUseCase {
1617

18+
@Value("${app.front-url}")
19+
private String frontUrl;
20+
1721
private final RedirectStrategy redirectStrategy;
1822

23+
private static final String ERROR_PATH = "/error";
24+
1925
@Override
20-
public void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
21-
redirectStrategy.sendRedirect(request, response, url);
26+
public void redirect(HttpServletRequest request, HttpServletResponse response, String path) {
27+
try {
28+
redirectStrategy.sendRedirect(request, response, path);
29+
} catch (IOException e) {
30+
log.error("리디렉션 중 오류 발생 - 대상 URL: {}, 메시지: {}", path, e.getMessage());
31+
handleRedirectFailure(request, response);
32+
}
33+
}
34+
35+
private void handleRedirectFailure(HttpServletRequest request, HttpServletResponse response) {
36+
String fallbackUrl = buildFallbackUrl();
37+
try {
38+
redirectStrategy.sendRedirect(request, response, fallbackUrl);
39+
} catch (IOException e) {
40+
log.error("에러 페이지로 리디렉션 시도 중 오류 발생 - 메시지: {}", e.getMessage());
41+
}
42+
}
43+
44+
private String buildFallbackUrl() {
45+
return frontUrl + RedirectService.ERROR_PATH;
2246
}
2347
}

src/main/java/com/somemore/global/auth/redirect/RedirectUseCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import jakarta.servlet.http.HttpServletRequest;
44
import jakarta.servlet.http.HttpServletResponse;
5-
import java.io.IOException;
65

76
public interface RedirectUseCase {
8-
void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException;
7+
void redirect(HttpServletRequest request, HttpServletResponse response, String path);
98
}

src/main/java/com/somemore/global/config/SecurityConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.somemore.global.auth.jwt.filter.JwtAuthFilter;
77
import com.somemore.global.auth.jwt.filter.JwtExceptionFilter;
88
import com.somemore.global.auth.jwt.usecase.GenerateTokensOnLoginUseCase;
9-
import com.somemore.global.auth.oauth.handler.failure.CustomOAuthFailureHandler;
10-
import com.somemore.global.auth.oauth.handler.success.CustomOAuthSuccessHandler;
9+
import com.somemore.global.auth.oauth.handler.CustomOAuthFailureHandler;
10+
import com.somemore.global.auth.oauth.handler.CustomOAuthSuccessHandler;
1111
import com.somemore.global.auth.oauth.service.CustomOAuth2UserService;
1212
import lombok.RequiredArgsConstructor;
1313
import org.springframework.context.annotation.Bean;

0 commit comments

Comments
 (0)