Skip to content

Instantly share code, notes, and snippets.

@dk8996
Last active June 25, 2017 20:53
Show Gist options
  • Save dk8996/267962fde8cf05d5a1b3 to your computer and use it in GitHub Desktop.
Save dk8996/267962fde8cf05d5a1b3 to your computer and use it in GitHub Desktop.
Play Framework Filter for AWS Elastic Load Balancer (forward HTTP to HTTPS)
import com.typesafe.scalalogging.slf4j.Logging
import play.api.mvc._
import scala.concurrent.Future
import play.mvc.Results._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
object HTTPSRedirectFilter extends Filter with Logging {
def apply(nextFilter: (RequestHeader) => Future[SimpleResult])(requestHeader: RequestHeader): Future[SimpleResult] = {
//play uses lower case headers.
requestHeader.headers.get("x-forwarded-proto") match {
case Some(header) => {
if ("https" == header) {
nextFilter(requestHeader).map { result =>
result.withHeaders(("Strict-Transport-Security", "max-age=31536000"))
}
} else {
Future.successful(Results.Redirect("https://" + requestHeader.host + requestHeader.uri, 301))
}
}
case None => nextFilter(requestHeader)
}
}
}
@dk8996
Copy link
Author

dk8996 commented May 14, 2014

This is Filter for Play Framework 2.2.x that enables http to https redirect, designed to work within AWS Elastic Load Balancer.

You can read more about it in this blog post:
http://www.mentful.com/2014/05/25/play-framework-filter-for-aws-elastic-load-balancer-forward-http-to-https/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment