Url play framework2:查看应用程序是否在http或https上运行
我试图找出Play2(带有scala)应用程序是否在http或https上运行 我尝试了routes.Application.index.absoluteURL(请求),如下所示Url play framework2:查看应用程序是否在http或https上运行,url,playframework-2.0,router,Url,Playframework 2.0,Router,我试图找出Play2(带有scala)应用程序是否在http或https上运行 我尝试了routes.Application.index.absoluteURL(请求),如下所示 def chatUri(username: String)(implicit request: RequestHeader): String = { val uri = routes.Application.index.absoluteURL(request) 但我得到了以下错误: /home/sas/tmp/
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))
/home/sas/tmp/websocket chat/app/controllers/Application.scala:51:重载方法值absoluteURL及其他选项:
[错误](安全:布尔)(隐式请求:play.api.mvc.RequestHeader)java.lang.String
[错误](play.mvc.Http.Request)java.lang.String
[错误]无法应用于(play.api.mvc.RequestHeader)
[错误]val rootUri=Uri(routes.Application.index.absoluteURL(请求))
我试图将RequestHeader转换为请求,但出现以下错误
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]]))
val rootUri=Uri(routes.Application.index.absoluteURL(request.asInstanceOf[request[Any]]))
(安全:布尔)(隐式请求:play.api.mvc.RequestHeader)java.lang.String
[错误](play.mvc.Http.Request)java.lang.String
[错误]无法应用于(play.api.mvc.Request[Any])
[错误]val rootUri=Uri(routes.Application.index.absoluteURL(request.asInstanceOf[request[Any]]))
你知道如何实现它吗?首先,通过创建一个绝对URL,你无法确定应用程序是否在http或https上运行-请查看方法签名:
def absoluteURL (secure: Boolean = false)(implicit request: RequestHeader): String
没错,你必须告诉这个方法你是否想要安全
我认为这是因为Play设计用于反向代理,这使得使用加密请求变得透明。这意味着游戏不应该关心这个absoluteURL
只能强制使用https URL,例如确保登录页面使用https
根据您的反向代理,您可以设置一个自定义http头,告诉您使用了什么
RequestHeader
没有相关信息。必须说,我对Scala中获取绝对url的问题感到惊讶,而Java中的AFAIR它工作得很好,不管怎样。。。我怀疑它是否能帮助您确定协议(编辑:正如@MariusSoutier所写)
正如重头戏2中所说,您很可能正在(或应该)使用应用程序前面的某个HTTP服务器,比如Apache。有一些示例和帖子描述了该过程:
def index = Action { request =>
val proto = request.headers("X-FORWARDED-PROTO")
Ok("Got request [" + request + "] with schema: " + proto )
}
public static Result index() {
String proto = request().getHeader("X-FORWARDED-PROTO");
return ok("Got request [" + request() + "] with schema: " + proto);
}
或Java控制器中的相同:
def index = Action { request =>
val proto = request.headers("X-FORWARDED-PROTO")
Ok("Got request [" + request + "] with schema: " + proto )
}
public static Result index() {
String proto = request().getHeader("X-FORWARDED-PROTO");
return ok("Got request [" + request() + "] with schema: " + proto);
}
请允许我分享一篇关于处理这件事的解决办法的好文章: