1

I have a Spring Boot web app with the usual routings:

@GetMapping("/account")
public String search(@RequestParam(value="search"...

I've now deployed this behind an AWS Application Load Balancer, where I have used path based routing to target my app.

I've configured /admin/* on the Load Balancer to forward to my app, which works fine.

The issue though is that my app is now seeing /admin/account, rather than the /account it is expecting.

AWS says:

Note that the path pattern is used to route requests but does not alter them. For example, if a rule has a path pattern of /img/*, the rule would forward a request for /img/picture.jpg to the specified target group as a request for /img/picture.jpg.

Is there any tidy way around this? Or do I have to resort to something like this:

@GetMapping("*/account")
Kong
  • 8,792
  • 15
  • 68
  • 98

2 Answers2

0

I don't think that functionality exists in AWS Applicxation loadbalancer -- check this out stackoverflow.com/questions/39317685

Quoting the relevant part here

you can use a reverse proxy on your server and do something like this (this is Nginx configuration):

server {
  listen 80 default_server;

  location /secured/ {
    proxy_pass http://localhost:{service_port}/;
  }
}

This will strip the /admin part and proxy everything else to your service. Just be sure to have the trailing / after the service port.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
0

The only thing I can think of doing (besides web server url rewrites or a reverse proxy like Zuul) is to do the url rewrites within your app.

An easy way to do that is using UrlRewriteFilter

@Bean
public FilterRegistrationBean urlRewriteFilter() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    filterRegistration.setFilter(new UrlRewriteFilter());

    // You probably want to set the order very low so it fires before
    // other filters, especially if you're using Spring Security
    filterRegistration.setOrder(Integer.MIN_VALUE);

    filterRegistration.setUrlPatterns(Collections.singletonList("/*"));

    Map<String, String> initParams = new HashMap<>();
    initParams.put("confPath", "url_rewrite.xml");

    filterRegistration.setInitParameters(initParams);

    return filterRegistration;
}

The maven coordinates for the filter:

<dependency>
    <groupId>org.tuckey</groupId>
    <artifactId>urlrewritefilter</artifactId>
    <version>4.0.3</version>
</dependency>

A configuration file example can be found here: http://cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/urlrewrite.xml

int21h
  • 819
  • 7
  • 15