Skip to content

Commit

Permalink
Merge pull request #182 from Funssion-SWM/user
Browse files Browse the repository at this point in the history
fix: 일반로그인계정으로 구글로그인 시 failure handler 동작하게 수정
  • Loading branch information
goathoon authored Nov 14, 2023
2 parents 5d36a53 + ad01170 commit dc194f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Funssion.Inforum.access_handler;

import Funssion.Inforum.common.dto.IsSuccessResponseDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
public class OAuthAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
makeFailureResponseBody(response);
}
private void makeFailureResponseBody(HttpServletResponse response) throws IOException {
String failureResponse = convertFailureObjectToString();
response.setStatus(response.SC_UNAUTHORIZED);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(failureResponse);
}

private String convertFailureObjectToString() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
IsSuccessResponseDto isSuccessResponseDto = new IsSuccessResponseDto(false, "같은 이메일의 일반 로그인 계정이 존재합니다.");
String successResponse = objectMapper.writeValueAsString(isSuccessResponseDto);
return successResponse;
}
}
9 changes: 4 additions & 5 deletions src/main/java/Funssion/Inforum/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package Funssion.Inforum.config;

import Funssion.Inforum.access_handler.AuthenticationSuccessHandler;
import Funssion.Inforum.access_handler.JwtAccessDeniedHandler;
import Funssion.Inforum.access_handler.JwtAuthenticationEntryPoint;
import Funssion.Inforum.access_handler.NonSocialLoginFailureHandler;
import Funssion.Inforum.access_handler.*;
import Funssion.Inforum.domain.member.service.OAuthService;
import Funssion.Inforum.jwt.JwtSecurityConfig;
import Funssion.Inforum.jwt.TokenProvider;
Expand Down Expand Up @@ -42,6 +39,7 @@ public class SecurityConfig {
private final ClientRegistrationRepository clientRegistrationRepository;
private final OAuthService oAuthService;
private final AuthenticationSuccessHandler authenticationSuccessHandler;
private final OAuthAuthenticationFailureHandler oAuthAuthenticationFailureHandler;
@Value("${jwt.domain}") private String domain;


Expand Down Expand Up @@ -111,7 +109,8 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.oauth2Login(oauth2 -> oauth2
.clientRegistrationRepository(clientRegistrationRepository)
.userInfoEndpoint(it -> it.userService(oAuthService))
.successHandler(authenticationSuccessHandler))
.successHandler(authenticationSuccessHandler)
.failureHandler(oAuthAuthenticationFailureHandler))
.formLogin((formLogin) -> formLogin
.loginProcessingUrl("/users/login")
.failureHandler(nonSocialLoginFailureHandler)
Expand Down

0 comments on commit dc194f9

Please sign in to comment.