From f065456d43b0cbaf449b99e6a158b28a9743cf2f Mon Sep 17 00:00:00 2001 From: leejh7 <777joonho@kookmin.ac.kr> Date: Sun, 10 Mar 2024 23:13:16 +0900 Subject: [PATCH] =?UTF-8?q?feat/refactor=20#2=20-=20application=20yaml=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=88=98=EC=A0=95=20/=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat - naver redirect uri 변경 refactor - security 패키지 생성 --- .../capstone/maru/dto/CustomOAuth2User.java | 14 ---- .../maru/dto/KakaoOAuth2UserInfo.java | 21 ------ .../maru/dto/NaverOAuth2UserInfo.java | 20 ------ .../org/capstone/maru/dto/OAuth2UserInfo.java | 17 ----- .../capstone/maru/dto/OAuthAttributes.java | 65 ------------------- src/main/java/org/capstone/maru/dto/Role.java | 14 ---- .../CustomOAuth2UserService.java | 0 .../security/KakaoOAuth2Response.java | 0 .../maru/security/OAuth2Response.java | 43 ++++++++++++ .../security/SharedPostPrincipal.java | 0 .../constant}/SocialType.java | 0 src/main/resources/application.yaml | 2 +- 12 files changed, 44 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/org/capstone/maru/dto/CustomOAuth2User.java delete mode 100644 src/main/java/org/capstone/maru/dto/KakaoOAuth2UserInfo.java delete mode 100644 src/main/java/org/capstone/maru/dto/NaverOAuth2UserInfo.java delete mode 100644 src/main/java/org/capstone/maru/dto/OAuth2UserInfo.java delete mode 100644 src/main/java/org/capstone/maru/dto/OAuthAttributes.java delete mode 100644 src/main/java/org/capstone/maru/dto/Role.java rename src/main/java/org/capstone/maru/{service => security}/CustomOAuth2UserService.java (100%) rename src/main/java/org/capstone/maru/{dto => }/security/KakaoOAuth2Response.java (100%) create mode 100644 src/main/java/org/capstone/maru/security/OAuth2Response.java rename src/main/java/org/capstone/maru/{dto => }/security/SharedPostPrincipal.java (100%) rename src/main/java/org/capstone/maru/{dto => security/constant}/SocialType.java (100%) diff --git a/src/main/java/org/capstone/maru/dto/CustomOAuth2User.java b/src/main/java/org/capstone/maru/dto/CustomOAuth2User.java deleted file mode 100644 index d050359fb8..0000000000 --- a/src/main/java/org/capstone/maru/dto/CustomOAuth2User.java +++ /dev/null @@ -1,14 +0,0 @@ -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 authorities, - Map attributes, String nameAttributeKey, String email) { - super(authorities, attributes, nameAttributeKey); - } -} diff --git a/src/main/java/org/capstone/maru/dto/KakaoOAuth2UserInfo.java b/src/main/java/org/capstone/maru/dto/KakaoOAuth2UserInfo.java deleted file mode 100644 index 240431a460..0000000000 --- a/src/main/java/org/capstone/maru/dto/KakaoOAuth2UserInfo.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.capstone.maru.dto; - -import java.util.Map; - -public class KakaoOAuth2UserInfo extends OAuth2UserInfo { - - public KakaoOAuth2UserInfo(Map attributes) { - super(attributes); - } - - @Override - public String getId() { - return String.valueOf(attributes.get("id")); - } - - @Override - public String getNickname() { - return null; - } - -} diff --git a/src/main/java/org/capstone/maru/dto/NaverOAuth2UserInfo.java b/src/main/java/org/capstone/maru/dto/NaverOAuth2UserInfo.java deleted file mode 100644 index b1c0a11e7f..0000000000 --- a/src/main/java/org/capstone/maru/dto/NaverOAuth2UserInfo.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.capstone.maru.dto; - -import java.util.Map; - -public class NaverOAuth2UserInfo extends OAuth2UserInfo { - - public NaverOAuth2UserInfo(Map attributes) { - super(attributes); - } - - @Override - public String getId() { - return String.valueOf(attributes.get("id")); - } - - @Override - public String getNickname() { - return null; - } -} diff --git a/src/main/java/org/capstone/maru/dto/OAuth2UserInfo.java b/src/main/java/org/capstone/maru/dto/OAuth2UserInfo.java deleted file mode 100644 index 595161562d..0000000000 --- a/src/main/java/org/capstone/maru/dto/OAuth2UserInfo.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.capstone.maru.dto; - -import java.util.Map; - -public abstract class OAuth2UserInfo { - - protected Map attributes; - - public OAuth2UserInfo(Map attributes) { - this.attributes = attributes; - } - - public abstract String getId(); //소셜 식별 값 : 구글 - "sub", 카카오 - "id", 네이버 - "id" - - public abstract String getNickname(); - -} diff --git a/src/main/java/org/capstone/maru/dto/OAuthAttributes.java b/src/main/java/org/capstone/maru/dto/OAuthAttributes.java deleted file mode 100644 index 99104b527b..0000000000 --- a/src/main/java/org/capstone/maru/dto/OAuthAttributes.java +++ /dev/null @@ -1,65 +0,0 @@ -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 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 attributes) { - return OAuthAttributes.builder() - .nameAttributeKey(userNameAttributeName) - .oauth2UserInfo(new KakaoOAuth2UserInfo(attributes)) - .build(); - } - - public static OAuthAttributes ofNaver(String userNameAttributeName, - Map 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(); - } - -} diff --git a/src/main/java/org/capstone/maru/dto/Role.java b/src/main/java/org/capstone/maru/dto/Role.java deleted file mode 100644 index 4871045593..0000000000 --- a/src/main/java/org/capstone/maru/dto/Role.java +++ /dev/null @@ -1,14 +0,0 @@ -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; -} - diff --git a/src/main/java/org/capstone/maru/service/CustomOAuth2UserService.java b/src/main/java/org/capstone/maru/security/CustomOAuth2UserService.java similarity index 100% rename from src/main/java/org/capstone/maru/service/CustomOAuth2UserService.java rename to src/main/java/org/capstone/maru/security/CustomOAuth2UserService.java diff --git a/src/main/java/org/capstone/maru/dto/security/KakaoOAuth2Response.java b/src/main/java/org/capstone/maru/security/KakaoOAuth2Response.java similarity index 100% rename from src/main/java/org/capstone/maru/dto/security/KakaoOAuth2Response.java rename to src/main/java/org/capstone/maru/security/KakaoOAuth2Response.java diff --git a/src/main/java/org/capstone/maru/security/OAuth2Response.java b/src/main/java/org/capstone/maru/security/OAuth2Response.java new file mode 100644 index 0000000000..da95a7f2d2 --- /dev/null +++ b/src/main/java/org/capstone/maru/security/OAuth2Response.java @@ -0,0 +1,43 @@ +package org.capstone.maru.security; + +import java.util.Map; +import lombok.Builder; +import lombok.Getter; +import org.capstone.maru.domain.MemberAccount; +import org.capstone.maru.security.constant.SocialType; + +@Getter +public abstract class OAuthAttributes { + + /** + * SocialType에 맞는 메소드 호출하여 OAuthAttributes 객체 반환 파라미터 : userNameAttributeName -> OAuth2 로그인 시 + * 키(PK)가 되는 값 / attributes : OAuth 서비스의 유저 정보들 소셜별 of 메소드(ofGoogle, ofKaKao, ofNaver)들은 각각 소셜 + * 로그인 API에서 제공하는 회원의 식별값(id), attributes, nameAttributeKey를 저장 후 build + */ + public static OAuthAttributes of( + SocialType socialType, + Map attributes + ) { + switch (socialType) { + case KAKAO -> { + ofKakao(attributes); + } + case NAVER -> { + ofNaver(attributes); + } + default -> throw new IllegalStateException("Unexpected value: " + socialType); + } + } + + private static OAuthAttributes ofKakao(Map attributes) { + return null; + } + + public static OAuthAttributes ofNaver(Map attributes) { + return null; + } + + public abstract String email(); + + public abstract String nickname(); +} diff --git a/src/main/java/org/capstone/maru/dto/security/SharedPostPrincipal.java b/src/main/java/org/capstone/maru/security/SharedPostPrincipal.java similarity index 100% rename from src/main/java/org/capstone/maru/dto/security/SharedPostPrincipal.java rename to src/main/java/org/capstone/maru/security/SharedPostPrincipal.java diff --git a/src/main/java/org/capstone/maru/dto/SocialType.java b/src/main/java/org/capstone/maru/security/constant/SocialType.java similarity index 100% rename from src/main/java/org/capstone/maru/dto/SocialType.java rename to src/main/java/org/capstone/maru/security/constant/SocialType.java diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 5bc8c80370..028252c999 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -47,7 +47,7 @@ spring: client-id: OvLyXvoT1NlEr8Wfiu2u client-secret: U6Or_rMFOQ authorization-grant-type: authorization_code - redirect-uri: http://localhost:8080/login/auth + redirect-uri: "{baseUrl}/login/oauth2/code/naver" provider: kakao: authorization-uri: https://kauth.kakao.com/oauth/authorize