Using Symfony2's AccessDeniedHandlerInterface

前端 未结 1 2058
再見小時候
再見小時候 2020-11-30 05:14

I am trying to get my security stuff setup for symfony2 and I have it working so far, but now I need to do some more fancy things. I am currently using everything dealing wi

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 05:53

    This sounds about right.

    Or, if you're specifically interested in AccessDeniedException you could also define access_denied_handler within your firewall in security.yml:

    security:
        firewalls:
            my_firewall:
                # ...
                access_denied_handler: kernel.listener.access_denied.handler
                # ...
    

    Then define your service in your services.xml or equivalent:

    
        Path\To\Your\Class
    
    
    
        
    
    

    The handler class:

    use \Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
    
    class MyAccessDeniedHandler implements AccessDeniedHandlerInterface
    {
        public function handle(Request $request, AccessDeniedException $accessDeniedException)
        {
            // do something with your exception and return Response object (plain message of rendered template)
        }
    }
    

    You can find complete Security reference of Symfony2 here: http://symfony.com/doc/2.8/reference/configuration/security.html

    0 讨论(0)
提交回复
热议问题