Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 애플 oauth 로그인 추가 #164

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/config/OauthConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.readyvery.readyverydemo.config;

import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class OauthConfig {
public static final String KAKAO_NAME = "kakao";
public static final String APPLE_NAME = "apple";
public static final String GOOGLE_NAME = "google";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.readyvery.readyverydemo.domain;

public enum SocialType {
KAKAO, NAVER, GOOGLE
KAKAO, NAVER, GOOGLE, APPLE
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.readyvery.readyverydemo.domain.Role;
import com.readyvery.readyverydemo.domain.SocialType;
import com.readyvery.readyverydemo.domain.UserInfo;
import com.readyvery.readyverydemo.security.oauth2.userinfo.AppleOAuth2UserInfo;
import com.readyvery.readyverydemo.security.oauth2.userinfo.GoogleOAuth2UserInfo;
import com.readyvery.readyverydemo.security.oauth2.userinfo.KakaoOAuth2UserInfo;
import com.readyvery.readyverydemo.security.oauth2.userinfo.OAuth2UserInfo;
Expand Down Expand Up @@ -40,6 +41,9 @@ public static OAuthAttributes of(SocialType socialType,
if (socialType == SocialType.KAKAO) {
return ofKakao(userNameAttributeName, attributes);
}
if (socialType == SocialType.APPLE) {
return ofApple(userNameAttributeName, attributes);
}
return ofGoogle(userNameAttributeName, attributes);

}
Expand All @@ -58,6 +62,13 @@ public static OAuthAttributes ofGoogle(String userNameAttributeName, Map<String,
.build();
}

public static OAuthAttributes ofApple(String userNameAttributeName, Map<String, Object> attributes) {
return OAuthAttributes.builder()
.nameAttributeKey(userNameAttributeName)
.oauth2UserInfo(new AppleOAuth2UserInfo(attributes))
.build();
}

/**
* of메소드로 OAuthAttributes 객체가 생성되어, 유저 정보들이 담긴 OAuth2UserInfo가 소셜 타입별로 주입된 상태
* OAuth2UserInfo에서 socialId(식별값), nickname, imageUrl을 가져와서 build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.readyvery.readyverydemo.security.oauth2.service;

import static com.readyvery.readyverydemo.config.OauthConfig.*;

import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -27,8 +29,6 @@ public class CustomOAuth2UserService implements OAuth2UserService<OAuth2UserRequ

private final UserRepository userRepository;

private static final String KAKAO = "kakao";

@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {

Expand Down Expand Up @@ -68,8 +68,10 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
}

private SocialType getSocialType(String registrationId) {
if (KAKAO.equals(registrationId)) {
if (KAKAO_NAME.equals(registrationId)) {
return SocialType.KAKAO;
} else if (APPLE_NAME.equals(registrationId)) {
return SocialType.APPLE;
}
return SocialType.GOOGLE;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.readyvery.readyverydemo.security.oauth2.userinfo;

import java.util.Map;

public class AppleOAuth2UserInfo extends OAuth2UserInfo {

public AppleOAuth2UserInfo(Map<String, Object> attributes) {
super(attributes);
}

@Override
public String getId() {
return (String)attributes.get("sub");
}

@Override
public String getNickName() {
return (String)attributes.get("name");
}

@Override
public String getEmail() {
return (String)attributes.get("email");
}

@Override
public String getImageUrl() {
return (String)attributes.get("picture");
}

@Override
public String getPhoneNumber() {
return "readyvery";
}

@Override
public String getBirth() {
return "readyvery";
}

@Override
public String getAge() {
return "readyvery";
}
}
Loading