Silex SecurityServiceProivder抛出AccessDeniedException

时间:2015-11-15 16:33:54

标签: php symfony silex

我试图使用Silex和SecurityServiceProvider来集成Symfony的安全组件。我已经设置了基于表单的身份验证,并且日志记录本身运行正常。

在未登录或输入虚假凭据时尝试访问安全路径时出现问题。根据我读过的所有文件,它应该转发到登录表单或向我显示403错误页面。它既没有,只是抛出AccesssDeniedException并退出。我是否必须捕获异常并自己编写重定向部分?我错过了什么?

这是SecurityServiceProvider的设置:

$app->register(new Silex\Provider\SecurityServiceProvider());

$app['security.firewalls'] = array(
    'default' => array(
        'pattern' => '^/.*$',
        'form' => array(
            'login_path' => '/login',
            'check_path' => '/login_check',
        ),
        'anonymous'  => true,
        'logout' => array('logout_path' => '/logout'),
        'users' => array(
            'admin' => array('ROLE_ADMIN', '...'),
        ),
    ),
);
$app['security.access_rules'] = array(
        array('^/dbtest', 'ROLE_ADMIN'),
);

$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new Silex\Provider\SessionServiceProvider());

但是,我尝试根据文档捕获异常,并且结果也没有做任何事情:

$app->error(function (\Symfony\Component\Security\Core\Exception\AccessDeniedException $exception, $code) use ($app) {
    //Redirect things somwehere
    return new \Symfony\Component\HttpFoundation\Response("Access Denied.");
});

错误消息:

2015/11/15 17:14:09 [error] 3124#0: *3 FastCGI sent in stderr: "PHP message: PHP Warning:  Uncaught exception 'Symfony\Component\Security\Core\Exception\AccessDeniedException' with message 'Access Denied.' in /srv/http/workspace/sfad/vendor/symfony/security/Http/Firewall/AccessListener.php:70
Stack trace:
#0 /srv/http/workspace/sfad/vendor/symfony/security/Http/Firewall.php(69): Symfony\Component\Security\Http\Firewall\AccessListener->handle(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))
#1 [internal function]: Symfony\Component\Security\Http\Firewall->onKernelRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#2 /srv/http/workspace/sfad/vendor/symfony/event-dispatcher/EventDispatcher.php(158): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Symfony\Component\EventDispatcher\EventDispatcher))
#3 /srv/http/workspace/sfad/vendor/symfony/event-dispatcher/EventDispatcher.php(46): Symfony\Component\EventDis...
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /srv/http/workspace/sfad/public/index.php:0
PHP message: PHP   2. Silex\Application->run($request = *uninitialized*) /srv/http/workspace/sfad/public/index.php:8
PHP message: PHP   3. Silex\Application->handle($request = class Symfony\Component\HttpFoundation\Request { public $attributes = class Symfony\Component\HttpFoundation\ParameterBag { protected $parameters = array ('_controller' => class Closure { public $static = array ('app' => class Silex\Application { protected $providers = array (0 => class Silex\Provider\MonologServiceProvider {  }, 1 => class Silex\Provider\SecurityServiceProvider { protected $fakeRoutes = array (0 => array (0 => 'get', 1 => '/logout', 2 => 'logout'), 1 => array (0 => 'match', 1 => '/login_check', 2 => 'login_check')) }, 2 => class Silex\Provider\UrlGeneratorServiceProvider {  }, 3 => class Silex\Provider\ServiceControllerServiceProvider {  }, 4 => class 
2015/11/15 17:14:09 [error] 3124#0: *3 FastCGI sent in stderr: "vider { private $app = ... }, 5 => class Silex\Provider\TwigServiceProvider {  }); protected $booted = TRUE; protected $values = array ('logger' => class Closure { public $static = array ('app' => ...); public $this = class Si...
PHP message: PHP   6. Symfony\Component\EventDispatcher\EventDispatcher->dispatch($eventName = 'kernel.request', $event = class Symfony\Component\HttpKernel\Event\GetResponseEvent { private $response = NULL; private ${Symfony\Component\HttpKernel\Event\KernelEvent}:kernel = class Symfony\Component\HttpKernel\HttpKernel { protected $dispatcher = class Symfony\Component\EventDispatcher\EventDispatcher { private $listeners = array ('kernel.request' => array (128 => array (0 => array (0 => class Silex\Provider\SessionServiceProvider { private $app = class Silex\Application { protected $providers = array (0 => class Silex\Provider\MonologServiceProvider {  }, 1 => class Silex\Provider\SecurityServiceProvider { protected $fakeRoutes = array (0 => array (0 => 'get', 1 => '/logout', 2 => 'logout'), 1 => array (0 => 'match', 1 => '/login_check', 2 => 'login_check')) }, 2 => class Silex\Provider\UrlGeneratorServiceProvider {  }, 3 => class Silex\Provider\ServiceControllerServiceProvider {  }, 4 => ..., 5 => class S...
PHP message: PHP   7. Symfony\Component\EventDispatcher\EventDispatcher->doDispatch($listeners = array (0 => array (0 => class Silex\Provider\SessionServiceProvider { private $app = class Silex\Application { protected $providers = array (0 => class Silex\Provider\MonologServiceProvider {  }, 1 => class Silex\Provider\SecurityServiceProvider { protected $fakeRoutes = array (0 => array (0 => 'get', 1 => '/logout', 2 => 'logout'), 1 => array (0 => 'match', 1 => '/login_check', 2 => 'login_check')) }, 2 => class Silex\Provider\UrlGeneratorServiceProvider {  }, 3 => class Silex\Provider\ServiceControllerServiceProvider {  }, 4 => ..., 5 => class Silex\Provider\TwigServiceProvider {  }); protected $booted = TRUE; protected $v
2015/11/15 17:14:12 [error] 3124#0: *3 FastCGI sent in stderr: " 1 => '/login_check', 2 => 'login_check')) }, 2 => class Silex\Provider\UrlGeneratorServiceProvider {  }, 3 => class Silex\Provider\ServiceControllerServiceProvider {  }, 4 => ..., 5 => class Silex\Provider\TwigServiceProvider...
PHP message: PHP  10. Symfony\Component\Security\Http\Firewall\AccessListener->handle($event = class Symfony\Component\HttpKernel\Event\GetResponseEvent { private $response = NULL; private ${Symfony\Component\HttpKernel\Event\KernelEvent}:kernel = class Symfony\Component\HttpKernel\HttpKernel { protected $dispatcher = class Symfony\Component\EventDispatcher\EventDispatcher { private $listeners = array ('kernel.request' => array (128 => array (0 => array (0 => class Silex\Provider\SessionServiceProvider { private $app = class Silex\Application { protected $providers = array (0 => class Silex\Provider\MonologServiceProvider {  }, 1 => class Silex\Provider\SecurityServiceProvider { protected $fakeRoutes = array (0 => array (0 => 'get', 1 => '/logout', 2 => 'logout'), 1 => array (0 => 'match', 1 => '/login_check', 2 => 'login_check')) }, 2 => class Silex\Provider\UrlGeneratorServiceProvider {  }, 3 => class Silex\Provider\ServiceControllerServiceProvider {  }, 4 => ..., 5 => class Silex\Provider\TwigServicePr" while reading response header from upstream, client: 127.0.0.1, server: sfad.stormshard, request: "GET /dbtest HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "sfad.stormshard"

我必须遗漏一些东西,但我对此可能感到茫然。

0 个答案:

没有答案