spray - 为什么 authentication 指令会导致 "Error: type mismatch"?

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

有谁知道我做错了什么?

提前谢谢大家!

最佳答案

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

这是为了让 Future s 工作,因为它们声明了一个隐式 ExecutionContext 参数。

关于spray - 为什么 authentication 指令会导致 "Error: type mismatch"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23746603/

相关文章:

scala - 使用 scala akka spray can 对 http 服务器进行非常基本的测试时出错

java - 不支持的密码套件 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256

scala - 代码中看不到Twirl模板(喷雾应用)

performance - 使用什么scala或java库可以以最有效的方式每天从1000个域中抓取+10M页面

java - 使用 spray 服务分层静态内容

scala - 如何使用 Spray-client 设置非标准用户代理?

scala - Akka Http - "Type mismatch"同时将 DateTime 解析为 json

scala - 喷洒死信消息

scala - 资源 url 中的 Swagger (spray) 路径参数

unit-testing - 如何模拟喷雾客户端响应