diff --git a/src/main/java/corecord/dev/common/config/SecurityConfig.java b/src/main/java/corecord/dev/common/config/SecurityConfig.java index 7bb1be1..21127bb 100644 --- a/src/main/java/corecord/dev/common/config/SecurityConfig.java +++ b/src/main/java/corecord/dev/common/config/SecurityConfig.java @@ -36,7 +36,7 @@ public class SecurityConfig { private final OAuthLoginFailureHandler oAuthLoginFailureHandler; private final String[] swaggerUrls = {"/swagger-ui/**", "/v3/**"}; - private final String[] authUrls = {"/", "/api/users/register", "/oauth2/authorization/kakao", "/api/token/access-token", "/actuator/health"}; + private final String[] authUrls = {"/", "/api/users/register", "/oauth2/authorization/kakao", "/api/token/access-token", "/actuator/health", "/api/token/**"}; private final String[] allowedUrls = Stream.concat(Arrays.stream(swaggerUrls), Arrays.stream(authUrls)) .toArray(String[]::new); diff --git a/src/main/java/corecord/dev/domain/token/constant/TokenSuccessStatus.java b/src/main/java/corecord/dev/domain/token/constant/TokenSuccessStatus.java index 8325b25..0d4ad57 100644 --- a/src/main/java/corecord/dev/domain/token/constant/TokenSuccessStatus.java +++ b/src/main/java/corecord/dev/domain/token/constant/TokenSuccessStatus.java @@ -8,6 +8,7 @@ @Getter @AllArgsConstructor public enum TokenSuccessStatus implements BaseSuccessStatus { + SUCCESS_TEST(HttpStatus.OK, "SUCCESS_TEST", "쿠키 발급 테스트 성공입니다."), REISSUE_ACCESS_TOKEN_SUCCESS(HttpStatus.CREATED, "S001", "Access Token 재발급 성공입니다."); private final HttpStatus httpStatus; diff --git a/src/main/java/corecord/dev/domain/token/controller/TokenController.java b/src/main/java/corecord/dev/domain/token/controller/TokenController.java index c75a64b..f427040 100644 --- a/src/main/java/corecord/dev/domain/token/controller/TokenController.java +++ b/src/main/java/corecord/dev/domain/token/controller/TokenController.java @@ -8,9 +8,7 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/token") @@ -26,4 +24,29 @@ public ResponseEntity> reissueAcc TokenResponse.AccessTokenResponse accessTokenResponse = tokenService.reissueAccessToken(request, response); return ApiResponse.success(TokenSuccessStatus.REISSUE_ACCESS_TOKEN_SUCCESS, accessTokenResponse); } + + @GetMapping("/cookie/test") + public ResponseEntity> test( + HttpServletResponse response, + @RequestHeader("registerToken") String registerToken + ) { + tokenService.test(response, registerToken); + return ApiResponse.success(TokenSuccessStatus.SUCCESS_TEST); + } + + @PostMapping("/cookie/test") + public ResponseEntity> testPost( + HttpServletResponse response, + @RequestBody String registerToken + ) { + tokenService.test(response, registerToken); + return ApiResponse.success(TokenSuccessStatus.SUCCESS_TEST); + } + + @GetMapping("/cookie") + public ResponseEntity> testGetCookie( + @CookieValue(value = "tmpRefreshToken", required = false) String tmpRefreshToken + ) { + return ApiResponse.success(TokenSuccessStatus.SUCCESS_TEST, tmpRefreshToken); + } } diff --git a/src/main/java/corecord/dev/domain/token/service/TokenService.java b/src/main/java/corecord/dev/domain/token/service/TokenService.java index 83dfbb3..4788f7d 100644 --- a/src/main/java/corecord/dev/domain/token/service/TokenService.java +++ b/src/main/java/corecord/dev/domain/token/service/TokenService.java @@ -8,13 +8,16 @@ import corecord.dev.domain.token.exception.enums.TokenErrorStatus; import corecord.dev.domain.token.exception.model.TokenException; import corecord.dev.domain.token.repository.RefreshTokenRepository; +import corecord.dev.domain.user.exception.model.UserException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseCookie; import org.springframework.stereotype.Service; +@Slf4j @Service @RequiredArgsConstructor public class TokenService { @@ -22,6 +25,24 @@ public class TokenService { private final JwtUtil jwtUtil; private final CookieUtil cookieUtil; + public void test(HttpServletResponse response, String registerToken) { + // registerToken 유효성 검증 + if(!jwtUtil.isRegisterTokenValid(registerToken)) { + throw new TokenException(TokenErrorStatus.INVALID_REGISTER_TOKEN); + } + + // registerToken에서 providerId 추출 + String providerId = jwtUtil.getProviderIdFromToken(registerToken); + log.info("providerId: {}", providerId); + + // 배포환경 쿠키 발급 테스트 + String tmpRefreshToken = "000tmpRefreshToken000"; + ResponseCookie tmpRefreshTokenCookie = cookieUtil.createTokenCookie("tmpRefreshToken", tmpRefreshToken); + + // 쿠키 생성 + response.addHeader("Set-Cookie", tmpRefreshTokenCookie.toString()); + } + @Transactional public TokenResponse.AccessTokenResponse reissueAccessToken(HttpServletRequest request, HttpServletResponse response) { // RefreshToken 추출 및 유효성 검증