在Google App Engine和Play Framework上将HTTP请求重定向到HTTPS

问题描述 投票:7回答:2

我在Google App Engine上使用灵活的环境来运行用Scala和Play Framework编写的Web应用程序。 我已将自定义域添加到我的应用程序中,现在我的应用程序可通过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)

UPD也许问题是我需要在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"]
scala google-app-engine https playframework app-engine-flexible
2个回答
1
投票

也许你必须编写另一个小应用程序,监听http端口并向任何传入请求发出Location头?


0
投票

以下是我实现我想要的方式:

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"
© www.soinside.com 2019 - 2024. All rights reserved.