php 事件订阅者:重定向403访问被拒绝的内核异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 事件订阅者:重定向403访问被拒绝的内核异常相关的知识,希望对你有一定的参考价值。

services:
  mymodule.subscriber:
    class: Drupal\my_module\EventSubscriber\Subscriber
    tags:
      - { name: event_subscriber }
<?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Redirect 403 to User Login event subscriber.
 */
class Subscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   *
   * The priority for the exception must be as low as possible this subscriber
   * to respond with AccessDeniedHttpException.
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::EXCEPTION => 'onKernelException',
    ];
  }

  /**
   * Redirects on 403 Access Denied kernel exceptions.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   Thrown when the access is denied and redirects to user login page.
   */
  public function onKernelException(GetResponseEvent $event) {
    $exception = $event->getException();
    if (!($exception instanceof AccessDeniedHttpException)) {
      return;
    }

    $url = Url::fromRoute('user.login')->toString();
    $response = new RedirectResponse($url);
    $event->setResponse($response);
  }

}

以上是关于php 事件订阅者:重定向403访问被拒绝的内核异常的主要内容,如果未能解决你的问题,请参考以下文章

2017年php面试题汇总

浏览器常见错误代码

登录成功后访问登录页面时出现Spring安全访问被拒绝消息。它应该重定向到默认页面

使用 javaconfig 拒绝访问 Spring Security

Spring Boot:Oauth2:访问被拒绝(用户是匿名的);重定向到身份验证入口点

http常见的状态码,400,401,403状态码 , http响应 ,php设置http响应