Skip to content

Commit

Permalink
Merge pull request #35 from AmorGakCo/hotfix/#31
Browse files Browse the repository at this point in the history
hotfix/#34
  • Loading branch information
songhaechan authored Aug 23, 2024
2 parents 6987dfd + f7f3efd commit 8314c01
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.amorgakco.backend.global.oauth.handler;

import com.amorgakco.backend.jwt.controller.JwtCookieLoader;
import com.amorgakco.backend.jwt.dto.AccessTokenResponse;
import com.amorgakco.backend.jwt.dto.MemberJwt;
import com.amorgakco.backend.jwt.service.JwtProperties;
import com.amorgakco.backend.jwt.service.JwtService;
import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -16,15 +15,14 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;

@RequiredArgsConstructor
@Component
public class Oauth2SuccessHandler implements AuthenticationSuccessHandler {

private final JwtService jwtService;
private final JwtCookieLoader jwtCookieLoader;
private final ObjectMapper objectMapper;
private final JwtProperties jwtProperties;

@Override
public void onAuthenticationSuccess(
Expand All @@ -33,13 +31,7 @@ public void onAuthenticationSuccess(
final Authentication authentication)
throws IOException {
final MemberJwt memberJwt = jwtService.createAndSaveMemberToken(authentication.getName());
jwtCookieLoader.loadCookie(response, memberJwt.refreshToken());
final String accessToken =
objectMapper.writeValueAsString(new AccessTokenResponse(memberJwt.accessToken()));
final PrintWriter writer = response.getWriter();
writer.write(accessToken);
// 프론트랑 붙일 때 필요함
// response.sendRedirect(jwtProperties.redirectUri());
writer.flush();
jwtCookieLoader.loadCookies(response, memberJwt.refreshToken(), memberJwt.accessToken());
response.sendRedirect(jwtProperties.frontUrl());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@
@Component
public class JwtCookieLoader {

private static final int COOKIE_AGE_SECONDS = 604800;
private static final String REFRESH_TOKEN_COOKIE_NAME = "refresh-token";
private static final int REFRESH_COOKIE_AGE_SECONDS = 604800;
private static final int ACCESS_COOKIE_AGE_SECONDS = 180;
private static final String REFRESH_COOKIE_NAME = "refresh-token";
private static final String ACCESS_COOKIE_NAME = "access-token";

public void loadCookie(final HttpServletResponse response, final String refreshToken) {
final ResponseCookie refreshTokenCookie = makeCookie(refreshToken);
public void loadCookies(
final HttpServletResponse response,
final String refreshToken,
final String accessToken) {
final ResponseCookie refreshTokenCookie =
makeCookie(REFRESH_COOKIE_NAME, REFRESH_COOKIE_AGE_SECONDS, refreshToken);
final ResponseCookie accessTokenCookie =
makeCookie(ACCESS_COOKIE_NAME, ACCESS_COOKIE_AGE_SECONDS, accessToken);
response.setHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString());
response.setHeader(HttpHeaders.SET_COOKIE, accessTokenCookie.toString());
}

private ResponseCookie makeCookie(final String token) {
return ResponseCookie.from(REFRESH_TOKEN_COOKIE_NAME, token)
.maxAge(COOKIE_AGE_SECONDS)
private ResponseCookie makeCookie(
final String cookieName, final int cookieAge, final String token) {
return ResponseCookie.from(cookieName, token)
.maxAge(cookieAge)
.secure(false)
.httpOnly(true)
.path("/")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "jwt")
public record JwtProperties(Long accessExpiration, Long refreshExpiration, String redirectUri) {}
public record JwtProperties(Long accessExpiration, Long refreshExpiration, String frontUrl) {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@RequiredArgsConstructor
public class JwtService {
private final JwtProperties jwtProperties;
private final JwtValidator jwtValidator;
private final JwtCreator jwtCreator;
private final RefreshTokenRepository refreshTokenRepository;

Expand Down

0 comments on commit 8314c01

Please sign in to comment.