Skip to content

Commit

Permalink
hotfix : cookie 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
oosedus committed Oct 20, 2024
1 parent 5f5f992 commit adf9b5c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -26,4 +24,29 @@ public ResponseEntity<ApiResponse<TokenResponse.AccessTokenResponse>> reissueAcc
TokenResponse.AccessTokenResponse accessTokenResponse = tokenService.reissueAccessToken(request, response);
return ApiResponse.success(TokenSuccessStatus.REISSUE_ACCESS_TOKEN_SUCCESS, accessTokenResponse);
}

@GetMapping("/cookie/test")
public ResponseEntity<ApiResponse<String>> test(
HttpServletResponse response,
@RequestHeader("registerToken") String registerToken
) {
tokenService.test(response, registerToken);
return ApiResponse.success(TokenSuccessStatus.SUCCESS_TEST);
}

@PostMapping("/cookie/test")
public ResponseEntity<ApiResponse<String>> testPost(
HttpServletResponse response,
@RequestBody String registerToken
) {
tokenService.test(response, registerToken);
return ApiResponse.success(TokenSuccessStatus.SUCCESS_TEST);
}

@GetMapping("/cookie")
public ResponseEntity<ApiResponse<String>> testGetCookie(
@CookieValue(value = "tmpRefreshToken", required = false) String tmpRefreshToken
) {
return ApiResponse.success(TokenSuccessStatus.SUCCESS_TEST, tmpRefreshToken);
}
}
21 changes: 21 additions & 0 deletions src/main/java/corecord/dev/domain/token/service/TokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,41 @@
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 {
private final RefreshTokenRepository refreshTokenRepository;
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 추출 및 유효성 검증
Expand Down

0 comments on commit adf9b5c

Please sign in to comment.