在 Google App Engine 和 Play Framework 上将 HTTP 请求重定向到 HTTPS
Redirect HTTP requests to HTTPS on Google App Engine and Play Framework
我在 Google App Engine 上使用灵活的环境来 运行 用 Scala 和 Play Framework 编写的网络应用程序。我已将自定义域添加到我的应用程序,现在我的应用程序可通过 http 和 https 使用。但是我需要从 http 重定向到 https。我尝试通过执行以下操作来管理它,但它没有用:
application.conf:
play.http.filters = "controllers.Filters"
controllers.Filters:
import javax.inject.Inject
import play.api.http.DefaultHttpFilters
import play.filters.cors.CORSFilter
import play.filters.https.RedirectHttpsFilter
class Filters @Inject() (corsFilter: CORSFilter, redirectHttpsFilter: RedirectHttpsFilter) extends DefaultHttpFilters(corsFilter, redirectHttpsFilter)
更新
也许问题是我需要在我的 Dockerfile 中指定 https 端口?
这是一个 Dockerfile:
FROM gcr.io/google_appengine/openjdk
RUN wget http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.deb
RUN dpkg -i scala-2.11.8.deb
RUN wget https://dl.bintray.com/sbt/debian/sbt-0.13.13.deb
RUN dpkg -i sbt-0.13.13.deb
RUN apt-get update
RUN apt-get install scala sbt
RUN rm -f scala-2.11.8.deb
RUN rm -f sbt-0.13.13.deb
ADD . /appname
WORKDIR /appname
RUN chmod 755 ./docker-entrypoint.bash
ENTRYPOINT ["./docker-entrypoint.bash"]
CMD ["./target/universal/stage/bin/appname", "-Dhttp.port=8080"]
可能您需要编写另一个小型应用程序,监听 http 端口并向任何传入请求发出位置 header?
以下是我如何实现我想要的:
class Filters @Inject() (corsFilter: CORSFilter, redirectHttpsFilter: HttpsFilter)
extends DefaultHttpFilters(corsFilter, redirectHttpsFilter)
@Singleton
class HttpsFilter extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = EssentialAction { req =>
req.headers.get("X-Forwarded-Proto") match {
case Some("http") =>
//Got request over HTTP
val httpsUrl = s"https://${req.host}"
Accumulator.done(Results.Redirect(httpsUrl))
case _ =>
next(req)
}
}
}
application.conf:
play.http.filters = "controllers.Filters"
我不知道为什么@alex 解决方案对我不起作用。即使在 application.conf
中允许 XForwardedProto
但是,您可以在简单模式下检查 req
是否安全(布尔值),而不是检查此值:
@Singleton
class HttpsFilter extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = EssentialAction { req =>
req.secure match {
case false => {
val httpsUrl= s"https://${req.host}"
Accumulator.done(Results.Redirect(httpsUrl))
}
case true => next(req)
}
}
}
我在 Google App Engine 上使用灵活的环境来 运行 用 Scala 和 Play Framework 编写的网络应用程序。我已将自定义域添加到我的应用程序,现在我的应用程序可通过 http 和 https 使用。但是我需要从 http 重定向到 https。我尝试通过执行以下操作来管理它,但它没有用:
application.conf:
play.http.filters = "controllers.Filters"
controllers.Filters:
import javax.inject.Inject
import play.api.http.DefaultHttpFilters
import play.filters.cors.CORSFilter
import play.filters.https.RedirectHttpsFilter
class Filters @Inject() (corsFilter: CORSFilter, redirectHttpsFilter: RedirectHttpsFilter) extends DefaultHttpFilters(corsFilter, redirectHttpsFilter)
更新 也许问题是我需要在我的 Dockerfile 中指定 https 端口? 这是一个 Dockerfile:
FROM gcr.io/google_appengine/openjdk
RUN wget http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.deb
RUN dpkg -i scala-2.11.8.deb
RUN wget https://dl.bintray.com/sbt/debian/sbt-0.13.13.deb
RUN dpkg -i sbt-0.13.13.deb
RUN apt-get update
RUN apt-get install scala sbt
RUN rm -f scala-2.11.8.deb
RUN rm -f sbt-0.13.13.deb
ADD . /appname
WORKDIR /appname
RUN chmod 755 ./docker-entrypoint.bash
ENTRYPOINT ["./docker-entrypoint.bash"]
CMD ["./target/universal/stage/bin/appname", "-Dhttp.port=8080"]
可能您需要编写另一个小型应用程序,监听 http 端口并向任何传入请求发出位置 header?
以下是我如何实现我想要的:
class Filters @Inject() (corsFilter: CORSFilter, redirectHttpsFilter: HttpsFilter)
extends DefaultHttpFilters(corsFilter, redirectHttpsFilter)
@Singleton
class HttpsFilter extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = EssentialAction { req =>
req.headers.get("X-Forwarded-Proto") match {
case Some("http") =>
//Got request over HTTP
val httpsUrl = s"https://${req.host}"
Accumulator.done(Results.Redirect(httpsUrl))
case _ =>
next(req)
}
}
}
application.conf:
play.http.filters = "controllers.Filters"
我不知道为什么@alex 解决方案对我不起作用。即使在 application.conf
XForwardedProto
但是,您可以在简单模式下检查 req
是否安全(布尔值),而不是检查此值:
@Singleton
class HttpsFilter extends EssentialFilter {
override def apply(next: EssentialAction): EssentialAction = EssentialAction { req =>
req.secure match {
case false => {
val httpsUrl= s"https://${req.host}"
Accumulator.done(Results.Redirect(httpsUrl))
}
case true => next(req)
}
}
}