Skip to content

Instantly share code, notes, and snippets.

@urcadox
Created February 11, 2016 17:23
Show Gist options
  • Save urcadox/5739fdec5255a6ab96b1 to your computer and use it in GitHub Desktop.
Save urcadox/5739fdec5255a6ab96b1 to your computer and use it in GitHub Desktop.
Play 2.4.6 HTTPS filter
package controllers;
import play.api._
import play.api.http.HttpFilters
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.Play.current
class TLSFilter extends Filter {
def apply(nextFilter: RequestHeader => Future[Result])(requestHeader: RequestHeader): Future[Result] = {
if(requestHeader.headers.get("X-Forwarded-Proto").getOrElse("http") != "https" && Play.isProd)
Future.successful(Results.MovedPermanently("https://" + requestHeader.host + requestHeader.uri))
else
nextFilter(requestHeader).map(_.withHeaders("Strict-Transport-Security" -> "max-age=31536000"))
}
}
class MyFilters extends HttpFilters {
val filters = Seq(new TLSFilter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment