我已根据official security documentation为我的Play Framework(版本2.3.5)应用程序实施了授权:
trait Secured {
def username(request: RequestHeader) = request.session.get(Security.username)
def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Login.index)
def withAuth(f: => String => Request[AnyContent] => Result) = {
Security.Authenticated(username, onUnauthorized) { user =>
Action(request => f(user)(request))
}
}
}
然而,当我用withAuth
函数装饰我的控制器动作时,如下所示:
def export = withAuth { username => implicit request =>
val csv = csv()
Ok(csv)
.as("text/csv")
.withHeaders(
CONTENT_DISPOSITION -> ("attachment; filename=export.csv"),
CONTENT_LENGTH -> csv.length.toString
)
}
我的控制器specs2单元测试中出现编译错误:
"export data as CSV" in {
val controller = new controllers.DataController
val result = controller.export().apply(FakeRequest())
headers(result) must havePair("Content-Disposition" -> ("attachment; filename=export.csv"))
}
对headers
测试帮助程序函数的调用失败,并显示以下编译器错误消息:
类型不匹配;发现:play.api.libs.iteratee.Iteratee [Array [Byte],play.api.mvc.Result]必需:scala.concurrent.Future [play.api.mvc.Result]
我做错了吗?我已尝试过其他Stackoverflow用户的一些remedies suggested,但他们似乎都依赖于设置正文类型,但我的行为没有正文类型。
看起来withAuth
的调用模糊了被包裹的Action的某些类型,所以可能有一种更安全的表达Secured
特征的方式?
答案 0 :(得分:1)
尝试更改此内容:
sqlite_cmd.Parameters.Add("@title",SqlDbType.NVarChar, 30);
进入这个:
val result = controller.export().apply(FakeRequest())
然后它应该有用。