【问题标题】:Zuul returns HTTP status 200 instead of 302Zuul 返回 HTTP 状态 200 而不是 302
【发布时间】:2018-05-28 18:48:06
【问题描述】:

我在 Zuul(端口:8080)后面有一个名为 Backend(端口:9090)的服务。

浏览器调用 Zuul 上的 GET 方法,该方法执行重定向。

示例调用:http://localhost:8080/testredirect

结果:

预期结果:

  • 浏览器应该收到 Http status = 302 并更改 URL,导致:
  • 浏览器网址:http://localhost:8080/hello
  • 浏览器显示:“Hello world”

Zuul 似乎返回 200,隐藏重定向,因为它是代理服务器。

在服务器端的GET方法中定义了URL,如何在浏览器中实现302重定向?

这里是后端服务:

@SpringBootApplication
public class BackendApplication {

    @RestController
    @RequestMapping
    public class Backend {
        @GetMapping(value = "/hello")
        public String sayHello() {
            return "Hello world";
        }

        @GetMapping(value = "/testredirect")
        public RedirectView sayRandom() {
            return new RedirectView("/hello");
        }

       public static void main(String[] args) {
          SpringApplication.run(BackendApplication.class, args);
       }
    }
}

Zuul的配置如下:

server:
  port: 8080
zuul:
  routes:
    core:
      sensitive-headers:
      path: /core/**
      url: http://localhost:9090/

注意:spring cloud -> Edgware.RELEASE

【问题讨论】:

  • zuul 只是代理结果,它不会改变状态。您确定没有发生错误或其他原因吗?
  • 我没有看到异常。我得到了正确的内容,但浏览器中的 URL 不正确。浏览器看不到 302,而是看到 200。
  • 我不明白,您获取的内容是否正确?能否提供一个重现问题的示例项目?
  • 您的服务未按预期工作。我们有这个确切的场景,如果服务返回 302,那么它会像您期望的那样通过 zuul 传递回来。
  • 我尝试改进解释并添加一些非常简单的示例代码。

标签: spring-cloud netflix-zuul


【解决方案1】:

spring-boot 版本 Edgeware.RELEASE 中的功能已更改。

在 Edgeware.RELEASE 中:返回 200,以及页面的内容。

在 Dalston.SR4 中:返回 302 并且浏览器重定向。

【讨论】:

    【解决方案2】:

    您可以在 Spring 上下文中定义一个 bean。

    @Bean 
    public ApacheHttpClientFactory apacheHttpClientFactory() {
         return new ApacheHttpClientFactory() {
             @Override 
             public HttpClientBuilder createBuilder() {
                 return HttpClientBuilder.create()
                         .disableContentCompression()
                         .disableCookieManagement()
                         .useSystemProperties()
                         .disableRedirectHandling();
             }
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2011-03-25
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2011-11-09
      相关资源
      最近更新 更多