如何在Spring Boot中将用户请求重定向到在其他服务器上运行的其他Restful API?

时间:2019-06-21 16:27:03

标签: spring-boot redirect forward

我是春季靴子的菜鸟。我正在为某些服务编写网关。在某种情况下,我需要在身份验证后将用户请求转发到其他服务(某些Restful API)。我已经对“转发”和“重定向”进行了一些搜索。我认为我需要“前进”。但是我仍然有一些问题:1.当我将其转发到其他URI(例如abc.dce.com/service/)时,服务是否获得请求主体。 2.如何在春季靴中做到这一点?你们有适合我情况的好榜样吗? (我承认我对此很懒,但是确实有很多让我感到困惑的转发风格。)

//I find this example, but this is forwarding to service in same package //under same Internet. 
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    // forward requests to /admin and /user to their index.html
    registry.addViewController("/portal").setViewName(
            "forward:/app/index.html");
}

1 个答案:

答案 0 :(得分:0)

由于您提到了Spring Boot的新手,所以您可能想看看实现了功能齐全的网关的spring项目。我使用了它的早期版本(zuul),当前的spring-cloud-gateway允许您通过创建spring-boot项目和配置轻松实现完整的网关。它具有许多您可能想作为网关实现的功能(例如添加/删除标头,修改有效载荷等)。如果您需要它们不支持的功能,则可以通过过滤器和它们提供的其他接口来实现。它最初是从Netflix开源的,因此相当全面。

https://spring.io/projects/spring-cloud-gateway

示例项目: https://github.com/spring-cloud-samples/spring-cloud-gateway-sample