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: Cookie Key ์ด๋ฆ„ ์ผ์น˜ํ™” #13

Merged
merged 2 commits into from
Sep 10, 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 @@ -2,7 +2,6 @@

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.security.core.Authentication;
Expand All @@ -11,7 +10,6 @@

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;
Expand All @@ -32,9 +30,6 @@ public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final int accessTokenValidTime = 10 * 60; // ์œ ํšจ๊ธฐ๊ฐ„ : 10๋ถ„
private final int refreshTokenValidTime = 10 * 24 * 60 * 60; // ์œ ํšจ๊ธฐ๊ฐ„ : 10์ผ

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

private ResponseCookie createCookie(String key, String value, int maxAge) {
return ResponseCookie.from(key, value)
.maxAge(maxAge)
Expand All @@ -60,15 +55,20 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
ObjectMapper objectMapper = new ObjectMapper();
String jsonResponse = objectMapper.writeValueAsString(loginSucessResponse);

response.addHeader(HttpHeaders.SET_COOKIE, createCookie("username", username, refreshTokenValidTime).toString());
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("accessToken", accessToken, accessTokenValidTime).toString());
response.addHeader(HttpHeaders.SET_COOKIE, createCookie("refreshToken", refreshToken, refreshTokenValidTime).toString());
response.addHeader(HttpHeaders.SET_COOKIE,
createCookie("username", username, refreshTokenValidTime).toString());
response.addHeader(HttpHeaders.SET_COOKIE,
createCookie("accessToken", accessToken, accessTokenValidTime).toString());
response.addHeader(HttpHeaders.SET_COOKIE,
createCookie("refreshToken", refreshToken, refreshTokenValidTime).toString());

// ์‘๋‹ต ์„ค์ •
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

// JSON ์‘๋‹ต์„ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์— ์ž‘์„ฑ
response.getWriter().write(jsonResponse);

response.sendRedirect("https://kaboo.site/join");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
String username = null, cookieAccessToken = null, cookieRefreshToken = null;
for (Cookie cookie : cookies) {
switch (cookie.getName()) {
case "Username" -> username = cookie.getValue();
case "Authorization" -> cookieAccessToken = cookie.getValue();
case "RefreshToken" -> cookieRefreshToken = cookie.getValue();
case "username" -> username = cookie.getValue();
case "accessToken" -> cookieAccessToken = cookie.getValue();
case "refreshToken" -> cookieRefreshToken = cookie.getValue();
}
}

Expand Down Expand Up @@ -100,7 +100,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
jwtTokenProvider.createAccessToken(username));

jwtAccessTokenRepository.save(newAccessToken);
response.addCookie(createCookie("Authorization", newAccessToken.getAccessToken()));
response.addCookie(createCookie("accessToken", newAccessToken.getAccessToken()));
}
}
}
Expand Down
Loading