Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC "redirect:" prefix always redirects to http -- how do I make it stay on https?

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 an InternalResourceViewResolver:

<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" /> </bean> 

I have a pretty simple handler method in my controller, but I've simplified it even more for this example:

@RequestMapping("/groups") public String selectGroup() {     return "redirect:/"; } 

The problem is, if I browse to https://my.domain.com/groups, I end up at http://my.domain.com/ after the redirect. (In actuality my load-balancer redirects all http requests to https, but this just causes multiple browser alerts of the type "You are leaving/entering a secure connection" for folks who have such alerts turned on.)

So the question is: how does one get spring to redirect to https when that's what the original request used?

like image 265
super_aardvark Avatar asked Aug 03 '10 22:08

super_aardvark


People also ask

How use redirect attribute in Spring MVC?

Method SummaryAdd the supplied attribute to this Map using a generated name . Add the supplied attribute under the supplied name. Add the given flash storage using a generated name . Add the given flash attribute.

What is the difference between forward and redirect in Spring MVC?

Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.

How do I redirect a URL in spring?

We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.

How do I redirect one HTML page to another in spring boot?

Try a URL http://localhost:8080/HelloWeb/index and you should see the following result if everything is fine with your Spring Web Application. Click the "Redirect Page" button to submit the form and to get the final redirected page.


1 Answers

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.)

like image 98
super_aardvark Avatar answered Sep 19 '22 15:09

super_aardvark