22
33import com .somemore .auth .authentication .JwtAuthenticationToken ;
44import com .somemore .auth .jwt .domain .EncodedToken ;
5+ import com .somemore .auth .jwt .domain .TokenType ;
56import com .somemore .auth .jwt .exception .JwtErrorType ;
67import com .somemore .auth .jwt .exception .JwtException ;
78import com .somemore .auth .jwt .usecase .JwtUseCase ;
89import io .jsonwebtoken .Claims ;
910import jakarta .servlet .FilterChain ;
1011import jakarta .servlet .ServletException ;
12+ import jakarta .servlet .http .Cookie ;
1113import jakarta .servlet .http .HttpServletRequest ;
1214import jakarta .servlet .http .HttpServletResponse ;
1315import lombok .RequiredArgsConstructor ;
1921import org .springframework .web .filter .OncePerRequestFilter ;
2022
2123import java .io .IOException ;
24+ import java .util .Arrays ;
2225import java .util .List ;
26+ import java .util .Objects ;
2327
2428@ RequiredArgsConstructor
2529@ Slf4j
@@ -30,11 +34,11 @@ public class JwtAuthFilter extends OncePerRequestFilter {
3034
3135 @ Override
3236 protected boolean shouldNotFilter (HttpServletRequest request ) {
33- String token = request . getHeader ( "Authorization" );
37+ EncodedToken accessToken = getAccessToken ( request );
3438 String path = request .getRequestURI ();
3539
36- return token == null
37- || token . isEmpty ()
40+ return accessToken == null
41+ || accessToken . isUninitialized ()
3842 || path .equals ("/api/center/sign-in" );
3943 }
4044
@@ -54,17 +58,33 @@ protected void doFilterInternal(HttpServletRequest request,
5458 }
5559
5660 private EncodedToken getAccessToken (HttpServletRequest request ) {
57- String accessToken = request .getHeader ("Authorization" );
58- if (accessToken == null || accessToken .isEmpty ()) {
61+ EncodedToken accessToken = findAccessTokenFromCookie (request );
62+
63+ if (accessToken .isUninitialized ()) {
64+ accessToken = new EncodedToken (request .getHeader ("Authorization" ));
65+ }
66+
67+ if (accessToken .isUninitialized ()) {
5968 throw new JwtException (JwtErrorType .MISSING_TOKEN );
6069 }
6170
62- String tokenPrefix = "Bearer " ;
63- if (accessToken .startsWith (tokenPrefix )) {
64- return new EncodedToken (accessToken .substring (tokenPrefix .length ()));
71+ String prefix = "Bearer " ;
72+ return accessToken .removePrefix (prefix );
73+ }
74+
75+ private EncodedToken findAccessTokenFromCookie (HttpServletRequest request ) {
76+ Cookie [] cookies = request .getCookies ();
77+ if (cookies == null ) {
78+ return new EncodedToken ("UNINITIALIZED" );
6579 }
6680
67- return new EncodedToken (accessToken );
81+ return Arrays .stream (cookies )
82+ .filter (Objects ::nonNull )
83+ .filter (cookie -> cookie .getName ().equals (TokenType .ACCESS .name ()))
84+ .map (Cookie ::getValue )
85+ .findFirst ()
86+ .map (EncodedToken ::new )
87+ .orElse (new EncodedToken ("UNINITIALIZED" ));
6888 }
6989
7090 private JwtAuthenticationToken createAuthenticationToken (Claims claims ,
0 commit comments