From 1161b9c48ad9ef9190ca6252f9b4fbc9d7d058c7 Mon Sep 17 00:00:00 2001 From: Jinseong Date: Sat, 4 Jan 2025 17:22:04 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20OAuth2=20=EC=9D=B8=EA=B0=80=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 패키지 이름 auth -> oauth2 수정 - 카카오 인증 서버 프로필 정보 요청 성공 - DB 반영 resolve : #27 --- .../kongju/domain/member/entity/Member.java | 6 ++++- .../member/repository/MemberRepository.java | 2 ++ .../domain/member/service/MemberService.java | 26 ++++++++++++++++++- .../kongju/global/config/SecurityConfig.java | 9 ++++--- .../{auth => oauth2}/AuthController.java | 2 +- .../CustomOAuth2UserService.java | 20 ++++++-------- .../CustomRequestEntityConverter.java | 2 +- .../global/{auth => oauth2}/UserInfo.java | 2 +- .../kakao/dto/KakaoProfileInfoResponse.java | 2 +- .../kakao/dto/KakaoToken.java | 2 +- 10 files changed, 51 insertions(+), 22 deletions(-) rename src/main/java/univ/goormthon/kongju/global/{auth => oauth2}/AuthController.java (93%) rename src/main/java/univ/goormthon/kongju/global/{auth => oauth2}/CustomOAuth2UserService.java (71%) rename src/main/java/univ/goormthon/kongju/global/{auth => oauth2}/CustomRequestEntityConverter.java (97%) rename src/main/java/univ/goormthon/kongju/global/{auth => oauth2}/UserInfo.java (71%) rename src/main/java/univ/goormthon/kongju/global/{auth => oauth2}/kakao/dto/KakaoProfileInfoResponse.java (92%) rename src/main/java/univ/goormthon/kongju/global/{auth => oauth2}/kakao/dto/KakaoToken.java (88%) diff --git a/src/main/java/univ/goormthon/kongju/domain/member/entity/Member.java b/src/main/java/univ/goormthon/kongju/domain/member/entity/Member.java index 7901e05..1e479e6 100644 --- a/src/main/java/univ/goormthon/kongju/domain/member/entity/Member.java +++ b/src/main/java/univ/goormthon/kongju/domain/member/entity/Member.java @@ -29,12 +29,16 @@ public class Member extends BaseTimeEntity { @Setter private String profileImage; + @Column(name = "registration_id") + private String registrationId; + @Builder - public Member(Long kakaoId, String nickname, String email, String profileImage) { + public Member(Long kakaoId, String nickname, String email, String profileImage, String registrationId) { this.kakaoId = kakaoId; this.nickname = nickname; this.email = email; this.profileImage = profileImage; + this.registrationId = registrationId; } } \ No newline at end of file diff --git a/src/main/java/univ/goormthon/kongju/domain/member/repository/MemberRepository.java b/src/main/java/univ/goormthon/kongju/domain/member/repository/MemberRepository.java index c922830..1fc9127 100644 --- a/src/main/java/univ/goormthon/kongju/domain/member/repository/MemberRepository.java +++ b/src/main/java/univ/goormthon/kongju/domain/member/repository/MemberRepository.java @@ -9,4 +9,6 @@ @Repository public interface MemberRepository extends JpaRepository { Optional findByEmail(String email); + + Optional findByRegistrationIdAndEmail(String registrationId, String email); } diff --git a/src/main/java/univ/goormthon/kongju/domain/member/service/MemberService.java b/src/main/java/univ/goormthon/kongju/domain/member/service/MemberService.java index ba56b11..4ec0c46 100644 --- a/src/main/java/univ/goormthon/kongju/domain/member/service/MemberService.java +++ b/src/main/java/univ/goormthon/kongju/domain/member/service/MemberService.java @@ -6,10 +6,12 @@ import univ.goormthon.kongju.domain.member.dto.response.ProfileInfo; import univ.goormthon.kongju.domain.member.entity.Member; import univ.goormthon.kongju.domain.member.repository.MemberRepository; -import univ.goormthon.kongju.global.auth.kakao.dto.KakaoProfileInfoResponse; +import univ.goormthon.kongju.global.oauth2.kakao.dto.KakaoProfileInfoResponse; import univ.goormthon.kongju.global.exception.NotFoundException; import univ.goormthon.kongju.global.exception.dto.ErrorCode; +import java.util.Map; + @Service @RequiredArgsConstructor public class MemberService { @@ -27,6 +29,12 @@ public Member findOrRegisterMember(String email, KakaoProfileInfoResponse respon .orElseGet(() -> registerMember(response)); } + @Transactional + public Member findOrRegisterMember(String registrationId, Map attributes) { + return memberRepository.findByRegistrationIdAndEmail(registrationId, (String) attributes.get("email")) + .orElseGet(() -> registerMember(registrationId, attributes)); + } + private Member registerMember(KakaoProfileInfoResponse response) { Member member = Member.builder() .kakaoId(response.id()) @@ -37,6 +45,22 @@ private Member registerMember(KakaoProfileInfoResponse response) { return memberRepository.save(member); } + private Member registerMember(String registrationId, Map attributes) { + Map kakaoAccount = (Map) attributes.get("kakao_account"); + Map properties = (Map) attributes.get("properties"); + + Member member = Member.builder() + .kakaoId(Long.valueOf(attributes.get("id").toString())) + .email((String) kakaoAccount.get("email")) + .nickname((String) properties.get("nickname")) + .profileImage((String) properties.get("profile_image")) + .registrationId(registrationId) + .build(); + + return memberRepository.save(member); + } + + private Member updateProfileIfChanged(Member member, KakaoProfileInfoResponse profileInfo) { boolean isChanged = false; String nickname = profileInfo.kakaoAccount().profile().nickname(); diff --git a/src/main/java/univ/goormthon/kongju/global/config/SecurityConfig.java b/src/main/java/univ/goormthon/kongju/global/config/SecurityConfig.java index c55ea39..1dbd2a1 100644 --- a/src/main/java/univ/goormthon/kongju/global/config/SecurityConfig.java +++ b/src/main/java/univ/goormthon/kongju/global/config/SecurityConfig.java @@ -5,12 +5,13 @@ import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; import org.springframework.security.oauth2.client.endpoint.DefaultAuthorizationCodeTokenResponseClient; import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest; import org.springframework.security.web.SecurityFilterChain; -import univ.goormthon.kongju.global.auth.CustomOAuth2UserService; -import univ.goormthon.kongju.global.auth.CustomRequestEntityConverter; +import univ.goormthon.kongju.global.oauth2.CustomOAuth2UserService; +import univ.goormthon.kongju.global.oauth2.CustomRequestEntityConverter; @Configuration @RequiredArgsConstructor @@ -23,9 +24,11 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti http .csrf(AbstractHttpConfigurer::disable) // 브라우저 환경이 아니므로 CSRF 보호 기능 비활성화 .authorizeHttpRequests(requests -> requests - .requestMatchers("/api/kongju/oauth2/**", "/api/kongju/auth/**").permitAll() + .requestMatchers("/h2-console/**","/swagger-ui.html","/swagger-ui/**", "/api/kongju/oauth2/**", "/api/kongju/auth/**").permitAll() .anyRequest().authenticated() ) + .headers(headers -> headers + .frameOptions(HeadersConfigurer.FrameOptionsConfig::disable)) .oauth2Login(oauth2 -> oauth2 .userInfoEndpoint(userInfo -> userInfo .userService(customOAuth2UserService)) diff --git a/src/main/java/univ/goormthon/kongju/global/auth/AuthController.java b/src/main/java/univ/goormthon/kongju/global/oauth2/AuthController.java similarity index 93% rename from src/main/java/univ/goormthon/kongju/global/auth/AuthController.java rename to src/main/java/univ/goormthon/kongju/global/oauth2/AuthController.java index d9d00e7..9729579 100644 --- a/src/main/java/univ/goormthon/kongju/global/auth/AuthController.java +++ b/src/main/java/univ/goormthon/kongju/global/oauth2/AuthController.java @@ -1,4 +1,4 @@ -package univ.goormthon.kongju.global.auth; +package univ.goormthon.kongju.global.oauth2; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; diff --git a/src/main/java/univ/goormthon/kongju/global/auth/CustomOAuth2UserService.java b/src/main/java/univ/goormthon/kongju/global/oauth2/CustomOAuth2UserService.java similarity index 71% rename from src/main/java/univ/goormthon/kongju/global/auth/CustomOAuth2UserService.java rename to src/main/java/univ/goormthon/kongju/global/oauth2/CustomOAuth2UserService.java index 0706a81..78c58de 100644 --- a/src/main/java/univ/goormthon/kongju/global/auth/CustomOAuth2UserService.java +++ b/src/main/java/univ/goormthon/kongju/global/oauth2/CustomOAuth2UserService.java @@ -1,4 +1,4 @@ -package univ.goormthon.kongju.global.auth; +package univ.goormthon.kongju.global.oauth2; import lombok.RequiredArgsConstructor; import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; @@ -8,7 +8,7 @@ import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import univ.goormthon.kongju.domain.member.entity.Member; +import univ.goormthon.kongju.domain.member.service.MemberService; import java.util.Map; @@ -16,27 +16,23 @@ @RequiredArgsConstructor public class CustomOAuth2UserService extends DefaultOAuth2UserService { - // private final MemberRepository memberRepository; + private final MemberService memberService; @Override @Transactional public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { OAuth2User oAuth2User = super.loadUser(userRequest); - // 사용자 정보 추출 String registrationId = userRequest.getClientRegistration().getRegistrationId(); - String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails() - .getUserInfoEndpoint().getUserNameAttributeName(); + String userNameAttributeName = userRequest.getClientRegistration() + .getProviderDetails() + .getUserInfoEndpoint() + .getUserNameAttributeName(); Map attributes = oAuth2User.getAttributes(); - System.out.println(attributes); + memberService.findOrRegisterMember(registrationId, attributes); return new DefaultOAuth2User(oAuth2User.getAuthorities(), attributes, userNameAttributeName); } - - // 가입하지 않은 사용자는 Member 객체를 새로 생성하여 DB에 저장 - private Member getMember() { - return null; - } } diff --git a/src/main/java/univ/goormthon/kongju/global/auth/CustomRequestEntityConverter.java b/src/main/java/univ/goormthon/kongju/global/oauth2/CustomRequestEntityConverter.java similarity index 97% rename from src/main/java/univ/goormthon/kongju/global/auth/CustomRequestEntityConverter.java rename to src/main/java/univ/goormthon/kongju/global/oauth2/CustomRequestEntityConverter.java index f4268ef..de18d48 100644 --- a/src/main/java/univ/goormthon/kongju/global/auth/CustomRequestEntityConverter.java +++ b/src/main/java/univ/goormthon/kongju/global/oauth2/CustomRequestEntityConverter.java @@ -1,4 +1,4 @@ -package univ.goormthon.kongju.global.auth; +package univ.goormthon.kongju.global.oauth2; import org.springframework.core.convert.converter.Converter; import org.springframework.http.HttpHeaders; diff --git a/src/main/java/univ/goormthon/kongju/global/auth/UserInfo.java b/src/main/java/univ/goormthon/kongju/global/oauth2/UserInfo.java similarity index 71% rename from src/main/java/univ/goormthon/kongju/global/auth/UserInfo.java rename to src/main/java/univ/goormthon/kongju/global/oauth2/UserInfo.java index 39b3e83..f2eb9c3 100644 --- a/src/main/java/univ/goormthon/kongju/global/auth/UserInfo.java +++ b/src/main/java/univ/goormthon/kongju/global/oauth2/UserInfo.java @@ -1,4 +1,4 @@ -package univ.goormthon.kongju.global.auth; +package univ.goormthon.kongju.global.oauth2; import java.util.Map; diff --git a/src/main/java/univ/goormthon/kongju/global/auth/kakao/dto/KakaoProfileInfoResponse.java b/src/main/java/univ/goormthon/kongju/global/oauth2/kakao/dto/KakaoProfileInfoResponse.java similarity index 92% rename from src/main/java/univ/goormthon/kongju/global/auth/kakao/dto/KakaoProfileInfoResponse.java rename to src/main/java/univ/goormthon/kongju/global/oauth2/kakao/dto/KakaoProfileInfoResponse.java index 2658db6..73e741b 100644 --- a/src/main/java/univ/goormthon/kongju/global/auth/kakao/dto/KakaoProfileInfoResponse.java +++ b/src/main/java/univ/goormthon/kongju/global/oauth2/kakao/dto/KakaoProfileInfoResponse.java @@ -1,4 +1,4 @@ -package univ.goormthon.kongju.global.auth.kakao.dto; +package univ.goormthon.kongju.global.oauth2.kakao.dto; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/src/main/java/univ/goormthon/kongju/global/auth/kakao/dto/KakaoToken.java b/src/main/java/univ/goormthon/kongju/global/oauth2/kakao/dto/KakaoToken.java similarity index 88% rename from src/main/java/univ/goormthon/kongju/global/auth/kakao/dto/KakaoToken.java rename to src/main/java/univ/goormthon/kongju/global/oauth2/kakao/dto/KakaoToken.java index 743ca58..57fe2a0 100644 --- a/src/main/java/univ/goormthon/kongju/global/auth/kakao/dto/KakaoToken.java +++ b/src/main/java/univ/goormthon/kongju/global/oauth2/kakao/dto/KakaoToken.java @@ -1,4 +1,4 @@ -package univ.goormthon.kongju.global.auth.kakao.dto; +package univ.goormthon.kongju.global.oauth2.kakao.dto; import com.fasterxml.jackson.annotation.JsonProperty;