2012-04-18 67 views
6

With Play!框架2.0,使用Security Trait在玩! 2.0 with Security trait如何在登錄後重定向到原始URL?

如果我讓用戶瀏覽到未經身份驗證的站點的幾個部分,但他們需要進行身份驗證的某些操作,我如何在身份驗證之前將它們重定向到其原始URL,而不是相同的URL所有?

這對Play這個問題有類似的要求! 1.x Playframework's Secure module is not redirecting to the original url after login

但據我所知,原始URL的flash參數在2.0中不可用。

Basicaly我正在尋找將在身份驗證方法處理

def authenticate = Action { implicit request => 
    loginForm.bindFromRequest.fold(
     formWithErrors => BadRequest(html.login(formWithErrors)), 
     user => Redirect(routes.Application.index).withSession(Security.username -> user._1) 
    ) 
    } 

當某種重定向(originalRequestUrl)將是得心應手的變化。

乾淨解決方案的任何想法?

回答

11

可以使用Referer頭,或明確攜帶返回URL在authenticate行動:

使用Referer

def authenticate = Action { implicit request => 
    loginForm.bindFromRequest.fold(
    errors => BadRequest(html.login(errors)), 
    user => { 
     val returnUrl = request.headers.get(REFERER).getOrElse(routes.Application.index.url) 
     Redirect(returnUrl).withSession(Security.username -> user._1) 
    } 
} 

明確攜帶的返回URL

def authenticate(returnUrl: String) = Action { implicit request => 
    loginForm.bindFromRequest.fold(
    errors => BadRequest(html.login(errors, returnUrl)), 
    user => Redirect(returnUrl).withSession(Security.username -> user._1) 
) 
} 

通過使用Referer HTTP標頭,您直到必須處理瀏覽器未填充此標題的情況,並且通過明確地將返回url作爲您的authenticate操作的參數進行處理,您可能會在代碼處理中使用身份驗證更多的樣板文件...

相關問題