[Security] Fix ExceptionListener to catch correctly AccessDeniedException if is not first exception

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9544, #9823
| License       | MIT
| Doc PR        | 

Same as #9823 but with some refactoring of the code and with some unit tests.