Spring-cloud-gateway: Filter registration for custom filters

Created on 13 Nov 2017  路  7Comments  路  Source: spring-cloud/spring-cloud-gateway

I have written a custom filter implementing Gateway Filter. But this filter is not being applied to the request. How do i add this to the list of default filters. is there an option to do filter registration?

question

Most helpful comment

@pravinkumarb84 Did you get CORS support working? If so will you post an example?

All 7 comments

You need to implement GatewayFilterFactory and register it as a spring bean.

Thanks. I am trying to implement CORS filter for all requests. I have implemented it as a custom filter with GatewayFilterFactory and register it as a spring bean. I have also implemented "Ordered" to give it highest precedence. But it is not getting applied. How to make this custom filter applied before all filters?

when i try a path in the routes, it navigates to the URI without the custom filter being applied.

My sample application.yml is as below:

spring:
application:
name: sample
cloud:
gateway:
routes:
- id: samplepath_route
uri: http://httpbin.org:80/
predicates:
- Path=/samplepath/

cors:
mappings:
/**:
allowedOrigins:
- localhost
- Ip address
allowedMethods:
- GET

Filters have to be applied to every route: see https://github.com/spring-cloud/spring-cloud-gateway/blob/2.0.x/docs/src/main/asciidoc/spring-cloud-gateway.adoc#addrequestparameter-gatewayfilter-factory

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: http://example.org
        filters:
        - AddResponseHeader=X-Response-Foo, Bar

Is how to add a filter named AddResponseHeaderGatewayFilterFactory.

Please learn how to properly format code and logs.

Thanks Spencer. I am able to implement the custom filter now.

@pravinkumarb84 Did you get CORS support working? If so will you post an example?

Please learn how to properly format code and logs.

Please learn some tact.

The documentation for custom filters outside of yaml config is nonexistent.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carrielfonseca picture carrielfonseca  路  29Comments

tianmingxing picture tianmingxing  路  32Comments

bcelenk picture bcelenk  路  31Comments

re6exp picture re6exp  路  36Comments

Burt-L picture Burt-L  路  25Comments