Skip to content
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

[Feat] OAuth 로그인 성공시의 로직을 설정한다 #69

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member, Long> {
boolean existsMemberByLoginId(String loginId);
Optional<Member> findMemberByLoginId(String loginId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +14,7 @@
@RequiredArgsConstructor
public class SecurityConfig {
private final CustomOAuth2UserService customOAuth2UserService;
private final OAuth2SuccessHandler oAuth2SuccessHandler;

@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
Expand All @@ -23,6 +25,7 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {
.oauth2Login()
.userInfoEndpoint()
.userService(customOAuth2UserService)
.and().and().build();
.and().successHandler(oAuth2SuccessHandler)
.and().build();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
user-name-attribute: id
oauth:
redirect-uri : "http://localhost:8080"
4 changes: 3 additions & 1 deletion src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
user-name-attribute: id
oauth:
redirect-uri : ${redirect-uri}
Loading