Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.somemore.global.auth.oauth.handler.failure;
package com.somemore.global.auth.oauth.handler;

import com.somemore.global.auth.redirect.RedirectUseCase;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,11 +14,13 @@
@Slf4j
public class CustomOAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {

private final RedirectUseCase redirectUseCase;

private static final String FAILURE_PATH = "/error/login";

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
// TODO 프론트엔드와 협의
log.error("안녕 난 말하는 감자야");
log.error(exception.getMessage());
log.error(exception.getCause().getMessage());
redirectUseCase.redirect(request, response, FAILURE_PATH);
log.error("OAuth 로그인 실패: {}", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.somemore.global.auth.oauth.handler.success;
package com.somemore.global.auth.oauth.handler;

import com.somemore.global.auth.cookie.CookieUseCase;
import com.somemore.global.auth.jwt.domain.EncodedToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.stereotype.Service;

Expand All @@ -14,10 +15,33 @@
@Slf4j
public class RedirectService implements RedirectUseCase {

@Value("${app.front-url}")
private String frontUrl;

private final RedirectStrategy redirectStrategy;

private static final String ERROR_PATH = "/error";

@Override
public void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
redirectStrategy.sendRedirect(request, response, url);
public void redirect(HttpServletRequest request, HttpServletResponse response, String path) {
try {
redirectStrategy.sendRedirect(request, response, path);
} catch (IOException e) {
log.error("리디렉션 중 오류 발생 - 대상 URL: {}, 메시지: {}", path, e.getMessage());
handleRedirectFailure(request, response);
}
}

private void handleRedirectFailure(HttpServletRequest request, HttpServletResponse response) {
String fallbackUrl = buildFallbackUrl();
try {
redirectStrategy.sendRedirect(request, response, fallbackUrl);
} catch (IOException e) {
log.error("에러 페이지로 리디렉션 시도 중 오류 발생 - 메시지: {}", e.getMessage());
}
}

private String buildFallbackUrl() {
return frontUrl + RedirectService.ERROR_PATH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

public interface RedirectUseCase {
void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException;
void redirect(HttpServletRequest request, HttpServletResponse response, String path);
}
4 changes: 2 additions & 2 deletions src/main/java/com/somemore/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.somemore.global.auth.jwt.filter.JwtAuthFilter;
import com.somemore.global.auth.jwt.filter.JwtExceptionFilter;
import com.somemore.global.auth.jwt.usecase.GenerateTokensOnLoginUseCase;
import com.somemore.global.auth.oauth.handler.failure.CustomOAuthFailureHandler;
import com.somemore.global.auth.oauth.handler.success.CustomOAuthSuccessHandler;
import com.somemore.global.auth.oauth.handler.CustomOAuthFailureHandler;
import com.somemore.global.auth.oauth.handler.CustomOAuthSuccessHandler;
import com.somemore.global.auth.oauth.service.CustomOAuth2UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
Expand Down
Loading