路由到静态时出错 - Play Framework

时间:2014-12-25 18:51:36

标签: scala playframework routes

所以我试图实现文件访问限制。 例如,当获得" flowers.jpg" URL看起来像这样:... / static / uploads / zfds98ys / flowers.jpg

路由文件:

GET /static/uploads/*hashCode/*fileName     controllers.Application.staticAccess(hashCode: String, fileName: String)

通过功能:

def staticAccess(hashCode: String, fileName: String) = Action { implicit request =>
    if (true /*insert hash check here...*/ ) Redirect(routes.Assets.at("/public/uploads", fileName))
    else Redirect(routes.Application.notFound())
}

我收到此错误:

  Execution exception
  [MatchError: (/public/uploads,flowers.jpg) (of class scala.Tuple2)]

  123 // @LINE:12
  124 def at(path:String, file:String): Call = {
  125    (path: @unchecked, file: @unchecked) match {
  126 // @LINE:12
  127 case (path, file) if path == "/public/javascripts" =>
  128   implicit val _rrc = new ReverseRouteContext(Map(("path", "/public/javascripts")))
  129   Call("GET", _prefix + { _defaultPrefix } + "static/javascripts/" +  implicitly[PathBindable[String]].unbind("file", file))Execution exception

为什么会发生这种情况......? 图像存在于给定路径中。我替换的代码工作得很好(下面):

GET /static/uploads/*fileName    controllers.Assets.at(path="/public/uploads", fileName)

1 个答案:

答案 0 :(得分:0)

这种方法可以通过删除原始Assets路由来实现,因为在这样做时,您不能Redirect到不存在的路由。但如果你有原始Assets路线,那么有人可以绕过它。您可以通过直接调用Assets控制器而不是重定向来完成此工作。

路线:

GET     /static/uploads/:hashCode/*fileName     controllers.Application.staticAccess(hashCode: String, fileName: String)

控制器功能:

def staticAccess(hashCode: String, fileName: String) = Action.async { implicit request =>
    if (true /*insert hash check here...*/ ) {
        controllers.Assets.at("/public/uploads", fileName)(request)
    }
    else Future.successful(NotFound)
}
相关问题