-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2AuthenticationFailureHandler.java
More file actions
34 lines (26 loc) · 1.34 KB
/
OAuth2AuthenticationFailureHandler.java
File metadata and controls
34 lines (26 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.oauth2.security;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException;
@Slf4j
@Component
public class OAuth2AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
@Value("${cors.allowed-origins}")
private String frontendUrl;
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException {
String targetUrl = UriComponentsBuilder.fromUriString(frontendUrl + "/login")
.queryParam("error", "oauth2_authentication_failed")
.queryParam("message", exception.getLocalizedMessage())
.build().toUriString();
log.error("OAuth2 인증 실패: {}", exception.getMessage());
getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
}