2015-02-06 113 views
1

我有一個正常的FOSUserBundle安裝在我的Symfony項目。 我打算做的事情是讓我的管理部分進一步提高安全性,讓公衆不知道。我想要做的就是當有人不是管理員直接訪問這個部分時,拋出一個404錯誤,所以沒有人知道它在哪個地址,以防止核心中的黑客攻擊。 我建立了一個的ExceptionListener,它工作得很好:FOSUserbundle:重定向訪問管理部分

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); 
} 

從我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 %} 

然而,當一個人在記錄爲用戶它纔會起作用。如果我沒有登錄並嘗試訪問/ admin,我將自動重定向到/通過FOSUserBundle登錄。我做了很多研究,但是我找不到任何有關如何阻止這種情況發生的信息,或者實際上在捆綁中處理了這個事件的信息。 我將不勝感激任何幫助。

編輯:我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 } 
+1

我只能猜測你已經設置在security.yaml firewals,這些防火牆重定向未授權的用戶登錄形式 – szapio 2015-02-06 09:54:29

+0

謝謝,但我避風港在我的security.yaml中不設置任何防火牆,它只包含默認的防火牆。 – c42 2015-02-06 09:57:11

回答

1

登錄頁面有一個公共接入,這樣是正常的,爲什麼FOSUser重定向用戶登錄形式(爲了訪問受保護的區域)。當用戶訪問一個安全區域時,一個403異常(訪問被拒絕)被拋出(而不是404未找到)。 我想你應該重寫FOSUser的寄存器行動在這個RegistrationController:

if ($user->hasRole('ROLE_ADMIN')) { 
    $this->authenticateUser($user, $response); 
} else { 
throw new AccessDeniedException ('Oups !!! Access denied ') ; 
} 
+0

對不起,我不太確定你的目標是什麼。我想停止FOSUser將我重定向到登錄頁面,如果我嘗試訪問安全區域並且沒有登錄,那麼未記錄的用戶將被視爲與登錄的用戶相同。我不明白這與註冊有什麼關係。 – c42 2015-02-06 13:07:01

+0

這就是我說覆蓋控制器時,用戶重定向您登錄頁面,並將其重定向到另一個頁面或拋出axception – 2015-02-06 14:25:54

+0

但它在哪裏重定向我從/ admin到/登錄?當然不在RegistrationController中。如果我錯了,請告訴我在哪裏,因爲那時我顯然太笨了...... – c42 2015-02-06 14:34:12