我试图查明 play 2(使用 scala)应用程序是否在 http 或 https 上运行
我尝试使用routes.Application.index.absoluteURL(request),像这样
def chatUri(username: String)(implicit request: RequestHeader): String = {
val uri = routes.Application.index.absoluteURL(request)
但我收到以下错误:
/home/sas/tmp/websocket-chat/app/controllers/Application.scala:51: overloaded method value absoluteURL with alternatives:
[error] (secure: Boolean)(implicit request: play.api.mvc.RequestHeader)java.lang.String <and>
[error] (play.mvc.Http.Request)java.lang.String
[error] cannot be applied to (play.api.mvc.RequestHeader)
[error] val rootUri = Uri(routes.Application.index.absoluteURL(request))
我尝试将 RequestHeader 转换为 Request,但出现以下错误
val rootUri = Uri(routes.Application.index.absoluteURL(request.asInstanceOf[Request[Any]]))
(secure: Boolean)(implicit request: play.api.mvc.RequestHeader)java.lang.String <and>
[error] (play.mvc.Http.Request)java.lang.String
[error] cannot be applied to (play.api.mvc.Request[Any])
[error] val rootUri = Uri(routes.Application.index.absoluteURL(request.asInstanceOf[Request[Any]]))
知道我怎样才能实现它吗?
必须说我对在 Scala 中获取绝对 url 的问题感到惊讶,AFAIR 在 Java 中它工作得很好,无论如何......我怀疑它是否会帮助你确定协议(edit:正如@MariusSoutier 所写)
因为有没有内置的 SSL 支持 https://groups.google.com/forum/?fromgroups=#!topic/play-framework/-7zP0jV5T4U在 Play 2 中,您很可能在应用程序前面使用(或者应该使用)一些 HTTP 服务器,比如 Apache。有一些示例和帖子描述了该过程:
- 看一下题目:如何配置 PlayFramework2 以支持 SSL? https://stackoverflow.com/a/10758612/1066240Nasir 提供了一个将 Apache 配置为 Play 代理的示例
- 还有很好的描述将 Apache 配置为代理 http://www.nczonline.net/blog/2012/08/08/setting-up-apache-as-a-ssl-front-end-for-play/ (warning 帖子描述了 Play 1.x,但 Apache 部分是相同的)
- 最后你需要设置将转发的正确标头 https://serverfault.com/a/257643/118032到您的应用程序
因此,设置标题后(如第 3 点所示),您将能够在控制器中检查它:
def index = Action { request =>
val proto = request.headers("X-FORWARDED-PROTO")
Ok("Got request [" + request + "] with schema: " + proto )
}
或在 Java 控制器中相同:
public static Result index() {
String proto = request().getHeader("X-FORWARDED-PROTO");
return ok("Got request [" + request() + "] with schema: " + proto);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)