Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Scalatra中的before筛选器中使用参数?_Scala_Scalatra - Fatal编程技术网

如何在Scalatra中的before筛选器中使用参数?

如何在Scalatra中的before筛选器中使用参数?,scala,scalatra,Scala,Scalatra,我想在基于scalatra的API中实现一个全局过滤器。为了简单起见,我希望任何具有带值栏的变量foo的API调用都抛出403。我是从继承链开始这个问题的 class NoFooBarRouter extends ScalatraServlet{ before() { if(params.getOrElse("foo", "") == "bar") //params is empty here. halt(403, "You are not al

我想在基于scalatra的API中实现一个全局过滤器。为了简单起见,我希望任何具有带值栏的变量foo的API调用都抛出403。我是从继承链开始这个问题的

class NoFooBarRouter extends ScalatraServlet{
    before() {
        if(params.getOrElse("foo", "") == "bar") //params is empty here. 
            halt(403, "You are not allowed here")
        else
            pass()
    }  
}

class APIRouter extends NoFooBarRouter{
    get("/someurl/:foo") {
        "Hello world!"
    }        
}

这是行不通的。在调试过程中,我注意到params变量总是空的,不管是否有param。是否有更好的方法或其他方法从before过滤器中提取参数

在before方法中,参数没有填写。您可以重写invoke方法

class NoFooBarRouter extends ScalatraServlet{
    override def invoke(matchedRoute: MatchedRoute): Option[Any] = {
        withRouteMultiParams(Some(matchedRoute)){
            val foo = params.getOrElse("foo", "")
            if(foo =="bar")
                halt(403, "You are not authorized for the requested client.")
            else
                NoFooBarRouter.super.invoke(matchedRoute)
        }
    }
}