2015-11-03 22 views
1

可以使用symfony的@security和REDIRECT_URL

/** 
    * @Security("has_role('ROLE_USER')") 
    */ 
    public function indexAction() 
    { 
     // ... 
    } 

並登錄後確定REDIRECT_URL重定向

這裏是security.yml

firewalls: 
     main: 
      pattern: ^/ 
      form_login: 
       target_path_parameter: redirect_url 

我不能使用引用,因爲它的Ajax請求。

所以現在我使用

if(!$user = $this->getUser()) { 
      return $this->redirect($this->generateUrl('fos_user_security_login', array(
       'redirect_url' => $request->server->get('HTTP_REFERER') 
      ))); 
     } 

有可能在安全註解做到這一點?

回答

1

可以定義登錄成功處理程序:

form_login: 
    success_handler: some.service.id 

創建一個實現\Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface類,使onAuthenticationSuccess()方法返回一個\Symfony\Component\HttpFoundation\RedirectResponse任何你想要的網址。

這樣你可以有任何自定義邏輯來確定你需要的目標URL。

例如,您可以使用\Symfony\Component\HttpFoundation\Request::isXmlHttpRequest()方法確定它是否是Ajax請求並相應地執行操作。