我正在尝试做一些与此非常相似的事情。ContextRequest
和ContextAction
。我有一个securedRequest
来自Silhouette
,我想撰写一个我设置的Action
阅读。cookie
不同的是我扩展了User
from SilhouetteIdentity
DataSourceRequest
class DataSourceRequest[DefaultEnv, B](
val dataSource: Option[String],
val securedRequest: SecuredRequest[DefaultEnv, B]
) extends WrappedRequest[B](securedRequest.request) {
def user: User = securedRequest.identity
def requestV: Request[B] = securedRequest.request
}
DataSourceAction
class DataSourceAction @Inject()
(val parser: BodyParsers.Default)(implicit val executionContext: ExecutionContext)
extends ActionRefiner[({ type R[B] = SecuredRequest[DefaultEnv, B] })#R, ({ type R[B] = DataSourceRequest[DefaultEnv, B] })#R] {
override protected def refine[A](securedRequest: SecuredRequest[DefaultEnv, A]) = Future.successful {
try {
Right(new DataSourceRequest[DefaultEnv, A](
securedRequest.session.get("dataSource").getOrElse("dataSourceMissing"),
securedRequest
))
} catch {
case _: Exception => Left(Results.NotFound)
}
}
}
DefaultEnv
trait DefaultEnv extends Env {
type I = User
type A = CookieAuthenticator
}
我无法得到User
因为
(注意 com.mohiva.play.silhouette.api.Identity 不匹配 models.User:包实体中的类 User 是包 api 中 trait Identity 的子类,但方法参数类型必须完全匹配。)
我知道我可能缺少一个包装器。