【发布时间】:2015-12-10 20:52:39
【问题描述】:
我想用这个来保护我的应用程序
def Secured[A](username: String, password: String)(action: Action[A]) = Action(action.parser) {
request => request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.filter {
encoded => new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoded.getBytes)).split(":").toList match {
case u :: p :: Nil if u == username && password == p => true
case _ => false
}
}.map(_ => action(request)) }.getOrElse {
Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Secured"""") } }
但在getOrElse 部分,我收到以下错误:
类型不匹配;找到:所需对象:play.api.mvc.Result
怎么了?
【问题讨论】:
-
查看每个代码块返回的类型。 Scala 推断 Object 因为它们是不同的。