404 error in spring cloud gateway for every alternate request
Asked Answered
R

4

7

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/**
Randarandal answered 25/5, 2020 at 18:47 Comment(1)
Getting the same issue with spring-cloud-gateway, have you able to solve it?Broadcloth
S
0

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)

Scraper answered 29/5, 2020 at 11:28 Comment(4)
Thanks. Load balancer is a a good avenue to explore. Can you put in your sample configuration here? I tried the capital letter suggestion and it's not working.Randarandal
Are you using a config server ? if yes, just add in your bootstrap.properties or .yml the following : spring.application.name=MICROSERVICENAME all in capitals for exemple : spring.application.name=OAUTH-SERVER and make sure that you try to have access to an instance with your load balancer using the annotation : @RibbonClient(name = "OAUTH-SERVER") if you use ribbon :) and by following the same exampleScraper
interesting one, do you have any documentation or a technical reference that these names must be in capitals?, I've being using undercase for years and that never was a problem.Yerxa
@Yerxa I didn't see the notification I'm gonna try to find it now as it was a moment ago :) EDIT : "vallues returned from eureka are uppercase ...." source : cloud.spring.io/spring-cloud-static/spring-cloud.htmlScraper
S
0

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.

Siding answered 12/12, 2021 at 10:17 Comment(0)
D
0

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

Denten answered 5/11, 2022 at 19:11 Comment(0)
C
-1

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.

Conradconrade answered 24/1, 2021 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.