我的喷雾项目中出现了这个错误。
Error:(41, 28) type mismatch;
found : spray.routing.authentication.ContextAuthenticator[co.s4n.authentication.entities.Usuario]
(which expands to) spray.routing.RequestContext => scala.concurrent.Future[scala.util.Either[spray.routing.Rejection,co.s4n.authentication.entities.Usuario]]
required: spray.routing.directives.AuthMagnet[?]
authenticate(validateToken) {
^
这是我的TokenValidator特征
trait TokenValidator {
def validateToken: ContextAuthenticator[Usuario] = {
ctx =>
val header = ctx.request.headers.find(_.name == "Access_Token")
if (header isDefined) {
doAuth(header.get)
}
else {
Future(Left(AuthenticationFailedRejection(AuthenticationFailedRejection.CredentialsMissing, List())))
}
}
def doAuth(header: HttpHeader): Future[Authentication[Usuario]] = {
Dao.validateToken(header.value).map {
case Some(usuario) => Right(usuario)
case None => Left(AuthenticationFailedRejection(AuthenticationFailedRejection.CredentialsRejected, List()))
}
}
}
这就是我收到错误的行
//@DELETE
//localhost:9090/authenticacion/users/{{userEmail}}
val `users/{{email}}` =
pathPrefix(`path-prefix`) {
pathPrefix(`users-path-prefix` / Segment) {
emailRef => {
delete {
authenticate(validateToken) { **HERE!!!!**
usuario =>
.....
}
}
}
}
}
有谁知道我做错了什么?
提前告诉你们所有人!
答案 0 :(得分:11)
我唯一缺少的是在范围内ExecutionContext
并且import ExecutionContext.Implicits.global
工作正常。
让Future
在声明隐式ExecutionContext
参数时起作用。
答案 1 :(得分:0)
我知道自实际问题出现以来已经很长时间了,但是Spray的这种方式是使用Spray提供的工具来确定执行环境:
implicit def executionContext = actorRefFactory.dispatcher