无法解决简单身份验证Scala中的类型错误,播放框架项目

时间:2018-11-24 23:47:26

标签: scala playframework

我有以下代码

package controllers

import play.api.mvc._

/**
  * Provide security features
  */
trait Secured {

  /**
    * Retrieve the connected user's email
    */
  private def username(request: RequestHeader) = request.session.get("username")

  /**
    * Not authorized, forward to login
    */
  private def onUnauthorized(request: RequestHeader) = {
    Results.Redirect(routes.Authentication.index)
  }

  /**
    * Action for authenticated users.
    */
  def IsAuthenticated(f: => String => Request[AnyContent] => Result) = {
    Security.Authenticated(username, onUnauthorized) { user =>
      Action(request => f(user)(request))
    }
  }
}

还有

package controllers

import javax.inject.Inject
import play.api.i18n.MessagesApi
import play.api.mvc.Controller
import service.userService
import views._
import scala.concurrent.ExecutionContext.Implicits.global

class Restricted @Inject()
(userService: userService,
 val messagesApi: MessagesApi) extends Controller with Secured {

  /**
    * Display restricted area only if user is logged in.
    */
  def index = IsAuthenticated { user =>
    _ => userService.getUser(username).map {
      Ok(views.html.restricted(user))
    }.getOrElse(Forbidden)
  }

}

但是我无法编译该代码,它在类型不匹配的views.html.restricted(user)上持续失败,应该是:字符串=> Request [AnyContent] =>结果,实际是:String => Request [AnyContent] = >任何。我是scala和游戏的超级新手,经过数小时的谷歌搜索之后,我找不到与我的错误类似的东西。

1 个答案:

答案 0 :(得分:0)

我设法通过将userService.getUser的返回类型从选项[User]更改为User并添加try / catch来处理(如果找不到)来解决此问题。