2012-02-01 29 views
7

我有我的安全文件配置如下:Symfony2中沒有在禁區重定向

security: 
... 
      pattern: ^/[members|admin] 
      form_login: 
       check_path: /members/auth 
       login_path: /public/login 
       failure_forward: false 
       failure_path: null 
      logout: 
       path: /public/logout 
       target:/

目前,如果我訪問成員URL無需驗證它重定向我/public/login,但我不希望它重定向。我主要在我的控制器上使用json進行響應,所以我只想在受限url上顯示警告,例如{"error": "Access denied"}。如果我取出login_path: /public/login代碼,它將重定向到默認的網址/登錄名。我該如何阻止它重定向?

+1

我想要做同樣的事情。不知何故,我不太會得到Symfony ...這是一個不尋常的請求,用**身份驗證來編寫AJAX控制器**嗎? – apfelbox 2012-03-07 14:47:51

回答

0

請參閱this page以獲得完整的security.yml配置參考。此外,this is an even better reference與每個關鍵的解釋。

我建議創建自己的偵聽器類來處理用戶需要登錄時返回的JSON。例如:https://gist.github.com/1015146

+0

您發佈的這些鏈接無效,請更新它們 – 2015-11-13 15:15:51

+1

謝謝@TomášTibenský - 修復。 – leek 2015-11-17 19:10:00

6

你可以像我一樣:在routing.yml中

access_denied: 
    path: /error403 
    defaults : 
     _controller: FrameworkBundle:Template:template 
     template: 'DpUserBundle:Static:error403.html.twig' 

在security.yml

firewalls: 
     administrators: 
      pattern: ^/ 
      form_login: 
       check_path: _security_check 
       login_path: _security_login 
      logout: true 
      security: true 
      anonymous: true 
      access_denied_url: access_denied 

只需添加到防火牆的部分* access_denied_url * PARAM

9

您需要創建一個監聽器,然後觸發您的響應。我的解決方案是基於 - https://gist.github.com/xanf/1015146

監聽器代碼 -

namespace Your\NameSpace\Bundle\Listener; 

use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 

class AjaxAuthenticationListener 
{ 

/** 
* Handles security related exceptions. 
* 
* @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance 
*/ 
public function onCoreException(GetResponseForExceptionEvent $event) 
{ 
    $exception = $event->getException(); 
    $request = $event->getRequest(); 

    if ($request->isXmlHttpRequest()) { 
     if ($exception instanceof AuthenticationException || $exception instanceof AccessDeniedException || $exception instanceof AuthenticationCredentialsNotFoundException) { 
      $responseData = array('status' => 401, 'msg' => 'User Not Authenticated'); 
      $response = new JsonResponse(); 
      $response->setData($responseData); 
      $response->setStatusCode($responseData['status']); 
      $event->setResponse($response); 
     } 
    } 
} 
} 

您需要創建監聽服務 -

e_ent_int_baems.ajaxauthlistener: 
    class: Your\NameSpace\Bundle\Listener\AjaxAuthenticationListener 
    tags: 
     - { name: kernel.event_listener, event: kernel.exception, method: onCoreException, priority: 1000 } 
+1

允許我爲未來的讀者添加: - 看看Symfony \ Component \ Security \ Http \ Firewall \ ExceptionListener :: onKernelException()這是默認的symfony實現 - – 2013-12-15 00:07:18

+0

謝謝!這正是我需要的:) – 2015-09-09 12:45:58

+0

0x05 「|| $ exception instanceof AuthenticationCredentialsNotFoundException」 「 – bfday 2017-08-11 14:26:42