Created
November 18, 2016 11:30
-
-
Save urcadox/a54c20f0b86f1e9d36a341c861efd2dd to your computer and use it in GitHub Desktop.
Play 2.5 HTTPS redirection on Clever Cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
play.http.forwarded.version="x-forwarded" | |
play.http.forwarded.trustedProxies=["0.0.0.0","::"] | |
play.http.filters=controllers.MyFilters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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