Skip to content

Commit

Permalink
feat: 소셜 로그인 실패 handler 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dong2ast committed Aug 13, 2023
1 parent 0b50264 commit 301c007
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/sophy/sophy/config/auth/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.config.auth.common.CustomOAuth2UserService;
import org.sophy.sophy.config.auth.common.OAuth2LoginFailureHandler;
import org.sophy.sophy.config.auth.common.OAuth2LoginSuccessHandler;
import org.sophy.sophy.jwt.JwtAccessDeniedHandler;
import org.sophy.sophy.jwt.JwtAuthenticationEntryPoint;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class SecurityConfig {
private final RedisTemplate redisTemplate;
private final CustomOAuth2UserService customOAuth2UserService;
private final OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler;
private final OAuth2LoginFailureHandler oAuth2LoginFailureHandler;

@Bean
public PasswordEncoder passwordEncoder() {
Expand Down Expand Up @@ -76,6 +78,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

http.oauth2Login()
.successHandler(oAuth2LoginSuccessHandler) // 동의하고 계속하기를 눌렀을 때 Handler 설정
.failureHandler(oAuth2LoginFailureHandler) // 소셜 로그인 실패 시 핸들러 설정
.userInfoEndpoint().userService(customOAuth2UserService) // customUserService 설정
.and()
.permitAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sophy.sophy.config.auth.common;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OAuth2LoginFailureHandler implements AuthenticationFailureHandler {

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().write("소셜 로그인이 실패하였습니다.");
log.error("소셜 로그인에 실패했습니다. 에러메세지 : {}", exception.getMessage());
}
}

0 comments on commit 301c007

Please sign in to comment.