Skip to content

Commit

Permalink
#8 feat: 로그아웃 api redis 이용한 토큰 관리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sojungpp committed Mar 4, 2023
1 parent 5b8289d commit b187229
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public enum BaseResponseStatus {
* 2000: Request 오류
*/
// users(2000~2099)
NULL_TOKEN(false, 2000, "토큰 값을 입력해주세요."),
NULL_TOKEN(false, 2000, "토큰 값을 입력해주세요."),
NULL_EMAIL(false, 2001, "이메일을 입력해주세요."),
NULL_PROVIDER(false, 2002, "소셜 이름을 입력해주세요."),
INVALID_PROVIDER(false, 2003, "잘못된 소셜 이름입니다."),
ALREADY_WITHDRAW_USER(false, 2003, "이미 삭제된 회원입니다."),
ALREADY_WITHDRAW_USER(false, 2004, "이미 삭제된 회원입니다."),
INVALID_TOKEN(false, 2005, "유효하지 않은 토큰 값입니다."),

// stores(2100~2199)
NULL_ADDRESS(false, 2100, "주소를 입력해주세요."),
Expand Down Expand Up @@ -68,8 +69,7 @@ public enum BaseResponseStatus {
INVALID_EMAIL(false, 3002, "존재하지 않는 이메일입니다."),
NO_STORE_ROLE(false, 3003, "판매자가 아닙니다."),
INVALID_USER_STATUS(false, 3004, "비활성화된 사용자입니다."),
INVALID_TOKEN(false, 3005, "잘못된 토큰 값입니다."),
EXPIRED_TOKEN(false, 3006, "만료된 토큰 값입니다."),
EXPIRED_TOKEN(false, 3005, "만료된 토큰 값입니다."),

// stores(3100~3199)
INVALID_STORE_IDX(false, 3100, "존재하지 않는 스토어입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public Long getUserIdx() throws BaseException{
}

// 토큰 추출
private static String getToken() {
private String getToken() throws BaseException {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
String token = request.getHeader(REQUEST_HEADER_NAME);
if(redisTemplate.opsForValue().get(token)!=null) throw new BaseException(INVALID_TOKEN);
return token;
}

Expand Down Expand Up @@ -115,4 +116,28 @@ public String validateRefreshToken(Long userIdx, String refreshTokenReq) throws
if(!refreshToken.equals(refreshTokenReq)) throw new BaseException(INVALID_TOKEN);
return refreshToken;
}

// refreshToken 삭제
public void deleteToken(Long userIdx) {
String key = String.valueOf(userIdx);
if(redisTemplate.opsForValue().get(key)!=null) redisTemplate.delete(key);
}

public Long getExpiration(String token) {
token = token.replaceAll(TOKEN_REGEX, TOKEN_REPLACEMENT);
Date expiration = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody().getExpiration();
long now = (new Date()).getTime();
return (expiration.getTime() - now);
}

public void logout(Long userIdx) throws BaseException {
String token = getToken();
deleteToken(userIdx);
Long expiration = getExpiration(token);
registerBlackList(token, expiration);
}

private void registerBlackList(String token, Long expiration) {
redisTemplate.opsForValue().set(token, "logout", Duration.ofMillis(expiration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PostUserRes signupCustomer(Long userIdx, PostCustomerReq postCustomerReq)
String accessToken = authService.createAccessToken(userIdx);
String refreshToken = authService.createRefreshToken(userIdx);

user.signup(postCustomerReq.getNickname(),Role.CUSTOMER, postCustomerReq.getProfileImg());
user.signup(postCustomerReq.getNickname(), Role.CUSTOMER, postCustomerReq.getProfileImg());
userRepository.save(user);

return new PostUserRes(accessToken, refreshToken, user.getRole());
Expand Down Expand Up @@ -120,8 +120,8 @@ public void signout(Long userIdx) throws BaseException {
public void logout(Long userIdx) throws BaseException {
try{
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
authService.logout(userIdx);
user.logout();
// TODO redis 사용해 토큰 관리
} catch (BaseException e) {
throw e;
} catch (Exception e) {
Expand Down

0 comments on commit b187229

Please sign in to comment.