Skip to content

Commit

Permalink
[Chore] 코드 정리 및 @value활용 토큰만료시간 구현(#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 committed May 30, 2024
1 parent 712ec85 commit 7602719
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

@Component
public class JwtTokenGenerator {

@Value("${jwt.secret}")
private String secretKey;
// @Value("${jwt.access-token-expire-time}")
// private long ACCESS_TOKEN_EXPIRE_TIME;
// @Value("${jwt.refresh-token-expire-time}")
// private long REFRESH_TOKEN_EXPIRE_TIME;

private final long accessExpiration = 1 * 60 * 1000L; //1분으로 테스트
private final long refreshExpiration = 60 * 60 * 1000L; //60분으로 테스트
@Value("${jwt.access-token-expire-time}") //1분
private long ACCESS_TOKEN_EXPIRE_TIME;

@Value("${jwt.refresh-token-expire-time}") //1시간
private long REFRESH_TOKEN_EXPIRE_TIME;

public String generateToken(final Long userId, boolean isAccessToken) {
final Date presentDate = new Date();
Expand Down Expand Up @@ -47,9 +47,9 @@ private Date generateExpireDataByToken(final boolean isAccessToken, Date present
//토근에 따라 만료시간 다름
private long setExpireTimeByToken(final boolean isAccessToken) {
if (isAccessToken) {
return accessExpiration;
return ACCESS_TOKEN_EXPIRE_TIME;
} else {
return refreshExpiration;
return REFRESH_TOKEN_EXPIRE_TIME;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<BaseResponse<?>> postMember(@RequestBody MemberCreateDTO m
@GetMapping
public ResponseEntity<BaseResponse<?>> findMemberById(@MemberId final Long memberId) {

final MemberFindDTO memberFindDTO = memberService.findMemberById(memberId);
final MemberFindDTO memberFindDTO = MemberFindDTO.of(memberService.findMemberById(memberId));

return ApiResponseUtil.success(SuccessMessage.MEMBER_FIND_SUCCESS, memberFindDTO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,19 @@ public TokenAndUserIdResponse reissue(final String refreshToken, final ReissueRe

Long memberId = reissueRequest.memberId();
validateRefreshToken(refreshToken,memberId);
Member member = findMemberBy(memberId);
Member member = findMemberById(memberId);
Token issueedToken = jwtTokenProvider.issueTokens(memberId);
updateRefreshToken(issueedToken.refreshToken(), memberId);
return TokenAndUserIdResponse.of(issueedToken, memberId);


}


private void validateRefreshToken(final String refreshToken, final Long userId) {
private void validateRefreshToken(final String refreshToken, final Long memberId) {
try {
jwtTokenValidator.validateRefreshToken(refreshToken);
String storedRefreshToken = getRefreshToken(userId);
String storedRefreshToken = getRefreshToken(memberId);
jwtTokenValidator.equalsRefreshToken(refreshToken, storedRefreshToken);
} catch (UnauthorizedException e) {
signOut(userId);
signOut(memberId);
throw e;
}
}
Expand All @@ -87,33 +84,24 @@ private String getRefreshTokenFromRedis(Long userId) {
return storedRefreshToken.getRefreshToken();
}

private void updateRefreshToken(String refreshToken, Long memberId) {
@Transactional
public void updateRefreshToken(String refreshToken, Long memberId) {
refreshTokenRepository.save(RefreshToken.of(memberId, refreshToken));
}

public void signOut(final Long memberId) {
Member findMember = findMemberBy(memberId);
Member findMember = findMemberById(memberId);
deleteRefreshToken(findMember);
}

public void findById(final Long memberId) {
findMember(memberId).orElseThrow(
() -> new NotFoundException(ErrorMessage.MEMBER_NOT_FOUND));
}

private void deleteRefreshToken(final Member member) {
@Transactional
public void deleteRefreshToken(final Member member) {
refreshTokenRepository.deleteById(member.getId());
}

public MemberFindDTO findMemberById(final Long memberId) {
return MemberFindDTO.of(findMember(memberId).orElseThrow(
() -> new NotFoundException(ErrorMessage.MEMBER_NOT_FOUND)));
}

public Member findMemberBy(final Long memberId) {
public Member findMemberById(final Long memberId) {
return memberRepository.findById(memberId).orElseThrow(
() -> new NotFoundException(ErrorMessage.MEMBER_NOT_FOUND));

}

@Transactional
Expand All @@ -123,10 +111,6 @@ public void deleteMemberById(final Long memberId) {
memberRepository.delete(member);
}

public Optional<Member> findMember(final Long memberId) {
return memberRepository.findById(memberId);
}

public List<MemberDataDTO> getAllMemberList() {

// .stream : 메서드가 반환한 컬렉션을 스트림으로 변환함, 스트림을 사용하면 데이터를 순차적으로 처리함
Expand All @@ -135,6 +119,4 @@ public List<MemberDataDTO> getAllMemberList() {
//.toList : 스트림을 리스트로 변환
return memberRepository.findAll().stream().map(member -> MemberDataDTO.of(member)).toList();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PostService {

@Transactional
public String postContent(final Long memberId, final Long blogId, final BlogContentRequestDTO blogContentRequestDTO) {
memberService.findById(memberId);
memberService.findMemberById(memberId);

Blog blog = fineBlogById(blogId);

Expand Down

0 comments on commit 7602719

Please sign in to comment.