2014-12-25 34 views
1

所以我试图实现文件访问限制。 例如,获取 “flowers.jpg” 当URL看起来是这样的:... /静态/上传/ 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) 
+0

你从你的'routes'文件中删除正常'Assets'路线? –

+0

是的。我认为问题是'重定向(routes.Assets.at(“/ public/uploads”,fileName))'不够用(需要声明一个路由)。我决定现在用另一种方法 – user3723987

+1

对。当路由不存在时,您不能使用反向路由重定向。 –

回答

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) 
} 
+0

当然,删除“资产”路线仍然会伤害应用程序的其余部分。 –