2014-12-30 3 views
2

저는 Scala 2.11.2, Akka 2.3.6 및 Spray 1.3.2를 사용하고 있습니다.권한 지정 지시문이 보호 대상 코드 뒤에 실행되는 이유는 무엇입니까?

나는 authorize 지시어에 관한 문제에 직면하고 있습니다. 다음은 코드의 흥미로운 부분입니다.

val authenticatorActor = context.actorOf(Props[AuthenticatorActor]) 
implicit val timeout = Timeout(5 seconds) 

cookie("userName") { cookie => 
    def optionUser = Await.result(authenticatorActor ? cookie.content, timeout.duration).asInstanceOf[Option[User]] 
    authorize(isAuthorized(optionUser)) { // ????? 
    val user = optionUser.get 
    //do stuff 
    } 
} 

def isAuthorized(user: Option[User]): Boolean = 
    user match { 
    case Some[User] => true 
    case None  => false 
    } 

기본적으로 쿠키를 검사하여 사용자 자격 증명의 유효성을 검사합니다.

isAuthorize 메서드 전에 authorize 지시문 내의 블록이 실행되는 것이 문제입니다.

미래가 None을 반환하는 경우 코드는 의 코드가 NonSuchElementException으로 실패합니다.

authorize 지침은 모든 작업의 ​​미세 아래의 코드에서 같은 if 문에 의해 변경되는 경우 :

if (isAuthorized(optionUser)) { 
    //do stuff 
} else reject(ValidationRejection("User has not access")) 

어떤 생각?

UPDATE

나는 answere에 대한 참조 Gangstead에

get { 
    path("") { 
    complete { 
     s"Hi ${user.name}. You have the next access: ${user.acceso}.\nWelcome to the ping-pong match" 
    } 
    } ~ 
    path("ping") { 
    complete("pong") 
    } ~ 
    path("pong") { 
    complete ("ping") 
    } 
} 
+3

는'complete' 블록의'// 할 stuff' 코드인가? [이것은 일어날 수 있습니다] (http://spray.io/documentation/1.2.2/spray-routing/advanced-topics/understanding-dsl-structure/#understanding-extractions). 그것은 내가 여러 번 재 학습해야만했던 것입니다. – Gangstead

+0

답변을 주셔서 감사합니다. 나는 물건에있는 내용을 질문에 추가하고, 더 이상은 아니다. 끝에는 전체 블록이있는 get/path() 계층 구조가 포함되어 있습니다. 나는 이것이 괜찮을 것이라고 생각한다. 덧붙여서 +1은 문서에 대한 링크 때문에 항상 잊어 버린다. – RoberMP

+0

이것은 약간의 수정 (예 :'isAuthorized'의 패턴 매치, 실제로 컴파일되지 않음)으로 나를 위해 일하고있다. 언급 한 예외와 'AuthenticatorActor'도 게시 할 수 있습니까? 어떤 스프레이 버전을 사용하고 있습니까? – edi

답변

2

감사하고 user3567830의 //do stuff 블록을 추가 해요. 워드 프로세서

http://spray.io/documentation/1.2.2/spray-routing/advanced-topics/understanding-dsl-structure/#understanding-extractions

올바른 방법으로

상태로

은 다음과 같이이다 :

authorize(isAuthorize(optionUser)) { 
     get { 
      path("") { 
      complete { 
       val user = optionUser.get 
       s"Hi ${user.name}. You have the next access: ${user.acceso}.\nWellcome to the ping-pong match" 
      } 
      } 
     } 
      ....