Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unable to run the api-gateway while using docker compose. #4417

Open
Satyam018 opened this issue Feb 27, 2025 · 2 comments
Open

Unable to run the api-gateway while using docker compose. #4417

Satyam018 opened this issue Feb 27, 2025 · 2 comments

Comments

@Satyam018
Copy link

api-gateway-1 | 2025-02-27T05:04:36.938Z INFO 1 --- [api-gateway] [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://server-registry:8761/eureka/}, exception=I/O error on GET request for "http://server-registry:8761/eureka/apps/": Connect to http://server-registry:8761 [se
rver-registry/172.19.0.6] failed: Connection refused stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://server-registry:8761/eureka/apps/": Connect to http://server-registry:8761 [server-registry/172.19.0.6] failed: Connection refused

Docker Compose File (API-gateway Code):

services:
server-registry:
build: ./server-registry
ports:
- "8761:8761"
networks:
- my-networks

api-gateway:
build: ./api-gateway
depends_on:
- server-registry
ports:
- "8060:8060"
links:
- server-registry
networks:
- my-networks

customer-service:
build: ./customer-service
depends_on:
- server-registry
ports:
- "8080:8080"
networks:
- my-networks

API-gatework (application.properties) file:

spring.application.name=api-gateway

server.port=8760

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

eureka.client.serviceUrl.defaultZone=http://server-registry:8761/eureka/ //(the same URL for other services)

@OlgaMaciaszek
Copy link
Collaborator

Hello, @Satyam018. If you'd like us to look into it, please provide a minimal, complete, verifiable example that reproduces the issue, along with the docker-compose files.

@Satyam018
Copy link
Author

Satyam018 commented Mar 6, 2025

I am working on a spring-boot microservices-based application. I have created separate services for user authentication using auth-services however, I wanted to verify by the user using the jwt token passed in api-gateway. but for some reason I am not able to call the authfilter.

application.properties

spring.application.name=api-gateway
server.port=8760
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
eureka.client.serviceUrl.defaultZone=http://server-registry:8761/eureka/
jwt.secret=Xw8vNd9eXplA7BY7Gg7z9y5fJ3TVLY5D4YJgWXjUQGk
spring.cloud.gateway.routes[0].id=auth-service
spring.cloud.gateway.routes[0].uri=http://localhost:8086
spring.cloud.gateway.routes[0].predicates[0]=Path=/auth/**
spring.cloud.gateway.routes[0].filters[0]=AuthFilter

AuthFilter class

package com.example.api_gateway.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@component
public class AuthFilter implements GatewayFilterFactory<AuthFilter.Config> {

@Autowired
RouteValidator routeValidator;

@Autowired
private JWTService jwtService;


@Override
public GatewayFilter apply(Config config) {
    return new GatewayFilter() {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ServerHttpRequest serverHttpRequest=exchange.getRequest();
            if (routeValidator.isSecured(serverHttpRequest)){
                if(!routeValidator.hasAuthorised((ServerWebExchange) serverHttpRequest)){
                    throw new RuntimeException("Missing Authoriztaion Header");
                }

                String token=serverHttpRequest.getHeaders().getFirst(HttpHeaders.
       AUTHORIZATION
          );
                if(token!=null && token.startsWith("Bearer ")){
                    token=token.substring(7);
                }
                if (!jwtService.validateToken(token)){
                    throw new RuntimeException("Invalid Token or Token Expired");
                }
            }


            return chain.filter(exchange);
        }
    };

}


public static class Config{}

@Override
public Class<Config> getConfigClass() {
    return Config.class;
}

}

Auth validator

package com.example.api_gateway.filter;

import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;

@component
public class RouteValidator {

private final List<String> OPEN_END_POINT=List.of(
        "/auth/register",
        "/auth/token"
);


public boolean isSecured(ServerHttpRequest request){
    String requestPath=request.getURI().getPath();
    System.
        out
        .println("Request path: " + requestPath);  // Log request path
    for (String uri:OPEN_END_POINT){
        if(requestPath.contains(uri))return false;
    }
    return true;
}

public boolean hasAuthorised(ServerWebExchange serverWebExchange){
    return serverWebExchange.getRequest().getHeaders().containsKey(HttpHeaders.
     AUTHORIZATION
      );
}
}

JWTservices

package com.example.api_gateway.filter;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@component
public class JWTService {

@Value("${jwt.secret}")
private  String SECRET;


public boolean validateToken(String token){
    Jws<Claims> claimsJws=Jwts.
parserBuilder
   ().setSigningKey(getSignKey()).build().parseClaimsJws(token);
    return true;
}



private Key getSignKey(){
    byte[] keyBytes= Decoders.
  BASE64
  .decode(SECRET);
    return Keys.
      hmacShaKeyFor
  (keyBytes);
    }
}

I am not able to call the RouteValidator Functions. what am I missing?

Thanks in advance.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants