如何在Scalatra中的before筛选器中使用参数?
我想在基于scalatra的API中实现一个全局过滤器。为了简单起见,我希望任何具有带值栏的变量foo的API调用都抛出403。我是从继承链开始这个问题的如何在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
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)
}
}
}