【问题标题】:symfony2 No redirect on restricted areassymfony2 限制区域没有重定向
【发布时间】:2012-02-01 18:36:55
【问题描述】:

我的安全文件配置如下:

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 代码,它会重定向到默认的 url /login。如何阻止它重定向?

【问题讨论】:

  • 我想做同样的事情。不知何故,我不太了解 Symfony... 这是一个不寻常的请求写 AJAX 控制器 with 身份验证?

标签: php symfony


【解决方案1】:

您需要创建一个侦听器,然后触发您的响应。我的解决方案基于-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 }

【讨论】:

  • 请允许我为以后的读者添加:- 看看 Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException() 这是默认的 symfony 实现-
  • в " || $exception instanceof AuthenticationCredentialsNotFoundException" нет необходимости, ибо оно наследует "AuthenticationException"
【解决方案2】:

你可以像我一样做: 在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

在routing.yml中

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

只需添加到防火墙部分 *access_denied_url* 参数

【讨论】:

    【解决方案3】:

    请参阅 this page 以获取完整的 security.yml 配置参考。另外,this is an even better reference 附有每个键的说明。

    我建议创建您自己的侦听器类来在用户需要登录时处理返回的 JSON。示例:https://gist.github.com/1015146

    【讨论】:

    • 您发布的链接无效,请更新
    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 2021-11-14
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多