Created
February 11, 2016 17:23
-
-
Save urcadox/5739fdec5255a6ab96b1 to your computer and use it in GitHub Desktop.
Play 2.4.6 HTTPS filter
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 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