Skip to content

Commit

Permalink
Feat: 카카오 로그인 이후 쿠키 정보 주도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Taejin1221 committed Sep 10, 2024
1 parent 5951713 commit d595741
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kaboo.kaboo_auth.domain.dto.response;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class LoginSucessResponse implements Serializable {
private String username;
private String accessToken;
private String refreshToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import kaboo.kaboo_auth.domain.dto.response.LoginSucessResponse;
import kaboo.kaboo_auth.domain.jwt.JwtTokenProvider;
import kaboo.kaboo_auth.domain.jwt.entity.JwtAccessToken;
import kaboo.kaboo_auth.domain.jwt.entity.JwtRefreshToken;
Expand All @@ -28,7 +31,7 @@ public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final int refreshTokenValidTime = 10 * 24 * 60 * 60; // 유효기간 : 10일

@Value("${AUTH.REDIRECT_URL}")
String redirectURL;
private String redirectURL;

private Cookie createCookie(String key, String value, int maxAge) {
Cookie cookie = new Cookie(key, value);
Expand All @@ -50,9 +53,15 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
jwtAccessTokenRepository.save(new JwtAccessToken(username, accessToken));
jwtRefreshTokenRepository.save(new JwtRefreshToken(username, refreshToken));

response.addCookie(createCookie("Username", username, refreshTokenValidTime));
response.addCookie(createCookie("Authorization", accessToken, accessTokenValidTime));
response.addCookie(createCookie("RefreshToken", refreshToken, refreshTokenValidTime));
response.sendRedirect(redirectURL);
LoginSucessResponse loginSucessResponse = new LoginSucessResponse(username, accessToken, refreshToken);
ObjectMapper objectMapper = new ObjectMapper();
String jsonResponse = objectMapper.writeValueAsString(loginSucessResponse);

// 응답 설정
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

// JSON 응답을 출력 스트림에 작성
response.getWriter().write(jsonResponse);
}
}

0 comments on commit d595741

Please sign in to comment.