脚本宝典收集整理的这篇文章主要介绍了php – symfony2在限制区域没有重定向,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
security: ... pattern: ^/[members|admin] form_login: check_path: /members/auth LOGin_path: /public/login failure_forward: false failure_path: null logout: path: /public/logout target: /
目前,如果我访问成员网址而不进行身份验证,它会将我重定向到/ public / login但我不希望它重定向.我主要是在控制器上使用json进行响应,所以我只想在受限制的URL上显示警告,例如{“error”:“Access denied”}.如果我取出login_path:/ public / login代码,它会重定向到默认的url / login.如何阻止它重定向?
听众代码 –
namespace Your\NameSpace\Bundle\Listener; use Symfony\component\HttpFoundation\JsonResponse; use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AcceSSDeniedException; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; class AjaxAuthenticationListener { /** * Handles security related exceptions. * * @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance */ public function onCoreException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); if ($request->isXMlHttPRequest()) { if ($exception instanceof AuthenticationException || $exception instanceof AccessDeniedException || $exception instanceof AuthenticationCredentialsNotFoundException) { $responseData = array('status' => 401,'msg' => 'User Not Authenticated'); $response = new JsonResponse(); $response->setData($responseData); $response->setStatusCode($responseData['status']); $event->setResponse($response); } } } }
您需要为侦听器创建服务 –
e_ent_int_BAEms.ajaxauthlistener: class: Your\NameSpace\Bundle\Listener\AjaxAuthenticationListener tags: - { name: kernel.event_listener,event: kernel.exception,method: onCoreException,priorITy: 1000 }
以上是脚本宝典为你收集整理的php – symfony2在限制区域没有重定向全部内容,希望文章能够帮你解决php – symfony2在限制区域没有重定向所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。