Skip to content

Commit

Permalink
Merge pull request #78 from H-Hive/feature/connect_frontend
Browse files Browse the repository at this point in the history
Refactor: 쿠키 방식에서 헤더 방식으로 jwt 전달
  • Loading branch information
wkdehdgk159 authored Jan 20, 2024
2 parents 723ad81 + f44d968 commit 8580bbc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 70 deletions.
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

0 comments on commit 8580bbc

Please sign in to comment.