我正在尝试创建一个寻找X-Forwarded-Proto
标头的ScalaInterceptor,所以基本上如果它在生产中或在代理后面然后播放!自动重定向到SSL。
我遇到了使用此代码进行编译的问题,而且我也不确定这是否适用于SecureSocial插件。我们没有在SecureSocial.conf中设置SSL = true的具体原因,我不会在这里讨论。
以下是我Global.scala
def WithHttpsRedirect[A](action: Action[A]): Action[A] = {
Action(action.parser) { request =>
val result = action(request)
request.headers.get("X-Forwarded-Proto").collect {
case "https" =>
result
case "http" =>
val url = "https://"+request.host+request.uri
Redirect(url)
} getOrElse {
result
}
}
}
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
super.onRouteRequest(request).map { handler =>
handler match {
case a: Action[_] => WithHttpsRedirect(a)
case _ => handler
}
}
}
我在getOrElse
:
[error] found : scala.concurrent.Future[play.api.mvc.SimpleResult]
[error] required: play.api.mvc.Result
[error] result
[error] ^
非常感谢您的帮助!
答案 0 :(得分:0)
替换:
Action(action.parser) { request =>
使用:
Action.async(action.parser) { request =>
您还需要替换:
Redirect(url)
使用:
Future.successful(Redirect(url))
答案 1 :(得分:0)
更改了我的攻击方法,而是实施了过滤器,而不是覆盖onRouteRequest
:
在 Global.scala :
object Global extends WithFilters(HttpsFilter) with GlobalSettings
然后 HttpsFilter.scala :
import play.api.mvc.Results._
import play.api.mvc.{SimpleResult, RequestHeader, Filter}
import scala.concurrent._
import ExecutionContext.Implicits.global
object HttpsFilter extends Filter {
def apply(next: (RequestHeader) => Future[SimpleResult])(request: RequestHeader): Future[SimpleResult] = {
request.headers.get("X-Forwarded-Proto").collect {
case "https" =>
next(request)
case "http" =>
val url = "https://"+request.host+request.uri
Future{ Redirect(url) }
} getOrElse {
next(request)
}
}
}