Last active
June 25, 2017 20:53
-
-
Save dk8996/267962fde8cf05d5a1b3 to your computer and use it in GitHub Desktop.
Play Framework Filter for AWS Elastic Load Balancer (forward HTTP to HTTPS)
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
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/