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

Refactor: 쿠키 방식에서 헤더 방식으로 jwt 전달 #78

Merged
merged 1 commit into from
Jan 20, 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 @@ -55,29 +55,9 @@ public ResponseEntity<CommonResponse<UserInfoResponseDTO>> login(

UserInfoResponseDTO userInfo = userService.login(requestDTO);

Cookie tokenCookie = jwtUtil.createTokenCookie(requestDTO.getUsername());
Cookie userInfoCookie = jwtUtil.createUserInfoCookie(userInfo);

ResponseCookie cookie1 = ResponseCookie.from(userInfoCookie.getName(),userInfoCookie.getValue())
.sameSite("none")
.secure(true)
.path("/")
.domain(".hhive.store")
.maxAge(3600)
.build();

ResponseCookie cookie2 = ResponseCookie.from(tokenCookie.getName(), tokenCookie.getValue())
.sameSite("none")
.httpOnly(true)
.secure(true)
.path("/")
.maxAge(3600)
.build();

response.setHeader(HttpHeaders.SET_COOKIE, cookie1.toString());
response.addHeader(HttpHeaders.SET_COOKIE, cookie2.toString());
// response.addCookie(tokenCookie);
// response.addCookie(userInfoCookie);
response.setHeader(JwtUtil.AUTHORIZATION_HEADER, jwtUtil.createToken(requestDTO.getUsername()));
// Cookie tokenCookie = jwtUtil.createTokenCookie(requestDTO.getUsername());
// Cookie userInfoCookie = jwtUtil.createUserInfoCookie(userInfo);

return ResponseEntity.ok()
.body(CommonResponse.of(HttpStatus.OK.value(), "로그인 성공", userInfo));
Expand Down
99 changes: 52 additions & 47 deletions src/main/java/com/HHive/hhive/global/jwt/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class JwtUtil {
public static final String AUTHORIZATION_HEADER = "Authorization";

// Token 식별자
public static final String BEARER_PREFIX = "Bearer%";
public static final String BEARER_PREFIX = "Bearer ";

// 쿠키 이름
public static final String USER_INFO_COOKIE_NAME = "userinfo";
Expand All @@ -59,18 +59,23 @@ public void init() {

public String resolveToken(HttpServletRequest request) {

Cookie[] cookies = request.getCookies();

if (cookies == null) {
return null;
}

String bearerToken = Arrays.stream(cookies)
.filter(cookie -> cookie.getName().equals(JWT_COOKIE_NAME))
.findFirst()
.map(Cookie::getValue)
.orElse(null);

// Cookie[] cookies = request.getCookies();
//
// if (cookies == null) {
// return null;
// }
//
// String bearerToken = Arrays.stream(cookies)
// .filter(cookie -> cookie.getName().equals(JWT_COOKIE_NAME))
// .findFirst()
// .map(Cookie::getValue)
// .orElse(null);
//
// if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(BEARER_PREFIX)) {
// return bearerToken.substring(7);
// }

String bearerToken = request.getHeader(AUTHORIZATION_HEADER);
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(BEARER_PREFIX)) {
return bearerToken.substring(7);
}
Expand All @@ -97,40 +102,40 @@ public Claims getUserInfoFromToken(String token) {
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
}

public Cookie createTokenCookie(String username) {

long EXPIRED_TIME = 60 * 60;

String jwtToken = createToken(username);

Cookie cookie = new Cookie(JWT_COOKIE_NAME, jwtToken);
cookie.setPath("/api");
cookie.setMaxAge((int) EXPIRED_TIME);
cookie.setHttpOnly(true);

return cookie;
}

public Cookie createUserInfoCookie(UserInfoResponseDTO responseDTO) {

long EXPIRED_TIME = 60 * 60;

String responseToString = null;

try {
responseToString = objectMapper.writeValueAsString(responseDTO);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

String encodedUserInfo = Base64.getEncoder().encodeToString(responseToString.getBytes());

Cookie cookie = new Cookie(USER_INFO_COOKIE_NAME, encodedUserInfo);
cookie.setPath("/");
cookie.setMaxAge((int) EXPIRED_TIME);

return cookie;
}
// public Cookie createTokenCookie(String username) {
//
// long EXPIRED_TIME = 60 * 60;
//
// String jwtToken = createToken(username);
//
// Cookie cookie = new Cookie(JWT_COOKIE_NAME, jwtToken);
// cookie.setPath("/api");
// cookie.setMaxAge((int) EXPIRED_TIME);
// cookie.setHttpOnly(true);
//
// return cookie;
// }
//
// public Cookie createUserInfoCookie(UserInfoResponseDTO responseDTO) {
//
// long EXPIRED_TIME = 60 * 60;
//
// String responseToString = null;
//
// try {
// responseToString = objectMapper.writeValueAsString(responseDTO);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// }
//
// String encodedUserInfo = Base64.getEncoder().encodeToString(responseToString.getBytes());
//
// Cookie cookie = new Cookie(USER_INFO_COOKIE_NAME, encodedUserInfo);
// cookie.setPath("/");
// cookie.setMaxAge((int) EXPIRED_TIME);
//
// return cookie;
// }

public String createToken(String username) {
Date date = new Date();
Expand Down
Loading