scala - 使用请求数据的剪影授权

标签 scala playframework silhouette

我使用 SilhouettePlay 2.4,如果 SecuredRequest 正文包含错误,我想限制操作。

我知道,我应该使用 official docs 描述的特性 Authorization .

我正在尝试执行以下操作:

case class WithCheck(checkCriteria: String) extends Authorization[User, CookieAuthenticator] {
  def isAuthorized[B](user: User, authenticator: CookieAuthenticator)(implicit request: Request[B], messages: Messages) = {

    Future.successful(user.criteria == checkCriteria)
  }
}

def myAction = SecuredAction(WithCheck("bar")) { implicit request =>
  val foo = ...// deserialize object from request.body
  val checkCriteria = foo.criteria 
  // do something else here
}

如何在 WithCheck 类中使用 checkCriteria 值?

最佳答案

我找到了解决方案。

不知何故,我盲目地看到 isAuthorized 具有与隐式参数相同的 request。因此,检查可以完全在 isAuthorized 中完成。例如,

case class WithCheck() extends Authorization[User, CookieAuthenticator] {
  def isAuthorized[B](user: User, authenticator: CookieAuthenticator)(implicit request: Request[B], messages: Messages) = {
    val foo = upickle.read[Foo](request.body.toString())
    Future.successful(user.criteria == foo.criteria)
  }
}

关于scala - 使用请求数据的剪影授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710881/

相关文章:

scala - 在 SBT 中为模块依赖项使用自定义类加载器

scala - 使用 play 进行 akka Actor 测试

java - 无法从结果集中的数据库中获取所有数据

java.lang.ClassNotFoundException : play. db.jpa.JPABase 使用 hibernate

error-handling - 无法导入Silhouette Visualizer-几乎尝试了所有操作

java - Java Optional 和 Scala Option 的区别

string - Scala,函数的字符串表示

scala - 将列表转换为选项(如果已填充)

r - 如何将颜色更改为 fviz_silhouette 中簇号以外的变量

scala - Play 剪影未将密码插入数据库表