[英]Routing - Spring Boot Web App & AWS Application Load Balancer
I have a Spring Boot web app with the usual routings: 我有一个具有常规路由的Spring Boot Web应用程序:
@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. 我现在将其部署在一个AWS Application Load Balancer后面,在这里我使用了基于路径的路由来定位我的应用程序。
I've configured /admin/*
on the Load Balancer to forward to my app, which works fine. 我已经在负载均衡器上配置了/admin/*
来转发到我的应用程序,效果很好。
The issue though is that my app is now seeing /admin/account
, rather than the /account
it is expecting. 但是问题是我的应用程序现在看到的是/admin/account
,而不是预期的/account
。
AWS says: AWS说:
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. 例如,如果规则的路径模式为/ img / *,则该规则会将对/img/picture.jpg的请求转发到指定的目标组,作为对/img/picture.jpg的请求。
Is there any tidy way around this? 有什么整洁的方法吗? Or do I have to resort to something like this: 还是我必须诉诸如下:
@GetMapping("*/account")
I don't think that functionality exists in AWS Applicxation loadbalancer -- check this out stackoverflow.com/questions/39317685 我认为AWS Applicxation负载平衡器中不存在功能-请检查一下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): 您可以在服务器上使用反向代理并执行以下操作(这是Nginx配置):
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. 这将删除/ admin部分并将其他所有内容代理到您的服务。 Just be sure to have the trailing / after the service port. 只需确保在服务端口后面有尾随/。
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. 我唯一想做的事情(除了Web服务器的URL重写或诸如Zuul的反向代理)是在应用程序中进行URL重写。
An easy way to do that is using UrlRewriteFilter 一种简单的方法是使用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: 过滤器的Maven坐标:
<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 可以在此处找到配置文件示例: http : //cdn.rawgit.com/paultuckey/urlrewritefilter/master/src/doc/manual/4.0/urlrewrite.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.