horatius
horatius

Reputation: 814

404 error in spring cloud gateway for every alternate request

I am encountering a very peculiar problem in spring cloud gateway. Every alternate request returns a 404. This happens across all services I've configured in the api-gateway without exception. I don't even know where to start to debug this problem.

Here's my application.yml file for common config.

server:
  port: 8080
  ssl:
    enabled: true
    key-store: classpath:keystore.p12
    key-store-password: password
    key-store-type: pkcs12
    key-alias: tomcat

security:
  require-ssl=true:
logging:
  level:
    org:
      springframework:
        cloud.gateway: DEBUG
        http.server.reactive: DEBUG
        web.reactive: DEBUG

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      httpclient:
        ssl:
          useInsecureTrustManager: true

Here's my java config file


@EnableWebFluxSecurity
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,
                                                            ReactiveClientRegistrationRepository clientRegistrationRepository) {
        // Authenticate through configured OpenID Provider
        http.oauth2Login();
        // Also logout at the OpenID Connect provider
        http.logout(logout -> logout.logoutSuccessHandler(new OidcClientInitiatedServerLogoutSuccessHandler(
                clientRegistrationRepository)));
        // Require authentication for all requests
        http.authorizeExchange().anyExchange().authenticated();
        // Allow showing /home within a frame
        http.headers().frameOptions().mode(Mode.SAMEORIGIN);
        // Disable CSRF in the gateway to prevent conflicts with proxied service CSRF
        http.csrf().disable();
        return http.build();
    }
}

Here's the spring profile specific config file that gets loaded on top of the common application.yml file.

spring:
  security:
    oauth2:
      client:
        provider:
          keycloak:
            issuerUri: http://localhost:9080/auth/realms/mylocal
            userNameAttribute: preferred_username
        registration:
          keycloak:
            clientId: api-gateway-client
            clientSecret: abcdefgh-ijkl-mnop-qrst-uvwxyz5d6a9
  cloud:
    gateway:
      default-filters:
        - TokenRelay
      routes:
        - id: news
          uri: http://localhost:8082/news
          predicates:
            - Path= /news/**
        - id: customers
          uri: http://localhost:8083/customers
          predicates:
            - Path= /boards/**
        - id: search
          uri: http://localhost:8085/search
          predicates:
            - Path= /search/**

Upvotes: 7

Views: 10541

Answers (4)

Manikandan S
Manikandan S

Reputation: 1

Check if there are two or more microservices using the same spring.application.name and registered with eureka.

Upvotes: 0

Purrini
Purrini

Reputation: 1

I had a similar issue. The reason was in registration of several services under the same application name in my Eureka server. Just like this: Eureka instances screenshot

Those are completely different services, but they are registered under one application name. So when a request is made through load balancer, the latter tries to use both of the service urls. One service is able to serve request correctly, but another one may know nothing about the requested path, that is why 404 is returned.

Upvotes: 0

Sudarshan
Sudarshan

Reputation: 33

I know it's an old question... But might help future readers.

hey, here is the simplest solution I found for myself, in the browser url, instead of 'localhost', give your system's name.

ex: http://hp:8083/SERVICE-NAME/customers/**

also make sure to name all your spring application in uppercase.

Upvotes: -1

Lufen Martofilia
Lufen Martofilia

Reputation: 15

Hey i got the issue recently too, I figure out that it was the load balancing the issue. Make sure that your spring.application.name of the microservice that you try to contact are all in capital letter (EXAMPLE) especially if you use Eureka. (hope it helps)

Upvotes: 0

Related Questions