Skip to content

Instantly share code, notes, and snippets.

@humpydonkey
Forked from guillaumebort/gist:8314512
Last active February 2, 2019 07:18
Show Gist options
  • Save humpydonkey/4abe613ac88f65ff2af7 to your computer and use it in GitHub Desktop.
Save humpydonkey/4abe613ac88f65ff2af7 to your computer and use it in GitHub Desktop.
[Enforce HTTPS (Play!)]Force HTTPS decorator for Play framework #scala #play #ssl #web
def forceHttps(handler: Handler, request: RequestHeader): Handler = {
(isHTTPSRequired, isRequestSecure(request), request.method, request.uri) match {
// HTTPS is supported but not required for the API
case (_, _, _, uri) if uri.startsWith("/api") => handler
// HTTPS is required here, redirect GET requests
case (true, false, "GET", uri) => Action(Results.Redirect(s"https://${request.domain}${request.uri}"))
// HTTPS is required but we can't redirect
case (true, false, _, _) => Action(Results.Forbidden("HTTPS is required"))
// Ok, let's execute this handler
case _ => handler
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment