简体   繁体   English

路由-Spring Boot Web App和AWS Application Load Balancer

[英]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.

相关问题 通过AWS Application Load Balancer的Spring Web套接字不起作用 - Spring Web Sockets over AWS Application Load Balancer not working Spring Boot Web应用程序加载由另一个应用程序创建的新网页 - Spring boot web app load new web pages created by another application 无法在Spring Boot应用程序中加载应用程序上下文 - Not able to load Application Context in Spring Boot app 如何在 AWS ElasticBeastalk Application Load Balancer 上运行的 Spring Web 应用程序上获取请求者 IP - How to get thei requester IP on a Spring webapp running on AWS ElasticBeastalk Application Load Balancer AngularJS和Spring启动Web应用程序作为独立的桌面应用程序 - AngularJS and Spring boot web application as a standalone desktop app Spring Boot - 非Web应用程序的长期运行应用程序 - Spring Boot - long running application for non-web app 由负载均衡器处理时,Spring Boot CSRF失败 - Spring Boot CSRF fails when handled by Load balancer Kafka中的独立生产者使用Spring Boot和负载均衡器 - Independent producers in Kafka using Spring Boot and load balancer 将Spring Web应用程序迁移到Spring Boot - Migrating Spring web application to Spring Boot 如何从 Spring 引导应用程序属性加载 AWS 区域特定属性? - How do I load AWS region specific properties from Spring Boot application properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM