Skip to content

Commit d33d3a7

Browse files
committed
🐛 fix: 토큰 없는 경우 400으로 에러 처리
1 parent 05de7b6 commit d33d3a7

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/main/java/org/withtime/be/withtimebe/domain/auth/service/command/AuthCommandServiceImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,18 @@ private Long getUserId(String token) {
104104
}
105105

106106
private String getAccessToken(HttpServletRequest request) {
107-
return CookieUtil.getCookie(request, AuthenticationConstants.ACCESS_TOKEN_NAME);
107+
return this.getInCookie(request, AuthenticationConstants.ACCESS_TOKEN_NAME);
108108
}
109109

110110
private String getRefreshToken(HttpServletRequest request) {
111-
return CookieUtil.getCookie(request, AuthenticationConstants.REFRESH_TOKEN_NAME);
111+
return this.getInCookie(request, AuthenticationConstants.REFRESH_TOKEN_NAME);
112+
}
113+
114+
private String getInCookie(HttpServletRequest request, String name) {
115+
String cookieValue = CookieUtil.getCookie(request, name);
116+
if (cookieValue == null) {
117+
throw new TokenException(TokenErrorCode.NOT_EXISTS_TOKEN);
118+
}
119+
return cookieValue;
112120
}
113121
}

src/main/java/org/withtime/be/withtimebe/global/error/code/TokenErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
public enum TokenErrorCode implements BaseErrorCode {
1010

1111
TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, "TOKEN401_1", "토큰의 기한이 만료되었습니다."),
12-
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "TOKEN401_2", "리프레시 토큰이 유효하지 않습니다.")
12+
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "TOKEN401_2", "리프레시 토큰이 유효하지 않습니다."),
13+
NOT_EXISTS_TOKEN(HttpStatus.BAD_REQUEST, "TOKEN400_1", "토큰이 존재하지 않습니다."),
1314
;
1415

1516
private final HttpStatus httpStatus;

src/main/java/org/withtime/be/withtimebe/global/security/handler/CustomLogoutHandler.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.withtime.be.withtimebe.domain.auth.service.command.TokenStorageCommandService;
88
import org.withtime.be.withtimebe.domain.auth.service.query.TokenQueryService;
99
import org.withtime.be.withtimebe.domain.auth.service.query.TokenStorageQueryService;
10+
import org.withtime.be.withtimebe.global.error.code.TokenErrorCode;
11+
import org.withtime.be.withtimebe.global.error.exception.TokenException;
1012
import org.withtime.be.withtimebe.global.security.constants.AuthenticationConstants;
1113
import org.withtime.be.withtimebe.global.util.CookieUtil;
1214

@@ -39,9 +41,18 @@ private Long getUserId(String token) {
3941
}
4042

4143
private String getAccessToken(HttpServletRequest request) {
42-
return CookieUtil.getCookie(request, AuthenticationConstants.ACCESS_TOKEN_NAME);
44+
return this.getInCookie(request, AuthenticationConstants.ACCESS_TOKEN_NAME);
4345
}
4446

4547
private String getRefreshToken(HttpServletRequest request) {
46-
return CookieUtil.getCookie(request, AuthenticationConstants.REFRESH_TOKEN_NAME);
47-
}}
48+
return this.getInCookie(request, AuthenticationConstants.REFRESH_TOKEN_NAME);
49+
}
50+
51+
private String getInCookie(HttpServletRequest request, String name) {
52+
String cookieValue = CookieUtil.getCookie(request, name);
53+
if (cookieValue == null) {
54+
throw new TokenException(TokenErrorCode.NOT_EXISTS_TOKEN);
55+
}
56+
return cookieValue;
57+
}
58+
}

0 commit comments

Comments
 (0)