mkopylec / charon-spring-boot-starter

Reverse proxy implementation in form of a Spring Boot starter.
Apache License 2.0
240 stars 54 forks source link

Does it support path rewrite? #72

Closed thiagolocatelli closed 5 years ago

thiagolocatelli commented 5 years ago

Does this library support path rewrite?

For example in spring cloud gateway, we are able to rewrite the path and use regular expressions.

    b.route("route",
        r -> r.path("some/path/**")
            .filters(f -> {
                f.rewritePath("/some/path/(?<segment>.*)", "/new/path/${segment}");
                return f;
            })
            .uri(destinationUri));

If not, any intent to add this to the library?

mkopylec commented 5 years ago

Hi, The library does not support path rewriting from yml properties but you can intercept request by implementing ForwardedRequestInterceptor. Intercepting allows you to modify any aspect of the request, not just path.

thiagolocatelli commented 5 years ago

but the library does not allow me to specify a interceptor per mapping, my understanding is that the interceptor is global. It would be interesting if we could provide a interceptor implementation per mapping. Not all my mappings require interceptor, just some mappings, the ones I need to hide the parts of the path and rewrite it. The rewrite function should be interesting in this project.

mkopylec commented 5 years ago

Ok, I see your point now. Indeed the rewriting support will be a nice feature in Charon. Stay tuned for the next release :)

thiagolocatelli commented 5 years ago

Thank you @mkopylec . The rewrite option in the configuration would be great but also, if you can squeeze a customer interceptor implementation per mapping it would be great. Some of my mappings need different headers to be included, so having an custom interceptor per mapping, instead of just one global, that would be great. It could be the name of the bean that implements the custom interceptor.

mkopylec commented 5 years ago

I'll inlcude MappingProperties in the RequestData. Is this ok for you?

mkopylec commented 5 years ago

Also I'll add a yml configuration for path rewriting per mapping.

thiagolocatelli commented 5 years ago

I was able to achieve this by applying some changes to your project:

2018-11-20 18:45:16.878 DEBUG 55594 --- [nio-8080-exec-1] c.g.m.c.core.http.ReverseProxyFilter     : Incoming: GET /get
2018-11-20 18:45:16.893 DEBUG 55594 --- [nio-8080-exec-1] c.g.s.s.r.f.GlobalRequestInterceptor     : GlobalRequestInterceptor executed
2018-11-20 18:45:16.894 DEBUG 55594 --- [nio-8080-exec-1] c.g.s.s.r.f.r.HttpBinRequestInterceptor1 : HttpBinRequestInterceptor1 executed
2018-11-20 18:45:16.894 DEBUG 55594 --- [nio-8080-exec-1] c.g.s.s.r.f.r.HttpBinRequestInterceptor2 : HttpBinRequestInterceptor2 executed
2018-11-20 18:45:17.190 DEBUG 55594 --- [nio-8080-exec-1] c.g.m.charon.core.http.RequestForwarder  : Forwarding: GET /get -> http://httpbin.org/get 200
2018-11-20 18:45:17.191 DEBUG 55594 --- [nio-8080-exec-1] c.g.s.s.r.f.GlobalResponseInterceptor    : GlobalResponseInterceptor executed
2018-11-20 18:45:17.191 DEBUG 55594 --- [nio-8080-exec-1] .g.s.s.r.f.r.HttpBinResponseInterceptor1 : HttpBinResponseInterceptor1 executed
2018-11-20 18:45:17.191 DEBUG 55594 --- [nio-8080-exec-1] .g.s.s.r.f.r.HttpBinResponseInterceptor2 : HttpBinResponseInterceptor2 executed
2018-11-20 18:45:17.191 DEBUG 55594 --- [nio-8080-exec-1] c.g.m.charon.core.retry.LoggingListener  : Attempt 1 to forward HTTP request using 'http-bin-get' mapping has succeeded

Path rewriting is also working:

config:

charon:
  mappings:
    -
      name: http-bin-getrewrite
      strip-path: false
      path: /any
      path-rewrite: /any/(?<segment>.*), /anything/${segment}
      forwarded-request-interceptors: httpBinRequestInterceptor1, httpBinRequestInterceptor2
      received-response-interceptors: httpBinResponseInterceptor1, httpBinResponseInterceptor2
      destinations: http://httpbin.org
    -
      name: http-bin-get
      strip-path: false
      path: /get
      forwarded-request-interceptors: httpBinRequestInterceptor1, httpBinRequestInterceptor2
      received-response-interceptors: httpBinResponseInterceptor1, httpBinResponseInterceptor2
      destinations: http://httpbin.org

log for http://127.0.0.1:8080/any/customer/policy/cart?variable=123:

2018-11-20 19:03:00.158 DEBUG 56666 --- [nio-8080-exec-4] c.g.m.c.core.http.ReverseProxyFilter     : Incoming: GET /any/customer/policy/cart?variable=123
2018-11-20 19:03:00.159 DEBUG 56666 --- [nio-8080-exec-4] c.g.s.s.r.f.GlobalRequestInterceptor     : GlobalRequestInterceptor executed
2018-11-20 19:03:00.159 DEBUG 56666 --- [nio-8080-exec-4] c.g.s.s.r.f.r.HttpBinRequestInterceptor1 : HttpBinRequestInterceptor1 executed
2018-11-20 19:03:00.159 DEBUG 56666 --- [nio-8080-exec-4] c.g.s.s.r.f.r.HttpBinRequestInterceptor2 : HttpBinRequestInterceptor2 executed
2018-11-20 19:03:00.331 DEBUG 56666 --- [nio-8080-exec-4] c.g.m.charon.core.http.RequestForwarder  : Forwarding: GET /any/customer/policy/cart?variable=123 -> http://httpbin.org/anything/customer/policy/cart?variable=123 200
2018-11-20 19:03:00.331 DEBUG 56666 --- [nio-8080-exec-4] c.g.s.s.r.f.GlobalResponseInterceptor    : GlobalResponseInterceptor executed
2018-11-20 19:03:00.331 DEBUG 56666 --- [nio-8080-exec-4] .g.s.s.r.f.r.HttpBinResponseInterceptor1 : HttpBinResponseInterceptor1 executed
2018-11-20 19:03:00.331 DEBUG 56666 --- [nio-8080-exec-4] .g.s.s.r.f.r.HttpBinResponseInterceptor2 : HttpBinResponseInterceptor2 executed
2018-11-20 19:03:00.331 DEBUG 56666 --- [nio-8080-exec-4] c.g.m.charon.core.retry.LoggingListener  : Attempt 1 to forward HTTP request using 'http-bin-getrewrite' mapping has succeeded
sabob commented 5 years ago

Hi all,

If possible a custom Interceptor per mapping would be great.

I'll inlcude MappingProperties in the RequestData.

Would be useful as well, Can the mapping also be passed to the response callback? That way I can use IFs to perform custom logic per mapping.

Kind regards

Bob

mkopylec commented 5 years ago

@sabob I have added mappings to interceptors in 3.1.0 version.

sabob commented 5 years ago

Thanks very much, works great!

Kind regards

Bob

mkopylec commented 5 years ago

The new version of Charon will support path rewriting. There will be a configuration option exposed for:

mkopylec commented 5 years ago

The new 4.0.0 version supports path rewriting