Skip to content

Commit

Permalink
[#2]✨Feat: 리프레시 토큰 삭제 및 추가 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sumin220 committed Nov 19, 2024
1 parent 9983e86 commit d8e3d4c
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/univ/yesummit/global/auth/util/JwtUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.transaction.Transactional;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -12,6 +16,8 @@
import univ.yesummit.domain.member.repository.MemberRepository;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Getter
Expand Down Expand Up @@ -91,6 +97,58 @@ public Optional<Long> extractMemberId(String token) {

return Optional.ofNullable(memberId);
}

// public Map<String, Object> getClaims(String token) {
// try {
// DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC512(secret)) // 동일한 키와 알고리즘 사용
// .build()
// .verify(token); // 토큰 검증
//
// // 클레임 반환
// Map<String, Object> claims = new HashMap<>();
// claims.put("subject", decodedJWT.getSubject());
// claims.put("member_id", decodedJWT.getClaim(ID_CLAIM).asLong());
// claims.put("expiresAt", decodedJWT.getExpiresAt());
//
// return claims;
// } catch (Exception e) {
// log.error("Invalid token: {}", e.getMessage());
// throw new IllegalArgumentException("Invalid token", e);
// }
// }

//== 추후에 유지보수하는 과정에서 리프레시 토큰의 만료 혹은 토큰의 블랙리스트를 구현할 때 사용할 것 ==//

//
// public void destroyRefreshToken(String username) {
// memberRepository.findByUsername(username)
// .ifPresentOrElse(
// Member::destroyRefreshToken,
// () -> { throw new MemberException(ErrorCode.NOT_FOUND_MEMBER); }
// );
// }
//
// public void updateRefreshToken(String username, String refreshToken) {
// memberRepository.findByUsername(username)
// .ifPresentOrElse(
// member -> member.updateRefreshToken(refreshToken),
// () -> { throw new MemberException(ErrorCode.NOT_FOUND_MEMBER); }
// );
// }

public Optional<String> extractRefreshToken(HttpServletRequest request) {
return Optional.ofNullable(request.getHeader(refreshHeader))
.filter(refreshToken -> refreshToken.startsWith(BEARER))
.map(refreshToken -> refreshToken.replace(BEARER, ""));
}

public void sendAccessToken(HttpServletResponse response, String accessToken) {
response.setStatus(HttpServletResponse.SC_OK);

response.setHeader(accessHeader, accessToken);
log.info("Send AccessToken: {}", accessToken);
}

/* 토큰 유효성 검증 */
public boolean isValid(String token) {
try {
Expand Down

0 comments on commit d8e3d4c

Please sign in to comment.