Spring MVC “redirect:” prefix always redirects to http — how do I make it stay on https?

后端 未结 7 1209
鱼传尺愫
鱼传尺愫 2020-12-07 16:28

I solved this myself, but I spent so long discovering such a simple solution, I figured it deserved to be documented here.

I have a typical Spring 3 MVC setup with

相关标签:
7条回答
  • 2020-12-07 16:58

    Are you sure?

    Looking at the code it seems there is no difference. Both variants call response.encodeRedirectURL(targetUrl), see Line 388 of RedirectView:

    if (http10Compatible) {
        // Always send status code 302.
        response.sendRedirect(response.encodeRedirectURL(targetUrl));
    }
    else {
        HttpStatus statusCode = getHttp11StatusCode(request, response, targetUrl);
        response.setStatus(statusCode.value());
        response.setHeader("Location", response.encodeRedirectURL(targetUrl));
    }
    

    I had the same problem, but it was triggered by setting up tomcat behind a loadbalancer. The loadbalancer does the SSL handshake and forwards to tomcat a plain http connection.

    Solution would be to send a special Http Header in your Loadbalancer, so tomcat can "trust" this connection. Using a servlet filter should set response.isSecure flag. Then overwrite RedirectView to see if response.isSecure and handle it the right way.

    I kept this solution short because i am not sure if it machtes the question.

    0 讨论(0)
  • 2020-12-07 16:58

    if you use springmvc ,you can try the following:

    modelAndView.setView(new RedirectView("redirect url path", true, false));
    
    0 讨论(0)
  • 2020-12-07 17:02

    What worked for me is adding this to application.properties server.tomcat.use-relative-redirects=true

    So when you have:

    public function redirect() {
      return "redirect:/"
    }
    

    Without the server.tomcat.use-relative-redirects it will add a Location header like: http://my-host.com/. With the server.tomcat.use-relative-redirects it will look like: /. So it will be relative to the current page from browser perspective.

    0 讨论(0)
  • 2020-12-07 17:05

    The short answer is, set the InternalResourceViewResolver's redirectHttp10Compatible property to false:

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/" />
      <property name="suffix" value=".jsp" />
      <property name="redirectHttp10Compatible" value="false" />
    </bean>
    

    You could do this on a per-request basis instead, by having your handler method return View instead of String, and creating the RedirectView yourself, and calling setHttp10Compatible(false).

    (It turns out the culprit is HttpServletResponse.sendRedirect, which the RedirectView uses for HTTP 1.0 compatible redirects, but not otherwise. I guess this means it's dependent on your servlet container's implementation (?); I observed the problem in both Tomcat and Jetty.)

    0 讨论(0)
  • 2020-12-07 17:10

    Spring Boot provides this nice configuration based solution to this if you're running behind a proxy server, simply add these two properties to your application.properties file:

    server.tomcat.remote_ip_header=x-forwarded-for
    server.tomcat.protocol_header=x-forwarded-proto
    

    This works for me deploying the otherwise unchanged spring-boot-sample-web-ui to Elastic Beanstalk behind an https load balancer. Without these properties the redirect on line 68 of the MessageController defaults to http and hangs.

    Hope this helps.

    0 讨论(0)
  • 2020-12-07 17:13

    Since Spring Boot 2.1 you have to add the following configuration to your application.properties:

    server.use-forward-headers=true 
    

    or application.yml:

    server:
      use-forward-headers: true
    
    0 讨论(0)
提交回复
热议问题