diff --git a/src/main/java/com/example/cargive/domain/member/repository/MemberRepository.java b/src/main/java/com/example/cargive/domain/member/repository/MemberRepository.java index d299fe2..1246b94 100644 --- a/src/main/java/com/example/cargive/domain/member/repository/MemberRepository.java +++ b/src/main/java/com/example/cargive/domain/member/repository/MemberRepository.java @@ -4,7 +4,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface MemberRepository extends JpaRepository { boolean existsMemberByLoginId(String loginId); + Optional findMemberByLoginId(String loginId); } diff --git a/src/main/java/com/example/cargive/global/config/SecurityConfig.java b/src/main/java/com/example/cargive/global/config/SecurityConfig.java index 01279ed..66fc820 100644 --- a/src/main/java/com/example/cargive/global/config/SecurityConfig.java +++ b/src/main/java/com/example/cargive/global/config/SecurityConfig.java @@ -1,6 +1,7 @@ package com.example.cargive.global.config; import com.example.cargive.domain.auth.service.CustomOAuth2UserService; +import com.example.cargive.global.config.oauth2.OAuth2SuccessHandler; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,6 +14,7 @@ @RequiredArgsConstructor public class SecurityConfig { private final CustomOAuth2UserService customOAuth2UserService; + private final OAuth2SuccessHandler oAuth2SuccessHandler; @Bean public SecurityFilterChain configure(HttpSecurity http) throws Exception { @@ -23,6 +25,7 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception { .oauth2Login() .userInfoEndpoint() .userService(customOAuth2UserService) - .and().and().build(); + .and().successHandler(oAuth2SuccessHandler) + .and().build(); } } diff --git a/src/main/java/com/example/cargive/global/config/oauth2/OAuth2SuccessHandler.java b/src/main/java/com/example/cargive/global/config/oauth2/OAuth2SuccessHandler.java new file mode 100644 index 0000000..fe0c725 --- /dev/null +++ b/src/main/java/com/example/cargive/global/config/oauth2/OAuth2SuccessHandler.java @@ -0,0 +1,49 @@ +package com.example.cargive.global.config.oauth2; + +import com.example.cargive.domain.member.repository.MemberRepository; +import com.example.cargive.global.base.BaseException; +import com.example.cargive.global.base.BaseResponseStatus; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.*; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.core.user.OAuth2User; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; +import org.springframework.stereotype.Component; +import org.springframework.web.util.UriComponentsBuilder; + +import java.io.IOException; + +@Component +@RequiredArgsConstructor +public class OAuth2SuccessHandler extends SimpleUrlAuthenticationSuccessHandler { + + @Value("${oauth.redirect-uri}") + private String redirectUrl; + + private final MemberRepository memberRepository; + + @Override + public void onAuthenticationSuccess(HttpServletRequest request, + HttpServletResponse response, + Authentication authentication) throws IOException { + String redirectUrl = getRedirectUrl(authentication); + getRedirectStrategy().sendRedirect(request, response, redirectUrl); + } + + private Long getMemberId(Authentication authentication) { + OAuth2User principal = (OAuth2User) authentication.getPrincipal(); + + String loginId = principal.getAttribute("name"); + + return memberRepository.findMemberByLoginId(loginId) + .orElseThrow(() -> new BaseException(BaseResponseStatus.MEMBER_NOT_FOUND_ERROR)).getId(); + } + + private String getRedirectUrl(Authentication authentication) { + return UriComponentsBuilder.fromUriString(redirectUrl) + .queryParam("memberId", getMemberId(authentication)) + .build().toUriString(); + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 2a45844..ddfb409 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -49,4 +49,6 @@ spring: authorization-uri: https://kauth.kakao.com/oauth/authorize token-uri: https://kauth.kakao.com/oauth/token user-info-uri: https://kapi.kakao.com/v2/user/me - user-name-attribute: id \ No newline at end of file + user-name-attribute: id +oauth: + redirect-uri : "http://localhost:8080" \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 9b7c5e1..e63c4b2 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -51,4 +51,6 @@ spring: authorization-uri: https://kauth.kakao.com/oauth/authorize token-uri: https://kauth.kakao.com/oauth/token user-info-uri: https://kapi.kakao.com/v2/user/me - user-name-attribute: id \ No newline at end of file + user-name-attribute: id +oauth: + redirect-uri : ${redirect-uri} \ No newline at end of file