无法解决简单的auth scala中的类型错误,玩框架项目

问题描述 投票:0回答:1

我有以下代码

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)上失败,期望:String => Request [AnyContent] => Result,actual:String => Request [AnyContent] => Any。我是scala和play的新手,经过几个小时的谷歌搜索,我找不到任何类似于我的错误。

scala playframework
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.