@Bean
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, AuthenticationFilter authFilter) {
return builder.routes()
.route("Gateway", r -> r
.path("/api/gateway/v1/user-info") // The specific path to route
.filters(f -> f.filter(authFilter.apply(new AuthenticationFilter.Config())))
.uri("http://localhost:8084"))
.build();
}
когда я пытаюсь подключиться к API: /api/gateway/v1/user-info, отображается любой из журналов или system.out.println, присутствующий в authentionFilter.
п>
Я не знаю почему, но мой метод применения внутри моего AuthenticationFileter не вызывается, хотя я нахожу маршруты мой AuthenticationFilter [code]@Component("Authenticate") @Slf4j public class AuthenticationFilter extends AbstractGatewayFilterFactory{
private static final Logger logger = LoggerFactory.getLogger(AuthenticationFilter.class);
@Autowired private RouteValidator routeValidator;
@Autowired private JwtUtil jwtUtil;
private final Auth_Manager_Service authManagerFeignClient;
public AuthenticationFilter(@Lazy Auth_Manager_Service authManagerFeignClient) { super(Config.class); this.authManagerFeignClient = authManagerFeignClient; }
@PostConstruct public void init() { logger.info("!!!!!!!!!!!!!!!!!!!!AuthenticationFilter initialized and registered!!!!!!!!!!!!!!!!!!!!"); }
public static class Config {}
@Override public GatewayFilter apply(Config config) {
logger.debug("\n{}",routeValidator.isSecured.test(exchange.getRequest())); if (routeValidator.isSecured.test(exchange.getRequest())) {
//header contains token or not if (!exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) { logger.error("Missing or invalid authorization header"); throw new RuntimeException("Missing authorization header"); }
httpRequest = exchange.getRequest().mutate() .header("loggedInUser", jwtUtil.extractUsername(authHeader)) .header("loggedInUserRoles", rolesAsString) // Only for now as we are taking roles as single value and not as list .build(); logger.debug("Header Info: {}", httpRequest);
} catch (ExpiredJwtException e) { logger.error("JWT token has expired", e); throw new RuntimeException("JWT token has expired", e); } catch (MalformedJwtException e) { logger.error("JWT token is malformed", e); throw new RuntimeException("JWT token is malformed", e); } catch (SignatureException e) { logger.error("JWT signature is invalid", e); throw new RuntimeException("JWT signature is invalid", e); } catch (Exception e) { logger.error("Invalid access: Unauthorized access to application", e); System.out.println("Invalid access...!"); throw new RuntimeException("!!Unauthorized access to application!!"); } }
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder, AuthenticationFilter authFilter) { return builder.routes() .route("Gateway", r -> r .path("/api/gateway/v1/user-info") // The specific path to route .filters(f -> f.filter(authFilter.apply(new AuthenticationFilter.Config()))) .uri("http://localhost:8084")) .build(); } [/code] когда я пытаюсь подключиться к API: /api/gateway/v1/user-info, отображается любой из журналов или system.out.println, присутствующий в authentionFilter. п>