Ayo K
Ayo K

Reputation: 1774

Spring redirect: using HTTPS with code based configuration

I have a Spring Boot app deployed on a web server I have a controller annotated @Controller that does a redirect by returning redirect:/home this redirect changes the https to http I already saw an answer to the question here but I'm totally unfamiliar with XML based configuration and want to know if there's a way I can configure the bean from the code and not using XML.

Upvotes: 1

Views: 1624

Answers (2)

Pino
Pino

Reputation: 9303

In my case the application is behind Ngnix. HTTPS is managed by Ngnix and the app uses HTTP only. A little change in Ngnix configuration solved the problem: I added this line in each location block:

proxy_redirect ~^http:(.*)$ https:$1;

Upvotes: 1

Karol Dowbecki
Karol Dowbecki

Reputation: 44962

You could use JavaConfig to set the redirectHttp10Compatible property to false to prevent this.

@Bean
public ViewResolver configureViewResolver() {
  InternalResourceViewResolver vr = new InternalResourceViewResolver();
  vr.setRedirectHttp10Compatible(false);
  // other options
  return vr;
}

As per InternalResourceViewResolver.setRedirectHttp10Compatible() docs:

Set whether redirects should stay compatible with HTTP 1.0 clients.

Upvotes: 1

Related Questions