Symfony3 security.exception_listener.class不起作用

时间:2016-06-30 11:52:30

标签: php symfony

我试图操纵Symfony安全性中的登录目标路径,因为我的应用程序进行了一些AJAX调用。我跟着this documentation article,但没有任何反应(所以我假设security.exception_listener.class没有"#34;正确地勾选#34}。当我用Google搜索时,我在2015年在github上找到了this issue,声称所提供的文档中的解决方案在Symfony> = 3中无效。

现在我只是想知道 - 有没有人知道链接的文档是否真的过时了(这似乎是因为它对我不起作用),你如何在Symfony中完成同样的事情3?

1 个答案:

答案 0 :(得分:0)

Symfony 2.7的文档已更新为使用编译器传递:https://symfony.com/doc/2.7/security/target_path.html

我想这个更改很快就会流向文档的更高版本。

// src/AppBundle/Security/Firewall/ExceptionListener.php
namespace AppBundle\Security\Firewall;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener;

class ExceptionListener extends BaseExceptionListener
{
    protected function setTargetPath(Request $request)
    {
        // Do not save target path for XHR requests
        // You can add any more logic here you want
        // Note that non-GET requests are already ignored
        if ($request->isXmlHttpRequest()) {
            return;
        }

        parent::setTargetPath($request);
    }
}
// src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php
namespace AppBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use AppBundle\Security\Firewall\ExceptionListener;

class ExceptionListenerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        // Use the name of your firewall for the suffix e.g. 'secured_area'
        $definition = $container->getDefinition('security.exception_listener.secured_area');
        $definition->setClass(ExceptionListener::class);
    }
}
// src/AppBundle/AppBundle.php
namespace AppBundle;

use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass;

class AppBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass(new ExceptionListenerPass());
    }
}