使用Symfony2中的AccessDeniedHandlerInterface [英] Using Symfony2's AccessDeniedHandlerInterface
问题描述
我试图获得Symfony2的我的安全设置的东西,我把它到目前为止的工作,但现在我需要做一些更花哨的东西。我目前正在使用一切处理preAuthentication(我用的是第三方组件登录和会话管理)。这部分是串联与JMS安全捆绑伟大的工作。
现在我给点时,我想赶上那扔403s,所以我可以将它们只是转发给我使用的第三方组件的登录页面的用户。我觉得我最好的选择是一个异常处理程序添加到异常监听器。我在看的<一个href=\"http://api.symfony.com/2.0/Symfony/Component/Security/Http/Authorization/AccessDeniedHandlerInterface.html\">AccessDeniedHandlerInterface.
- 这是正确的方向,我要走向何方?
- 如何这个处理程序添加到异常监听器?
编辑:
最后我做类似的事情。我创建了一个在kernel.exception事件提示服务。 services.yml看起来是这样的:
服务:
kernel.listener.accessDenied:
类:完全\\合格\\命名空间\\路径\\为\\类
标签:
- {名称:kernel.event_listener,事件:kernel.exception,方法:onAccessDeniedException}
和阶级它的自我:
&LT; PHP命名空间完全\\合格\\命名空间\\路径\\要;使用的Symfony \\分量\\ HttpKernel \\事件\\ GetResponseForExceptionEvent,
Symfony的\\分量\\ HttpFoundation \\响应,
Symfony的\\分量\\安全\\核心\\异常\\ AccessDeniedException异常;Class类
{
公共职能onAccessDeniedException(GetResponseForExceptionEvent $事件)
{
$例外= $事件 - &GT; getException();
//获取异常的根本原因。
而(空== $&与异常GT;!获得previous()){
$例外= $&与异常GT;获得previous();
}
如果($例外的instanceof AccessDeniedException异常){
//转发给第三方。
}
}
}
这听起来是正确的。
或者,如果你特别感兴趣的AccessDeniedException异常,你也可以定义 access_denied_handler
在防火墙内的 security.yml
:
安全性:
防火墙:
my_firewall:
#...
access_denied_handler:kernel.listener.access_denied.handler
#...
然后在定义你的服务你的的services.xml
或等值:
&LT;&参数GT;
&LT;参数键=kernel.listener.security.class&GT;路径\\要\\您的\\&类LT; /参数&GT;
&LT; /参数&GT;&LT;服务ID =kernel.listener.access_denied.handler级=%kernel.listener.security.class%&GT;
&LT;标签名称=kernel.event_listener事件=security.kernel_response方法=处理/&GT;
&LT; /服务&GT;
的处理程序类:
使用\\ Symfony的\\分量\\安全\\ HTTP \\授权\\ AccessDeniedHandlerInterface;类MyAccessDeniedHandler实现AccessDeniedHandlerInterface
{
公共职能手柄($申请要求,AccessDeniedException异常$ AccessDeniedException异常)
{
//做一些与你的异常并返回Response对象(渲染模板的普通消息)
}
}
您可以在这里找到的Symfony2的完整的安全参考:的http:// symfony的。 COM / DOC / 2.8 /参考/配置/ security.html安全
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 with PreAuthentication (I use a third party component for logging in and session management). That part is working great in tandem with the JMS security bundle.
Now I am to the point when I want to catch the users that are throwing 403s so I can just forward them to the login page of the third party component that I am using. I think my best bet is to add an exception handler to the exception listener. I am looking at the AccessDeniedHandlerInterface.
- Is this the right direction for me to be going?
- How do I add this handler to the exception listener?
EDIT: I ended up doing something similar. I created a service that is prompted on the kernel.exception event. services.yml looks like this:
services:
kernel.listener.accessDenied:
class: Fully\Qualified\Namespace\Path\To\Class
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onAccessDeniedException }
and the class it self:
<?php
namespace Fully\Qualified\Namespace\Path\To;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\Security\Core\Exception\AccessDeniedException;
class Class
{
public function onAccessDeniedException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
//Get the root cause of the exception.
while (null !== $exception->getPrevious()) {
$exception = $exception->getPrevious();
}
if ($exception instanceof AccessDeniedException) {
//Forward to third-party.
}
}
}
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:
<parameters>
<parameter key="kernel.listener.security.class">Path\To\Your\Class</parameter>
</parameters>
<service id="kernel.listener.access_denied.handler" class="%kernel.listener.security.class%">
<tag name="kernel.event_listener" event="security.kernel_response" method="handle" />
</service>
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
这篇关于使用Symfony2中的AccessDeniedHandlerInterface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!