Skip to content

Instantly share code, notes, and snippets.

@urcadox
Created November 18, 2016 11:30
Show Gist options
  • Save urcadox/a54c20f0b86f1e9d36a341c861efd2dd to your computer and use it in GitHub Desktop.
Save urcadox/a54c20f0b86f1e9d36a341c861efd2dd to your computer and use it in GitHub Desktop.
Play 2.5 HTTPS redirection on Clever Cloud
play.http.forwarded.version="x-forwarded"
play.http.forwarded.trustedProxies=["0.0.0.0","::"]
play.http.filters=controllers.MyFilters
package controllers
import javax.inject._
import play.api._
import play.api.http.DefaultHttpFilters
import play.api.mvc._
import play.api.Environment
import scala.concurrent.{ExecutionContext, Future}
import akka.stream.Materializer
class TLSFilter @Inject() (
implicit val mat: Materializer, ec: ExecutionContext, env: Environment
) extends Filter {
def apply(nextFilter: RequestHeader => Future[Result])(requestHeader: RequestHeader): Future[Result] = {
if(requestHeader.headers.get("X-Forwarded-Proto").getOrElse("http") != "https" && env.mode == play.api.Mode.Prod)
Future.successful(Results.MovedPermanently("https://" + requestHeader.host + requestHeader.uri))
else
nextFilter(requestHeader).map(_.withHeaders("Strict-Transport-Security" -> "max-age=31536000"))
}
}
class MyFilters @Inject() (
tls: TLSFilter
) extends DefaultHttpFilters(tls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment