Redirect HTTP to HTTPS with Ninja framework on Heroku

157 Views Asked by At

How to configure a Ninja web application running on Heroku to force the use of SSL, that is, redirect all requests to HTTPS?

2

There are 2 best solutions below

0
Patrick On BEST ANSWER

Here is the class to add in the conf package:

public class Filters implements ApplicationFilters {
  @Override
  public void addFilters (List<Class<? extends Filter>> list) {
    list.add (HttpsFilter.class);
  }
  public static class HttpsFilter implements Filter {
    @Override
    public Result filter (FilterChain filterChain, Context context) {
      if ("http".equals (context.getHeader ("X-Forwarded-Proto"))) {
        return Results.redirect ("https://" + context.getHostname ()
          + context.getRequestPath ());
      }
      return filterChain.next (context);
    }
  }
}
8
Gianfrancesco Aurecchia On

If you look good in the ninja framework documentation it is indicated how to configure it to get what you want

http://www.ninjaframework.org/documentation/configuration_and_modes.html