Skip to content

Commit

Permalink
Merge pull request #171 from JNU-econovation/refactor/BE-58
Browse files Browse the repository at this point in the history
[BE-58] refactor : 토큰 쿠키에 담기
  • Loading branch information
LJH098 authored Feb 26, 2024
2 parents 83f04d8 + 93f5b7c commit 810ff99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.econovation.recruit.api.interviewer.docs.InterviewerExceptionDocs;
import com.econovation.recruit.api.user.usecase.UserLoginUseCase;
import com.econovation.recruit.api.user.usecase.UserRegisterUseCase;
import com.econovation.recruit.utils.SecurityUtils;
import com.econovation.recruitcommon.annotation.ApiErrorExceptionsExample;
import com.econovation.recruitcommon.annotation.DevelopOnlyApi;
import com.econovation.recruitcommon.annotation.PasswordValidate;
Expand All @@ -16,7 +17,6 @@
import com.econovation.recruitdomain.domains.interviewer.domain.Role;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -59,10 +59,17 @@ public ResponseEntity<TokenResponse> issueToken() {
public ResponseEntity<TokenResponse> login(
@RequestBody LoginRequestDto loginRequestDto, HttpServletResponse response) {
TokenResponse tokenResponse = userLoginUseCase.execute(loginRequestDto);
Cookie accessCookie = setCookie("ACCESS_TOKEN", tokenResponse.getAccessToken());
response.addCookie(accessCookie);
Cookie refreshCookie = setCookie("REFRESH_TOKEN", tokenResponse.getRefreshToken());
response.addCookie(refreshCookie);
response.addHeader(
"Set-Cookie",
new StringBuilder(
SecurityUtils.setCookie(
"ACCESS_TOKEN", tokenResponse.getAccessToken())
.toString())
.append("; ")
.append(
SecurityUtils.setCookie(
"REFRESH_TOKEN", tokenResponse.getRefreshToken()))
.toString());
return new ResponseEntity<>(tokenResponse, HttpStatus.OK);
}

Expand All @@ -88,13 +95,4 @@ public ResponseEntity<String> changePassword(
userRegisterUseCase.changePassword(password);
return new ResponseEntity<>(PASSWORD_SUCCESS_CHANGE_MESSAGE, HttpStatus.OK);
}

private Cookie setCookie(String name, String value) {
Cookie cookie = new Cookie(name, value);
cookie.setHttpOnly(true);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 24 * 30);
cookie.setSecure(true);
return cookie;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.econovation.recruit.utils;

import org.springframework.http.ResponseCookie;

public class SecurityUtils {
public static ResponseCookie setCookie(String name, String value) {
return ResponseCookie.from(name, value)
.secure(true)
.sameSite("None")
.httpOnly(true)
.maxAge(2592000)
.path("/")
.build();
}
}

0 comments on commit 810ff99

Please sign in to comment.