Spring Cloud Gateway 中每个备用请求的 404 错误

hor*_*ius 5 spring-boot spring-cloud api-gateway spring-cloud-gateway

我在 spring 云网关中遇到了一个非常奇特的问题。每个备用请求都会返回 404。这发生在我在 api-gateway 中配置的所有服务中,无一例外。我什至不知道从哪里开始调试这个问题。

这是我用于通用配置的 application.yml 文件。

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
Run Code Online (Sandbox Code Playgroud)

这是我的java配置文件


@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是加载在通用 application.yml 文件之上的 spring 配置文件特定配置文件。

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/**
Run Code Online (Sandbox Code Playgroud)

小智 0

嘿,我最近也遇到了这个问题,我发现这是负载平衡的问题。确保您尝试联系的微服务的 spring.application.name 全部为大写字母(示例),特别是如果您使用 Eureka。(希望能帮助到你)