forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d022adc
commit a349d85
Showing
14 changed files
with
305 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
public class CustomOAuth2User extends DefaultOAuth2User { | ||
|
||
public CustomOAuth2User(Collection<? extends GrantedAuthority> authorities, | ||
Map<String, Object> attributes, String nameAttributeKey, String email) { | ||
super(authorities, attributes, nameAttributeKey); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/capstone/maru/dto/KakaoOAuth2UserInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import java.util.Map; | ||
|
||
public class KakaoOAuth2UserInfo extends OAuth2UserInfo { | ||
|
||
public KakaoOAuth2UserInfo(Map<String, Object> attributes) { | ||
super(attributes); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return String.valueOf(attributes.get("id")); | ||
} | ||
|
||
@Override | ||
public String getNickname() { | ||
return null; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/capstone/maru/dto/NaverOAuth2UserInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import java.util.Map; | ||
|
||
public class NaverOAuth2UserInfo extends OAuth2UserInfo { | ||
|
||
public NaverOAuth2UserInfo(Map<String, Object> attributes) { | ||
super(attributes); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return String.valueOf(attributes.get("id")); | ||
} | ||
|
||
@Override | ||
public String getNickname() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class OAuth2UserInfo { | ||
|
||
protected Map<String, Object> attributes; | ||
|
||
public OAuth2UserInfo(Map<String, Object> attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
public abstract String getId(); //소셜 식별 값 : 구글 - "sub", 카카오 - "id", 네이버 - "id" | ||
|
||
public abstract String getNickname(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.capstone.maru.domain.MemberAccount; | ||
|
||
@Getter | ||
public class OAuthAttributes { | ||
|
||
private String nameAttributeKey; // OAuth2 로그인 진행 시 키가 되는 필드 값, PK와 같은 의미 | ||
private OAuth2UserInfo oauth2UserInfo; // 소셜 타입별 로그인 유저 정보(닉네임, 이메일, 프로필 사진 등등) | ||
|
||
@Builder | ||
public OAuthAttributes(String nameAttributeKey, OAuth2UserInfo oauth2UserInfo) { | ||
this.nameAttributeKey = nameAttributeKey; | ||
this.oauth2UserInfo = oauth2UserInfo; | ||
} | ||
|
||
/** | ||
* SocialType에 맞는 메소드 호출하여 OAuthAttributes 객체 반환 파라미터 : userNameAttributeName -> OAuth2 로그인 시 | ||
* 키(PK)가 되는 값 / attributes : OAuth 서비스의 유저 정보들 소셜별 of 메소드(ofGoogle, ofKaKao, ofNaver)들은 각각 소셜 | ||
* 로그인 API에서 제공하는 회원의 식별값(id), attributes, nameAttributeKey를 저장 후 build | ||
*/ | ||
public static OAuthAttributes of(SocialType socialType, | ||
String userNameAttributeName, Map<String, Object> attributes) { | ||
|
||
if (socialType == SocialType.NAVER) { | ||
return ofNaver(userNameAttributeName, attributes); | ||
} | ||
if (socialType == SocialType.KAKAO) { | ||
return ofKakao(userNameAttributeName, attributes); | ||
} | ||
return null; | ||
} | ||
|
||
private static OAuthAttributes ofKakao(String userNameAttributeName, | ||
Map<String, Object> attributes) { | ||
return OAuthAttributes.builder() | ||
.nameAttributeKey(userNameAttributeName) | ||
.oauth2UserInfo(new KakaoOAuth2UserInfo(attributes)) | ||
.build(); | ||
} | ||
|
||
public static OAuthAttributes ofNaver(String userNameAttributeName, | ||
Map<String, Object> attributes) { | ||
return OAuthAttributes.builder() | ||
.nameAttributeKey(userNameAttributeName) | ||
.oauth2UserInfo(new NaverOAuth2UserInfo(attributes)) | ||
.build(); | ||
} | ||
|
||
/** | ||
* of메소드로 OAuthAttributes 객체가 생성되어, 유저 정보들이 담긴 OAuth2UserInfo가 소셜 타입별로 주입된 상태 OAuth2UserInfo에서 | ||
* socialId(식별값), nickname을 가져와서 build | ||
*/ | ||
public MemberAccount toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo) { | ||
return MemberAccount.builder() | ||
.memberId(oauth2UserInfo.getId()) | ||
.nickname(oauth2UserInfo.getNickname()) | ||
.socialType(socialType) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.capstone.maru.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum Role { | ||
|
||
GUEST("ROLE_GUEST"), USER("ROLE_USER"); | ||
|
||
private final String key; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.capstone.maru.dto; | ||
|
||
public enum SocialType { | ||
KAKAO, NAVER | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/org/capstone/maru/repository/MemberAccountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package org.capstone.maru.repository; | ||
|
||
import java.util.Optional; | ||
import org.capstone.maru.domain.MemberAccount; | ||
import org.capstone.maru.dto.SocialType; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface MemberAccountRepository extends JpaRepository<MemberAccount, String> { | ||
|
||
Optional<MemberAccount> findBySocialTypeAndSocialId(SocialType socialType, String id); | ||
} |
Oops, something went wrong.