【问题标题】:Why does authenticate directive lead to "Error: type mismatch"?为什么身份验证指令会导致“错误:类型不匹配”?
【发布时间】:2014-05-19 20:24:56
【问题描述】:

我的喷涂项目出现此错误。

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

有谁知道我做错了什么?

提前谢谢大家!

【问题讨论】:

    标签: spray


    【解决方案1】:

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

    这是为了让Futures 工作,因为他们声明了一个隐含的ExecutionContext 参数。

    【讨论】:

    • 谢谢,节省了我一些时间)
    【解决方案2】:

    我知道距离真正的问题已经过去了很长时间,但是对于 Spray 来说,解决这个问题的方法是使用 Spray 提供的工具来确定执行上下文:

    implicit def executionContext = actorRefFactory.dispatcher
    

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多