我已经实现了身份验证操作,并将其放在我控制器中每个方法的开头
def addFile(itemId: Long) = AuthenticatedAction.async(FSBodyParser(itemId)){ request => ...
然后我基于MultipartFormData
实现了自己的身体解析器 def FSBodyParser(itemId:Long): BodyParser[MultipartFormData[Future[BaseFileInfo]]] = {
multipartFormData(Multipart.handleFilePart {
case Multipart.FileInfo(partName, filename, contentType) =>
//println(s"FileInfo($partName, $filename, $contentType)")
getIteratee(1, itemId, filename, contentType)
})
}
我发现我的文件首先上传,然后验证操作检查用户是否有效。我想检查用户身份验证拳头,然后才保存上传的文件。
您是否有任何想法如何以最佳方式实施?可能使用Play过滤器进行身份验证?
答案 0 :(得分:0)
我已将其实现如下(使用EssentialAction)
def authenticate(): Future[Option[Long]] = {
// here we should pass correct user id
Future.successful(Some(421))
}
def Authenticated[A](action: Action[A]) = EssentialAction{ rh =>
Iteratee.flatten(authenticate() map{
case Some(userId) => action(rh)
case None => Done(Results.Unauthorized)
})
}
//this is method of my service
//Authenticated is called before FSBodyParser starts working
def addFile(itemId: Long) = Authenticated{
Action.async(FSBodyParser(itemId)){ requestHeaders =>
ItemRepository.addFile(1, itemId, null) map( fileId => Ok)
}
}