-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathJwtTokenExtractor.java
More file actions
25 lines (21 loc) · 980 Bytes
/
JwtTokenExtractor.java
File metadata and controls
25 lines (21 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package finalmission.auth.infrastructure;
import finalmission.auth.domain.AuthTokenExtractor;
import finalmission.exception.auth.AuthTokenNotFoundException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;
import org.springframework.stereotype.Component;
@Component
public class JwtTokenExtractor implements AuthTokenExtractor<String> {
public String extract(final HttpServletRequest request) {
final Cookie[] cookies = request.getCookies();
if (cookies == null) {
throw new AuthTokenNotFoundException("쿠키가 존재하지 않습니다.");
}
return Arrays.stream(cookies)
.filter(cookie -> AUTH_TOKEN_NAME.equals(cookie.getName()))
.findFirst()
.map(Cookie::getValue)
.orElseThrow(() -> new AuthTokenNotFoundException("쿠키에 " + AUTH_TOKEN_NAME + "필드가 존재하지 않습니다."));
}
}