diff --git a/src/main/java/hs/kr/backend/devpals/infra/oauth2/CustomOauth2UserService.java b/src/main/java/hs/kr/backend/devpals/infra/oauth2/CustomOauth2UserService.java index ae8d06d..f9a36c8 100644 --- a/src/main/java/hs/kr/backend/devpals/infra/oauth2/CustomOauth2UserService.java +++ b/src/main/java/hs/kr/backend/devpals/infra/oauth2/CustomOauth2UserService.java @@ -4,6 +4,7 @@ import hs.kr.backend.devpals.domain.user.repository.UserRepository; import hs.kr.backend.devpals.global.exception.CustomException; import hs.kr.backend.devpals.global.exception.ErrorException; +import hs.kr.backend.devpals.domain.user.principal.CustomUserDetails; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -36,8 +37,8 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) { // 요청에 provider 저장 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - if (attributes != null) { - HttpServletRequest request = attributes.getRequest(); + HttpServletRequest request = attributes != null ? attributes.getRequest() : null; + if (request != null) { request.setAttribute("provider", provider); } @@ -48,8 +49,15 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) { throw new IllegalArgumentException("소셜 로그인 응답에서 email을 찾을 수 없습니다."); } - UserEntity user = userRepository.findByEmail(email) - .orElseGet(() -> new UserEntity(email, "SOCIAL_LOGIN_USER", name, true)); + UserEntity user; + + if ("github-auth".equals(provider) && request != null && request.getUserPrincipal() instanceof CustomUserDetails principal) { + user = userRepository.findById(principal.getId()) + .orElseThrow(() -> new CustomException(ErrorException.USER_NOT_FOUND)); + } else { + user = userRepository.findByEmail(email) + .orElseGet(() -> new UserEntity(email, "SOCIAL_LOGIN_USER", name, true)); + } if ("github".equals(provider) || "github-auth".equals(provider)) { String githubUrl = oAuth2User.getAttribute("html_url"); @@ -100,7 +108,7 @@ public static String extractName(String provider, OAuth2User oAuth2User) { return oAuth2User.getAttribute("name"); case "kakao": Map kakaoAccount = oAuth2User.getAttribute("kakao_account"); - if (kakaoAccount == null) return null; + if (kakaoAccount == null) return null; Map profile = (Map) kakaoAccount.get("profile"); return profile != null ? (String) profile.get("nickname") : null; case "naver": @@ -131,18 +139,12 @@ private static String fetchPrimaryEmailFromGithub(OAuth2UserRequest userRequest) ); List> emailList = response.getBody(); - if (emailList != null) { - for (Map emailInfo : emailList) { - } - } else { - } + if (emailList == null) return null; - String primaryEmail = emailList.stream() + return emailList.stream() .filter(e -> Boolean.TRUE.equals(e.get("primary")) && Boolean.TRUE.equals(e.get("verified"))) .map(e -> (String) e.get("email")) .findFirst() .orElse(null); - - return primaryEmail; } } diff --git a/src/main/java/hs/kr/backend/devpals/infra/oauth2/Oauth2LoginSuccessHandler.java b/src/main/java/hs/kr/backend/devpals/infra/oauth2/Oauth2LoginSuccessHandler.java index 4f132ac..7995c5c 100644 --- a/src/main/java/hs/kr/backend/devpals/infra/oauth2/Oauth2LoginSuccessHandler.java +++ b/src/main/java/hs/kr/backend/devpals/infra/oauth2/Oauth2LoginSuccessHandler.java @@ -10,6 +10,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; @@ -30,17 +32,26 @@ public class Oauth2LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHan public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { - OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); - String email = (String) oAuth2User.getAttributes().get("email"); - String provider = ((OAuth2AuthenticationToken) authentication).getAuthorizedClientRegistrationId(); - if (email == null) { - throw new CustomException(ErrorException.USER_NOT_FOUND); - } - if ("github-auth".equals(provider)) { - String githubUrl = (String) oAuth2User.getAttributes().get("html_url"); + OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); + String githubUrl = oAuth2User.getAttribute("html_url"); + + String email = oAuth2User.getAttribute("email"); + + if (email == null || email.isBlank()) { + Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if (principal instanceof UserDetails userDetails) { + email = userDetails.getUsername(); + } else { + email = SecurityContextHolder.getContext().getAuthentication().getName(); + } + } + + if (email == null || email.isBlank()) { + throw new CustomException(ErrorException.USER_NOT_FOUND); + } oauthUserService.updateGithubUrl(email, githubUrl); @@ -48,6 +59,14 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo return; } + // 일반 소셜 로그인 처리 + OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal(); + String email = oAuth2User.getAttribute("email"); + + if (email == null || email.isBlank()) { + throw new CustomException(ErrorException.USER_NOT_FOUND); + } + UserEntity user = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorException.USER_NOT_FOUND));