How use code to configure Spring cloud gateway supported GatewayFilter like SecureHeaders

684 views Asked by At

Spring cloud gateway has many GatewayFilters like AddRequestHeader、SecureHeaders,use configure file like application.yaml is easy to use,but how use this GatewayFilters in java code?

spring:
  cloud:
    gateway:
      routes:
        - id: route-1
          uri: http://localhost1:5230
          predicates:
            - Path=/api/**
          filters:
            - SecureHeaders

Current I use the follow code to configure

route("route-1", predicateSpec -> predicateSpec.path("/api/**")
                .uri("http://localhost:5230")
                .filter(new SecureHeadersGatewayFilterFactory(new SecureHeadersProperties()).apply(new Object())))

Has more better solution?

1

There are 1 answers

0
spencergibb On BEST ANSWER
route("route-1", predicateSpec -> predicateSpec.path("/api/**")
                .filters(f -> f.secureHeaders()).uri("http://localhost:5230")