I have a normal FOSUserBundle installed in my Symfony project. What I'm intending to do is provide my admin section further security by making it unknown to the public. What I want to do is throw a 404 error whenever somebody who is not an Admin tries to access the section directly, so nobody even knows at which address it is at, to prevent hacking attempts in the core. I set up an ExceptionListener, which works fine:
public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $templating = $this->container->get('templating'); $response = new Response($templating->render('ScatternoteBundle:Exception:error404.html.twig', array( 'exception' => $exception ))); $event->setResponse($response); } From my error404.html.twig:
{% if 'No route found for' not in exception.message and exception.message != 'Impossible to access an attribute ("album") on a NULL variable ("") in "ScatternoteBundle:Song:song.html.twig" at line 3' and 'Access Denied' not in exception.message%} <span style="font-size:8pt; color:grey;">Not a 404: {{ exception.message }}; Code: {{ exception.code }}</span> <br><br> {% endif %} However, it only works when one is logged in as a user. If I'm not logged in and try to access /admin, I am redirected automatically to /login by the FOSUserBundle. I've done a lot of research, however I couldn't find any information on how to stop this from happening, or where this event is actually handled in the bundle. I would greatly appreciate any help.
EDIT: My security.yaml:
security: # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password encoders: FOS\UserBundle\Model\UserInterface: sha512 # http://symfony.com/doc/current/book/security.html#hierarchical-roles role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers providers: fos_userbundle: id: fos_user.user_provider.username_email # the main part of the security, where you can set up firewalls # for specific sections of your app firewalls: # disables authentication for assets and the profiler, adapt it according to your needs dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false # the login page has to be accessible for everybody demo_login: pattern: ^/demo/secured/login$ security: false main: pattern: ^/ form_login: provider: fos_userbundle csrf_provider: form.csrf_provider logout: true anonymous: true # secures part of the application demo_secured_area: pattern: ^/demo/secured/ # it's important to notice that in this case _demo_security_check and _demo_login # are route names and that they are specified in the AcmeDemoBundle form_login: check_path: _demo_security_check login_path: _demo_login logout: path: _demo_logout target: _demo #anonymous: ~ #http_basic: # realm: "Secured Demo Area" # with these settings you can restrict or allow access for different parts # of your application based on roles, ip, host or methods # http://symfony.com/doc/current/cookbook/security/access_control.html access_control: - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin, role: ROLE_ADMIN }