I've found many question about enforcing HTTPS under heroku but no response are about the java.
Here are the link I found :
Scala : http://www.andersen-gott.com/2012/03/using-unfiltered-and-https-on-heroku.html
Rails : Rails - How to Redirect from http://mysite.com to https://www.mysite.com
Please note that I am using Spring MVC 3.1 so I would prefer a solution based on WebMvcConfigurerAdapter.addInterceptors(InterceptorRegistry registry)
Here is hte solution based on spring mvc :
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class ForceHerokuSsl extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String proto = request.getHeader("x-forwarded-proto");
return proto == null || "https".equalsIgnoreCase(proto);
}
}
Then in the configuration :
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ForceHerokuSslInterceptor());
}
}