2012-12-05 3 views
0

나는이 방법이 있습니다왜 Play 2의 특정 메소드에 오류가 발생합니까?

def withAuth(f: => User => Request[AnyContent] => Result) = { 
    Authentication.isAuthenticated(AuthenticationToken(AuthenticationService.TokenKey)) match { 
     case None => Results.Redirect(routes.AuthenticationService.notLoggedIn) 
     case Some(user) => Action(request => f(user)(request)) 
    } 
    } 

을 나는 그것을 좋아 사용

def list(locationId: Option[Int]) = withAuth { user => 
    implicit request => 
     val entities = Assets.filter(user, locationId) 
     Logger.info("Succesfully returned %d assets to user %s".format(entities.length, user)) 
     Ok(Json.toJson(entities.map(s => Json.toJson(s)))) 
    } 

당신은 내가 사용자가 로그인하지 않은 경우, 방법, Redirect들처럼 사용할 발견으로 그 사람 로그인 페이지 그렇지 않으면 세션에서 사용자를 반환합니다. 문제는 런타임에 재생과 불평, 그 리디렉션 함께 :

이 핸들러 등의 방법 반환 객체를 사용할 수 없습니다

사람이 어떤 실마리를 가지고 있습니까?

답변

0

withAuth의 반환 유형은 여러분이 생각하는 것과 다르다고 생각합니다. 리디렉션을 액션으로 포장해야한다고 생각합니다.

def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.AuthenticationService.notLoggedIn) 

    /** 
    * A very important wrapper method which checks if the user is logged-in: if it is, return the User, otherwise 
    * redirect the user to a specific page. 
    */ 
    def withAuthentication(f: => Option[User] => Request[AnyContent] => Result) = { 
    Security.Authenticated(userId, onUnauthorized) { user => 
     Action(request => f(Users.findById(Integer.valueOf(user)))(request)) 
    } 
    } 
:
1

는 결국 내가 좋아 않았다, 위의 문제를 방지하려면