Play Framework 2.1:Scala:如何获取整个基本 url(包括协议)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14863553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play Framework 2.1: Scala: how to get the whole base url (including protocol)?
提问by Eneko Alonso
Currently I am able to get the host from the request, which includes domain and optional port. Unfortunately, it does not include the protocol (http vs https), so I cannot create absolute urls to the site itself.
目前我能够从请求中获取主机,其中包括域和可选端口。不幸的是,它不包含协议(http 与 https),所以我无法创建站点本身的绝对 url。
object Application extends Controller {
def index = Action { request =>
Ok(request.host + "/some/path") // Returns "localhost:9000/some/path"
}
}
Is there any way to get the protocol from the request object?
有没有办法从请求对象中获取协议?
采纳答案by Farmor
Actually your portnumber will give you if it's http or https.
实际上,您的端口号会告诉您是 http 还是 https。
Start your Play server with https support JAVA_OPTS=-Dhttps.port=9001 play start
使用 https 支持启动您的 Play 服务器 JAVA_OPTS=-Dhttps.port=9001 play start
Here's a code snippet (you can make the validation more stable with a regex, take the https port number from properties ...)
这是一个代码片段(您可以使用正则表达式使验证更加稳定,从属性中获取 https 端口号...)
def path = Action { request =>
val path =
if(request.host.contains(":9000"))
"http://" + request.host + "/some/path"
else
"https://" + request.host + "/some/path"
Ok(path)
}
The code will return
代码将返回
http://ServerIp:9000/some/path if it's thru http
https://ServerIp:9001/some/path if it's thru https
回答by Bruno Batarelo
Actually there's a simple way to do it using Call class that reverse routers use to achieve similar thing. Given that you are within the scope of implicit request, you can do something like this:
实际上,有一种简单的方法可以使用反向路由器用来实现类似功能的 Call 类。鉴于您在隐式请求的范围内,您可以执行以下操作:
new Call(request.method, input.request).absoluteURL()
and it will provide you with the complete url (protocol, host, route and parameters).
它将为您提供完整的 url(协议、主机、路由和参数)。
回答by cdmckay
In Play 2.3 and later you can use the secureproperty of the Requestclass.
回答by EECOLOR
I don't think there is.
我不认为有。
- Play Framework 2.0 itself does not support https, see: play-framework [2.0] HTTPS
- The implementation of
absoluteURLmethod of theCallclass of the Play Framework 2.0 does not suggest it.
- Play Framework 2.0 本身不支持 https,参见:play-framework [2.0] HTTPS
- Play Framework 2.0
absoluteURL的Call类的方法的实现没有建议它。
A workaround is to use a protocol relative urlsusing //domain.com/path.
一种解决方法是使用协议相对 url使用//domain.com/path.
This however does not help you with links in email. In that case you could put the protocol in the application.conf. In most cases the difference is made because production supports https and development does not.
但是,这并不能帮助您处理电子邮件中的链接。在这种情况下,您可以将协议放在application.conf. 在大多数情况下,差异是因为生产支持 https 而开发不支持。
I have yet to find a situation where the above workarounds do not work.
我还没有找到上述解决方法不起作用的情况。
回答by doki42
My solution was to pass the beginning of the url as an additional parameter from javascript. The application.conf solution does not work for me, because the same application is accessible on http and https but from different subnet and domain.
我的解决方案是将 url 的开头作为来自 javascript 的附加参数传递。application.conf 解决方案对我不起作用,因为可以在 http 和 https 上访问相同的应用程序,但可以从不同的子网和域访问。

