[英]What is the best way to notify a user after an access_control rule redirects?
来自Symfony 2.3 安全文档:
如果访问被拒绝,系统将尝试验证用户(如果尚未验证)(例如将用户重定向到登录页面) 。 如果用户已经登录,将显示 403“访问被拒绝”错误页面。 有关更多信息,请参阅如何自定义错误页面。
我目前正在为几条路线使用access_control
规则。 如果匿名用户被重定向到登录路由,我想通知匿名用户,并显示“您必须登录才能访问该页面”之类的消息。 我已经通读了几次安全文档,但没有找到与此相关的任何内容。 我是否忽略了什么?
如果没有,只有当他们被重定向到登录时(即如果他们只是在未经授权的角色中),当他们被access_control
规则阻止时通知用户的最佳方式是什么?
编辑:为了澄清起见,我特别询问如何检查重定向是否是由access_control
规则引起的(如果可能,最好在树枝中)。
经过相当多的研究,我发现了正确的方法。 您需要使用入口点服务并在防火墙配置中定义它。
此方法不会破坏防火墙配置中指定的用于登录的默认页面设置。
security.yml:
firewalls:
main:
entry_point: entry_point.user_login #or whatever you name your service
pattern: ^/
form_login:
# ...
SRC /阿克米/ UserBundle /配置/ services.yml
services:
entry_point.user_login:
class: Acme\UserBundle\Service\LoginEntryPoint
arguments: [ @router ] #I am going to use this for URL generation since I will be redirecting in my service
SRC /阿克米/ UserBundle /服务/ LoginEntryPoint.php:
namespace Acme\UserBundle\Service;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface,
Symfony\Component\Security\Core\Exception\AuthenticationException,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\RedirectResponse;
/**
* When the user is not authenticated at all (i.e. when the security context has no token yet),
* the firewall's entry point will be called to start() the authentication process.
*/
class LoginEntryPoint implements AuthenticationEntryPointInterface
{
protected $router;
public function __construct($router)
{
$this->router = $router;
}
/*
* This method receives the current Request object and the exception by which the exception
* listener was triggered.
*
* The method should return a Response object
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$session = $request->getSession();
// I am choosing to set a FlashBag message with my own custom message.
// Alternatively, you could use AuthenticationException's generic message
// by calling $authException->getMessage()
$session->getFlashBag()->add('warning', 'You must be logged in to access that page');
return new RedirectResponse($this->router->generate('login'));
}
}
login.html.twig:
{# bootstrap ready for your convenience ;] #}
{% if app.session.flashbag.has('warning') %}
{% for flashMessage in app.session.flashbag.get('warning') %}
<div class="alert alert-warning">
<button type="button" class="close" data-dismiss="alert">×</button>
{{ flashMessage }}
</div>
{% endfor %}
{% endif %}
我认为kernel.exception
监听器和设置flash消息可以做到这一点。 未经测试的例子:
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class My403ExceptionListener
{
protected $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof AccessDeniedHttpException) {
$this->session->getFlashBag()->set('warning', 'You must login to access that page.');
}
}
}
不知道它是否有效或是否正确。 您可以将其注册为kernel.event_listener
。 或者,您可以更好地使用专用服务并将其设置为防火墙配置中access_denied_handler
的参数。 我认为有很多可能的方法。
你能不能只有两条登录路线?
例如,在安全配置集中
form_login:
login_path: /login_message
在您的登录控制器中
/**
* @Template()
* @Route("/notauthorized", name="login_message")
*/
public function loginMessageAction()
{
return [];
}
然后在你的loginMessage.html.twg中
<a href="{{ path('login') }}">You must login to access this page.</a>
我正在遵循不同的方法,在控制器中我抛出新的AuthenticationException
并在此之前在 flashbag 中添加错误消息。
// Check if user is logged in.
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED') == FALSE) {
$request->getSession()
->getFlashBag()
->add('error', "Please login to continue");
throw new AuthenticationException();
}
并在登录树枝模板中,显示消息。
{% for flash_message in app.session.flashBag.get('error') %}
<div class="callout alert" role="alert">
{{ flash_message }}
</div>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.