|
| 1 | +package com.irum.apigateway.filter; |
| 2 | + |
| 3 | +import com.irum.apigateway.properties.JwtProperties; |
| 4 | +import io.jsonwebtoken.Claims; |
| 5 | +import io.jsonwebtoken.JwtException; |
| 6 | +import io.jsonwebtoken.Jwts; |
| 7 | +import io.jsonwebtoken.security.Keys; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.Collections; |
| 11 | +import javax.crypto.SecretKey; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import lombok.extern.slf4j.Slf4j; |
| 14 | +import org.springframework.http.HttpHeaders; |
| 15 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 16 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 17 | +import org.springframework.security.core.GrantedAuthority; |
| 18 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 19 | +import org.springframework.security.core.context.ReactiveSecurityContextHolder; |
| 20 | +import org.springframework.security.core.context.SecurityContextImpl; |
| 21 | +import org.springframework.stereotype.Component; |
| 22 | +import org.springframework.web.server.ServerWebExchange; |
| 23 | +import org.springframework.web.server.WebFilter; |
| 24 | +import org.springframework.web.server.WebFilterChain; |
| 25 | +import reactor.core.publisher.Mono; |
| 26 | + |
| 27 | +@Component |
| 28 | +@RequiredArgsConstructor |
| 29 | +@Slf4j |
| 30 | +public class JwtAuthenticationFilter implements WebFilter { |
| 31 | + |
| 32 | + private final JwtProperties jwtProperties; |
| 33 | + |
| 34 | + @Override |
| 35 | + public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
| 36 | + String authHeader = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION); |
| 37 | + if (authHeader == null || !authHeader.startsWith("Bearer ")) { |
| 38 | + return chain.filter(exchange); |
| 39 | + } |
| 40 | + |
| 41 | + String token = authHeader.substring(7); |
| 42 | + try { |
| 43 | + SecretKey key = |
| 44 | + Keys.hmacShaKeyFor( |
| 45 | + jwtProperties.accessTokenSecret().getBytes(StandardCharsets.UTF_8)); |
| 46 | + Claims claims = |
| 47 | + Jwts.parser().verifyWith(key).build().parseSignedClaims(token).getPayload(); |
| 48 | + String memberId = claims.getSubject(); |
| 49 | + String role = claims.get("TOKEN_ROLE_NAME", String.class); |
| 50 | + GrantedAuthority authority = new SimpleGrantedAuthority(role); |
| 51 | + Collection<GrantedAuthority> authorities = Collections.singletonList(authority); |
| 52 | + |
| 53 | + UsernamePasswordAuthenticationToken auth = |
| 54 | + new UsernamePasswordAuthenticationToken(memberId, null, authorities); |
| 55 | + ServerHttpRequest mutatedRequest = |
| 56 | + exchange.getRequest().mutate().header("X-Member-Id", memberId).build(); |
| 57 | + |
| 58 | + ServerWebExchange mutatedExchange = exchange.mutate().request(mutatedRequest).build(); |
| 59 | + |
| 60 | + return chain.filter(mutatedExchange) |
| 61 | + .contextWrite( |
| 62 | + ReactiveSecurityContextHolder.withSecurityContext( |
| 63 | + Mono.just(new SecurityContextImpl(auth)))); |
| 64 | + |
| 65 | + } catch (JwtException e) { |
| 66 | + log.warn("JWT Validation failed: {}", e.getMessage()); |
| 67 | + return chain.filter(exchange); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments