-
Notifications
You must be signed in to change notification settings - Fork 10
[9주차/여니] 워크북 제출합니다 #58
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
992208e
refactor: security 패키지 구조 역할별로 분리
zeoueon ff2d16e
chore: JWT 의존성 추가 및 yml 설정
zeoueon f526025
feat: JWT 기반 로그인 API 구현
zeoueon c6ea5a0
feat: 마이페이지 API에 @AuthenticationPrincipal 적용
zeoueon 6186e3e
refactor: ObjectMapper 싱글톤 빈으로 관리
zeoueon cd53eb3
refactor: 응답 코드 enum 정리 및 AuthSuccessCode 구현
zeoueon ad73409
chore: Security 설정 OAuth2 적용 및 yml 설정
zeoueon adb2050
feat: 카카오 소셜 로그인 구현
zeoueon 031a3be
fix: 회원가입 시 모든 약관에 대한 입력을 검증하지 못하는 오류 수정
zeoueon 4bd0416
refactor: 선호 음식 조회 쿼리 최적화
zeoueon 0448597
fix: 약관 빈 값 검증 추가
zeoueon 7ecda5e
refactor: Member 조회 및 생성을 도메인 패키지로 분리
zeoueon 31839b3
feat: provider 파싱 메서드 분리 및 필수값 검증 추가
zeoueon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
20 changes: 19 additions & 1 deletion
20
src/main/java/umc/domain/auth/exception/code/AuthSuccessCode.java
This file contains hidden or 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,4 +1,22 @@ | ||
| package umc.domain.auth.exception.code; | ||
|
|
||
| public enum AuthSuccessCode { | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import org.springframework.http.HttpStatus; | ||
| import umc.global.apiPayload.code.BaseSuccessCode; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum AuthSuccessCode implements BaseSuccessCode { | ||
|
|
||
| // 200 | ||
| LOGIN_SUCCESS(HttpStatus.OK, "AUTH200_1", "로그인이 성공적으로 완료되었습니다."), | ||
|
|
||
| // 201 | ||
| SIGN_UP(HttpStatus.CREATED, "AUTH201_1", "회원가입이 완료되었습니다."), | ||
| ; | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/umc/domain/auth/oauth/OAuthAttributeMissingException.java
This file contains hidden or 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,16 @@ | ||
| package umc.domain.auth.oauth; | ||
|
|
||
| import lombok.Getter; | ||
| import umc.domain.auth.exception.AuthException; | ||
| import umc.domain.auth.exception.code.AuthErrorCode; | ||
|
|
||
| @Getter | ||
| public class OAuthAttributeMissingException extends AuthException { | ||
|
|
||
| private final String attribute; | ||
|
|
||
| public OAuthAttributeMissingException(String attribute) { | ||
| super(AuthErrorCode.OAUTH_MISSING_ATTRIBUTES); | ||
| this.attribute = attribute; | ||
| } | ||
| } |
This file contains hidden or 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,54 @@ | ||
| package umc.domain.auth.oauth; | ||
|
|
||
| import umc.domain.auth.exception.AuthException; | ||
| import umc.domain.auth.exception.code.AuthErrorCode; | ||
| import umc.domain.auth.oauth.dto.KakaoDTO; | ||
| import umc.domain.auth.oauth.dto.OAuthDTO; | ||
| import umc.domain.member.enums.SocialType; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class OAuthAttributes { | ||
|
|
||
| public static OAuthDTO of(String registrationId, Map<String, Object> rawAttributes) { | ||
| SocialType provider; | ||
| try { | ||
| provider = SocialType.valueOf(registrationId.toUpperCase()); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new AuthException(AuthErrorCode.NOT_SUPPORT_SOCIAL_PROVIDER); | ||
| } | ||
| return switch (provider) { | ||
| case KAKAO -> ofKakao(rawAttributes); | ||
| default -> throw new AuthException(AuthErrorCode.NOT_SUPPORT_SOCIAL_PROVIDER); | ||
| }; | ||
| } | ||
|
|
||
| private static OAuthDTO ofKakao(Map<String, Object> rawAttributes) { | ||
| String id = requireString(rawAttributes, "id"); | ||
|
|
||
| Map<String, Object> kakaoAccount = requireMap(rawAttributes, "kakao_account"); | ||
| String email = requireString(kakaoAccount, "email"); | ||
|
|
||
| Map<String, Object> profile = requireMap(kakaoAccount, "profile"); | ||
| String nickname = requireString(profile, "nickname"); | ||
|
|
||
| return new KakaoDTO(id, email, nickname); | ||
| } | ||
|
|
||
| private static String requireString(Map<String, Object> map, String key) { | ||
| Object value = map.get(key); | ||
| if (value == null || value.toString().isBlank()) { | ||
| throw new OAuthAttributeMissingException(key); | ||
| } | ||
| return value.toString(); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static Map<String, Object> requireMap(Map<String, Object> map, String key) { | ||
| Object value = map.get(key); | ||
| if (!(value instanceof Map<?, ?>)) { | ||
| throw new OAuthAttributeMissingException(key); | ||
| } | ||
| return (Map<String, Object>) value; | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/umc/domain/auth/oauth/OAuthMemberService.java
This file contains hidden or 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,22 @@ | ||
| package umc.domain.auth.oauth; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import umc.domain.auth.oauth.dto.OAuthDTO; | ||
| import umc.domain.member.converter.MemberConverter; | ||
| import umc.domain.member.entity.Member; | ||
| import umc.domain.member.repository.MemberRepository; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class OAuthMemberService { | ||
|
|
||
| private final MemberRepository memberRepository; | ||
|
|
||
| @Transactional | ||
| public Member loadOrCreate(OAuthDTO oAuthDto) { | ||
| return memberRepository.findBySocialTypeAndSocialUid(oAuthDto.getSocialType(), oAuthDto.getSocialUid()) | ||
| .orElseGet(() -> memberRepository.save(MemberConverter.toMember(oAuthDto))); | ||
| } | ||
| } |
This file contains hidden or 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,32 @@ | ||
| package umc.domain.auth.oauth.dto; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import umc.domain.member.enums.SocialType; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class KakaoDTO implements OAuthDTO { | ||
|
|
||
| private final String id; | ||
| private final String email; | ||
| private final String name; | ||
|
|
||
| @Override | ||
| public SocialType getSocialType() { | ||
| return SocialType.KAKAO; | ||
| } | ||
|
|
||
| @Override | ||
| public String getSocialUid() { | ||
| return id; | ||
| } | ||
|
|
||
| @Override | ||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| package umc.domain.auth.oauth.dto; | ||
|
|
||
| import umc.domain.member.enums.SocialType; | ||
|
|
||
| public interface OAuthDTO { | ||
| SocialType getSocialType(); | ||
| String getSocialUid(); | ||
| String getEmail(); | ||
| String getName(); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일반 로그인에서
AuthenticationManager.authenticate()를 사용하셨군요!저는 직접
findByEmail후passwordEncoder.matches()로 검증하는 흐름을 먼저 떠올렸는데, 이 방식은 Spring Security 인증 흐름을 그대로 따라UserDetailsService,PasswordEncoder, provider 구조와 연결된다는 장점이 있는 것 같아요,저도 이 방식으로 리팩토링해보면 좋을 것 같습니다!☺️