OAuth2로그인을 진행하는데 NullPointerException
이 났다.
말도 안돼! 분명 sotv
메서드로 널이 아닌 것도 확인했는데...
null
일리가 없다고 생각했다.
package com.jsh.oauth_jwt_study.service;
import com.jsh.oauth_jwt_study.dto.*;
import com.jsh.oauth_jwt_study.entity.UserEntity;
import com.jsh.oauth_jwt_study.repository.UserRepository;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
private final UserRepository userRepository;
public CustomOAuth2UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
//OAuth2 로그인 성공 후 실행되는 메서드 (이후 SecurityConfig 등록 예정)
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
System.out.println("!!!!!!!!!!!!!!1userRequest = " + userRequest);
OAuth2User oAuth2User = super.loadUser(userRequest);
System.out.println("!!!!!!!!!!!!!!!!! oAuth2User = " + oAuth2User);
String registrationID = userRequest.getClientRegistration().getRegistrationId();
OAuth2Response oAuth2Response = null;
System.out.println("!!!!!!!!!!!!registrationID = " + registrationID);
Map<String, Object> attribute = oAuth2User.getAttributes();
System.out.println("attribute = " + attribute);
if (registrationID == "naver") {
oAuth2Response = new NaverResponse(attribute);
} else if (registrationID == "google") {
oAuth2Response = new GoogleResponse(attribute);
} else {
return null;
}
//회원가입 진행 여부와 회원가입 로직
System.out.println("!!!!!!!!!!!!!!! oAuth2Response = " + oAuth2Response);
String username = oAuth2Response.getProvider() + " " + oAuth2Response.getProviderId();
System.out.println("!!!!!!!!!!!!!!!username = " + username);
UserEntity existData = userRepository.findByUsername(username);
System.out.println("!!!!!!!!!!!!existData = " + existData);
if (existData == null) {
System.out.println(" 회원가입 진행~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
//처음 로그인한 경우 -> 회원가입을 시켜야한다.
UserEntity userEntity = new UserEntity();
userEntity.setUsername(username);
userEntity.setEmail(oAuth2Response.getEmail());
userEntity.setName(oAuth2Response.getName());
userEntity.setRole("ROLE_USER");
userRepository.save(userEntity);
UserDTO userDTO = new UserDTO();
userDTO.setUsername(username);
userDTO.setName(oAuth2Response.getName());
userDTO.setRole("ROLE_USER");
return new CustomOAuth2User(userDTO);
} else {
//이미 회원가입이 된 유저인 경우
//이메일이 업데이트 되었을 수도 있으니까
existData.setEmail(oAuth2Response.getEmail());
existData.setName(oAuth2Response.getName());
userRepository.save(existData);
UserDTO userDTO = new UserDTO();
userDTO.setUsername(existData.getUsername());
userDTO.setName(oAuth2Response.getName());
userDTO.setRole(existData.getRole());
return new CustomOAuth2User(userDTO);
}
}
@Override
public void setAttributesConverter(Converter<OAuth2UserRequest, Converter<Map<String, Object>, Map<String, Object>>> attributesConverter) {
super.setAttributesConverter(attributesConverter);
}
}
!!!!!!!!!!!!!!1userRequest = org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest@3f067f1f
!!!!!!!!!!!!!!!!! oAuth2User = Name: [{id=t3CIbWd0qvGF24X-A4O_xkXu8b386UXG0OnoQxwLfJs, email=jin011228@naver.com, name=진서현}], Granted Authorities: [[OAUTH2_USER]], User Attributes: [{resultcode=00, message=success, response={id=t3CIbWd0qvGF24X-A4O_xkXu8b386UXG0OnoQxwLfJs, email=jin011228@naver.com, name=진서현}}]
!!!!!!!!!!!!registrationID = naver
attribute = {resultcode=00, message=success, response={id=t3CIbWd0qvGF24X-A4O_xkXu8b386UXG0OnoQxwLfJs, email=jin011228@naver.com, name=진서현}}
2024-06-20T17:45:32.989+09:00 ERROR 8212 --- [nio-8000-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/oauth_jwt] threw exception
java.lang.NullPointerException: Cannot invoke "org.springframework.security.oauth2.core.user.OAuth2User.getAuthorities()" because "oauth2User" is null
at org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationProvider.authenticate(OAuth2LoginAuthenticationProvider.java:122) ~[spring-security-oauth2-client-6.3.0.jar:6.3.0]
내가 메서드를 잘 못 썼다.
문자열 비교시 ==
이 아니라 .equals()
를 사용해야했다.
"google"
이던 "naver"
이던 모두 else문
에 걸리기 때문에 어떤 값도 할당이 되지 않는 것 이었다.
if (registrationID.equals("naver")) {
oAuth2Response = new NaverResponse(attribute);
} else if (registrationID.equals("google")) {
oAuth2Response = new GoogleResponse(attribute);
} else {
return null;
}