|
1 | 1 | package flipnote.apigateway.filter; |
2 | 2 |
|
3 | | -import flipnote.apigateway.util.JwtUtil; |
4 | | -import io.jsonwebtoken.Claims; |
5 | | -import io.jsonwebtoken.JwtException; |
| 3 | +import flipnote.apigateway.client.TokenValidationClient; |
6 | 4 | import lombok.extern.slf4j.Slf4j; |
7 | 5 | import org.springframework.cloud.gateway.filter.GatewayFilter; |
8 | 6 | import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; |
9 | | -import org.springframework.http.HttpHeaders; |
| 7 | +import org.springframework.http.HttpCookie; |
10 | 8 | import org.springframework.http.HttpStatus; |
11 | 9 | import org.springframework.stereotype.Component; |
12 | 10 | import org.springframework.web.server.ServerWebExchange; |
|
16 | 14 | @Component |
17 | 15 | public class AuthenticationFilter extends AbstractGatewayFilterFactory<AuthenticationFilter.Config> { |
18 | 16 |
|
19 | | - private final JwtUtil jwtUtil; |
| 17 | + private final TokenValidationClient tokenValidationClient; |
20 | 18 |
|
21 | | - public AuthenticationFilter(JwtUtil jwtUtil) { |
| 19 | + public AuthenticationFilter(TokenValidationClient tokenValidationClient) { |
22 | 20 | super(Config.class); |
23 | | - this.jwtUtil = jwtUtil; |
| 21 | + this.tokenValidationClient = tokenValidationClient; |
24 | 22 | } |
25 | 23 |
|
| 24 | + private static final String ACCESS_TOKEN_COOKIE = "accessToken"; |
| 25 | + |
26 | 26 | @Override |
27 | 27 | public GatewayFilter apply(Config config) { |
28 | 28 | return (exchange, chain) -> { |
29 | | - String authHeader = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION); |
| 29 | + HttpCookie cookie = exchange.getRequest().getCookies().getFirst(ACCESS_TOKEN_COOKIE); |
30 | 30 |
|
31 | | - if (authHeader == null || !authHeader.startsWith("Bearer ")) { |
32 | | - log.warn("Missing or invalid Authorization header"); |
| 31 | + if (cookie == null) { |
| 32 | + log.warn("Missing access token cookie"); |
33 | 33 | return onError(exchange, HttpStatus.UNAUTHORIZED); |
34 | 34 | } |
35 | 35 |
|
36 | | - String token = authHeader.substring(7); |
37 | | - |
38 | | - try { |
39 | | - Claims claims = jwtUtil.parseToken(token); |
40 | | - |
41 | | - Long userId = jwtUtil.getUserId(claims); |
42 | | - String email = jwtUtil.getEmail(claims); |
43 | | - String role = jwtUtil.getRole(claims); |
| 36 | + String token = cookie.getValue(); |
44 | 37 |
|
45 | | - log.debug("Authenticated user: id={}, email={}, role={}", userId, email, role); |
| 38 | + return tokenValidationClient.validateToken(token) |
| 39 | + .flatMap(response -> { |
| 40 | + log.debug("Authenticated user: id={}, email={}, role={}", |
| 41 | + response.userId(), response.email(), response.role()); |
46 | 42 |
|
47 | | - ServerWebExchange modifiedExchange = exchange.mutate() |
48 | | - .request(r -> r |
49 | | - .header("X-User-Id", String.valueOf(userId)) |
50 | | - .header("X-User-Email", email) |
51 | | - .header("X-User-Role", role)) |
52 | | - .build(); |
| 43 | + ServerWebExchange modifiedExchange = exchange.mutate() |
| 44 | + .request(r -> r |
| 45 | + .header("X-User-Id", String.valueOf(response.userId())) |
| 46 | + .header("X-User-Email", response.email()) |
| 47 | + .header("X-User-Role", response.role())) |
| 48 | + .build(); |
53 | 49 |
|
54 | | - return chain.filter(modifiedExchange); |
55 | | - |
56 | | - } catch (JwtException e) { |
57 | | - log.error("JWT validation failed: {}", e.getMessage()); |
58 | | - return onError(exchange, HttpStatus.UNAUTHORIZED); |
59 | | - } |
| 50 | + return chain.filter(modifiedExchange); |
| 51 | + }) |
| 52 | + .onErrorResume(e -> { |
| 53 | + log.error("Token validation failed: {}", e.getMessage()); |
| 54 | + return onError(exchange, HttpStatus.UNAUTHORIZED); |
| 55 | + }); |
60 | 56 | }; |
61 | 57 | } |
62 | 58 |
|
|
0 commit comments