# Handling Forbidden Exception for Request Authorization Middleware

**URL:** <https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708>\
**Category:** Plugins\
**Created:** [October 4, 2019, 1:10pm UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708 "2019-10-04T13:10:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AsDk320](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/asdk320/32/1392_2.png) [@AsDk320](https://discourse.cakephp.org/u/AsDk320)\
**Post date:** [October 4, 2019, 1:10pm UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708/1 "2019-10-04T13:10:34Z")

</div>

So i want to redirect this exception like [this](https://book.cakephp.org/authorization/1.1/en/middleware.html#handling-unauthorized-requests), but it keeps throwing The exception message. Handling the exceptions in the Request middleware is diferent from the Authorization middleware?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 4, 2019, 3:21pm UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708/2 "2019-10-04T15:21:25Z")

</div>

Please show some code. We can’t possibly guess what you’ve tried.

---

<div class="post-metadata">

**Author:** ![AsDk320](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/asdk320/32/1392_2.png) [@AsDk320](https://discourse.cakephp.org/u/AsDk320)\
**Post date:** [October 4, 2019, 4:06pm UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708/3 "2019-10-04T16:06:36Z")

</div>

I just tried to do what’s in the link above… something like this:  
\> $middlewareQueue-\>add(new AuthorizationMiddleware($this, [

> ```
> 'unauthorizedHandler' => [
> 'className' => 'Authorization.Redirect',
> 'url' => Router::url(['controller' => 'Users', 'action' => 'home']),
> 'queryParam' => 'CakeRedirect',
> 'exceptions' => [
> MissingIdentityException::class,
> ForbiddenException::class,
>                         
> ],
> ],
> ]));
> 
> ```

Since it didn’t work. I did the same with

> $middlewareQueue-\>add(new RequestAuthorizationMiddleware());

but i get the same response. It didn’t redirect my exception to my url.  
Neverthless, it works when i make use of this:

> $this-\>Authorization-\>authorize($user, ‘view’);

in my controllers. The problem is with the Request Authorization Middleware.

---

<div class="post-metadata">

**Author:** ![kani](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kani/32/515_2.png) [@kani](https://discourse.cakephp.org/u/kani)\
**Post date:** [April 29, 2020, 2:02am UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708/4 "2020-04-29T02:02:31Z")

</div>

did u found solution ?

---

<div class="post-metadata">

**Author:** ![TFG](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@TFG](https://discourse.cakephp.org/u/TFG)\
**Post date:** [December 13, 2020, 4:56pm UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708/5 "2020-12-13T16:56:07Z")

</div>

Doesn’t work for me either. Using [this example](https://book.cakephp.org/authorization/2/en/middleware.html#handling-unauthorized-requests)  
Added the ForbiddenException to my list of exceptions but it keeps throwing the Exception instead of redirecting to the login page. Anyone got this to work?

---

<div class="post-metadata">

**Author:** ![talw3g](https://avatars.discourse-cdn.com/v4/letter/t/ce7236/32.png) [@talw3g](https://discourse.cakephp.org/u/talw3g)\
**Post date:** [April 19, 2021, 4:36pm UTC](https://discourse.cakephp.org/t/handling-forbidden-exception-for-request-authorization-middleware/6708/6 "2021-04-19T16:36:47Z")

</div>

I was having the same issue, and it worked when I used the full path to ForbiddenException : **\Authorization\Exception\ForbiddenException::class**  
See this [answer](https://stackoverflow.com/a/59977415) on stackoverflow.
