d36choi / cossc-api

2 stars 0 forks source link

CustomOAuth2UserService User조회 key 값 설정 #2

Closed d36choi closed 1 year ago

d36choi commented 1 year ago
// 획득한 유저정보를 Java Model과 맵핑하고 프로세스 진행
  private OAuth2User process(OAuth2UserRequest oAuth2UserRequest, OAuth2User oAuth2User) {
    AuthProvider authProvider = AuthProvider.valueOf(oAuth2UserRequest.getClientRegistration().getRegistrationId().toUpperCase());
    OAuth2UserInfo userInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(authProvider, oAuth2User.getAttributes());

    if (userInfo.getEmail().isEmpty()) {
      throw new OAuthProcessingException("Email not found from OAuth2 provider");
    }
    Optional<User> userOptional = userRepository.findByEmail(userInfo.getEmail());
    User user;

google은 email로 조회 가능하지만 github의 경우 UserInfo attribute에 email=null 일 수 있음. 즉, User 정보를 조회할때 Email을 기준으로 조회하는 것은 적합하지 않음.

google의 user식별용 nameAttributeKey 는 "sub" / github는 "id" 임. 차라리 위 키들을 필드로 하나 설정해두고 그걸 통해 조회하도록 수정해야할 듯 함.

비슷한 내용

@chlee1252