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

fix: 카카오 로그인 관련 로직 업데이트 #179

Merged
merged 1 commit into from
Aug 15, 2024
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 @@ -9,7 +9,6 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

Expand All @@ -22,20 +21,17 @@ public class AuthController {

private final AuthService authService;

@GetMapping("/test")
public String test() {
return "test";
}

@Operation(summary = "로그인 API", description = "카카오 로그인 페이지로 리다이렉트되어 카카오 로그인을 수행할 수 있도록 안내")
@PostMapping("/sign-in")
public ApplicationResponse<LoginRes> signIn(@RequestParam(name = "code") String code) {
return ApplicationResponse.ok(authService.signIn(code));
public ApplicationResponse<LoginRes> signIn(@RequestParam(name = "code") String code,
@RequestParam(name = "redirect-uri") String redirectUri) {
return ApplicationResponse.ok(authService.signIn(code, redirectUri));
}

@Operation(summary = "로그아웃 API", description = "로그아웃된 JWT 블랙리스트 등록")
@PostMapping("/sign-out")
public ApplicationResponse<Void> signOut(HttpServletRequest request, @AuthenticationPrincipal PrincipalDetails principalDetails) {
public ApplicationResponse<Void> signOut(HttpServletRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) {
String token = request.getHeader("Authorization");
authService.signOut(token, principalDetails.getMember());
return ApplicationResponse.ok();
Expand All @@ -50,10 +46,9 @@ public ApplicationResponse<Void> withdrawal(@AuthenticationPrincipal PrincipalDe

@Operation(summary = "토큰재발급 API", description = "RefreshToken 정보로 요청 시, ")
@GetMapping("/reissue")
public ApplicationResponse<TokenDto> reissue(HttpServletRequest request, @AuthenticationPrincipal PrincipalDetails principalDetails) {
public ApplicationResponse<TokenDto> reissue(HttpServletRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) {
String token = request.getHeader("Authorization");
return ApplicationResponse.ok(authService.reissue(token, principalDetails.getMember()));
}
}

// https://yeees.tistory.com/231
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class AuthService {
private final MemberRepository memberRepository;

@Transactional
public LoginRes signIn(String code) {
public LoginRes signIn(String code, String redirectUri) {
// Business Logic
// 카카오로 액세스 토큰 요청하기
KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(code);
KakaoToken kakaoAccessToken = kakaoClient.getKakaoAccessToken(code, redirectUri);

// 카카오톡에 있는 사용자 정보 반환
KakaoProfile kakaoProfile = kakaoClient.getMemberInfo(kakaoAccessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public class KakaoClient {
@Value("${spring.security.oauth2.client.registration.kakao.authorization-grant-type}")
private String kakaoGrantType;

@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}")
private String kakaoRedirectUri;

@Value("${spring.security.oauth2.client.provider.kakao.token-uri}")
private String kakaoTokenUri;

Expand All @@ -40,15 +37,15 @@ public class KakaoClient {
* @param code - 카카오에서 발급해준 인가 코드
* @return - 카카오에서 반환한 응답 토큰 객체
*/
public KakaoToken getKakaoAccessToken(String code) {
public KakaoToken getKakaoAccessToken(String code, String redirectUri) {
// 요청 보낼 객체 기본 생성
WebClient webClient = WebClient.create(kakaoTokenUri);

//요청 본문
MultiValueMap<String , String> params = new LinkedMultiValueMap<>();
params.add("grant_type", kakaoGrantType);
params.add("client_id", kakaoClientId);
params.add("redirect_uri", kakaoRedirectUri);
params.add("redirect_uri", redirectUri);
params.add("code", code);
params.add("client_secret", kakaoClientSecret);

Expand Down
Loading