Symfony2:为什么access_denied_handler不起作用 [英] Symfony2: why access_denied_handler doesn't work

查看:155
本文介绍了Symfony2:为什么access_denied_handler不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在AccessDeniedException的情况下自定义Symfony2的行为。
如果提出异常的HTTP请求是XMLHTTPRequest,那么我用JSON回复,否则我会在登录页面上生成302。

I want to customize the behavior of Symfony2 in case of AccessDeniedException. If the HTTP request which raises the exception is an XMLHTTPRequest then I reply with a JSON otherwise I generate a 302 found to the login page.

这是我的实现。该日志显示AccessDeneidHandler从未在AccessDeniedException后调用。我缺少什么?

Here's my implementation. The log shows that AccessDeneidHandler is never called after an AccessDeniedException. What am I missing ?

#security.yml
firewalls:
    secured_area:
        access_denied_handler: kernel.listener.access_denied.handler





#config.yml
kernel.listener.access_denied.handler:
   class: NoaLisa\Bundle\OVMBundle\DependencyInjection\AccessDeniedHandler
     tags:
        - { name: kernel.event_listener, event: security.kernel_response, method: handle}





#AccessDeniedHandler

class AccessDeniedHandler implements AccessDeniedHandlerInterface{

function handle(Request $request, AccessDeniedException $accessDeniedException){

    if ($request->isXmlHttpRequest()) {
        $response = new Response(json_encode(array('status' => 'protected')));
        return $response;
    }
    else {
        return new RedirectResponse($this->router->generate('login'));
    }
}
}


推荐答案

最后,我发现当我挖掘到ExceptionListener

Ok finally I found out what was the problem when I dig into ExceptionListener

时,问题是access_denied_handler指向的服务只有在用户访问权限不足的情况下被调用资源。如果用户没有进行身份验证access_dened_handler从不被调用。
在security.yml中为entry_point提供服务确实解决了问题

the service pointed by access_denied_handler is only called if the user has unsufficient privilege to access the resource. If the user is not authenticated at all access_dened_handler is never called. Providing a service to entry_point in security.yml did actually solve the problem

这篇关于Symfony2:为什么access_denied_handler不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆