为什么authenticate指令导致"错误:类型不匹配"?

时间:2014-05-19 20:24:57

标签: spray

我的喷雾项目中出现了这个错误。

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 =>
                  .....
              }
            }
        }
      }
    }

有谁知道我做错了什么?

提前告诉你们所有人!

2 个答案:

答案 0 :(得分:11)

我唯一缺少的是在范围内ExecutionContext并且import ExecutionContext.Implicits.global工作正常。

Future在声明隐式ExecutionContext参数时起作用。

答案 1 :(得分:0)

我知道自实际问题出现以来已经很长时间了,但是Spray的这种方式是使用Spray提供的工具来确定执行环境:

implicit def executionContext = actorRefFactory.dispatcher