2015-05-12 86 views
1

我試圖在symfony2中創建一個認證系統,但沒有結果。在所有情況下,我都有調試狀態欄:記錄爲匿名。symfony 2中的登錄系統

如果我做會議的轉儲在視圖中我得到:

"_security.last_error" => BadCredentialsException 

我的路由文件:

shop_show_login_page: 
    path: /login 
    defaults: { _controller: ShopDesktopBundle:User:loginPage } 

shop_login_user: 
    path: /loginUser 
    defaults: { _controller: ShopDesktopBundle:User:loginCheck } 

shop_logout_user: 
    path: /logout 

我的控制器:

class UserController extends Controller{ 
public function loginPageAction(){ 
    return $this->render('ShopDesktopBundle:User:loginPage.html.twig'); 
} 
public function loginCheckAction(){ 
    $request = $this->getRequest(); 
    $password = $request->request->get('_password'); 
    $login = $request->request->get('_username'); 
    $em = $this->getDoctrine()->getEntityManager(); 
    $repository = $em->getRepository('ShopDesktopBundle:Customer'); 
    $user = $repository->findOneBy(array('customer_login'=> $login, 'customer_password'=> $password)); 
    if($user){ 
     return $this->redirect($this->generateUrl('shop_desktop_homepage')); 
    }else{ 
     return $this->render('ShopDesktopBundle:User:loginPage.html.twig',array('message_failed' => 'Eroare : login sau password este gresit')); 
    } 
} 
} 

我的觀點:

<form action="{{ path('shop_login_user') }}" method="post"> 
       <div class="form-group"> 
        <div class="input-group"> 
         <span class="input-group-addon"><i class="fa fa-user"></i></span> 
         <input type="text" class="form-control" placeholder="Username" name="_username"> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="input-group"> 
         <span class="input-group-addon"><i class="fa fa-lock"></i></span> 
         <input type="text" class="form-control" placeholder="Password" name="_password"> 
        </div> 
       </div> 
       <div class="form-group"> 
        <button type="submit" class="button">Autentificare</button> 
       </div> 
      </form> 

Security.yml:

security: 
# http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password 
encoders: 
    Symfony\Component\Security\Core\User\User: plaintext 

# http://symfony.com/doc/current/book/security.html#hierarchical-roles 
role_hierarchy: 
    ROLE_ADMIN:  ROLE_USER 
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers 
providers: 
    in_memory: 
     memory: 
      users: 
       user: { password: userpass, roles: [ 'ROLE_USER' ] } 
       admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] } 

# 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 
    secured_area: 
     pattern: ^/ 
     form_login: 
      check_path: shop_login_user 
      login_path: shop_show_login_page 
      username_parameter: _username 
      password_parameter: _password 
     logout: 
      invalidate_session: true 
      path: shop_logout_user 
      target:/
     anonymous: true 
     #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, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } 

我花了好幾天,但沒有結果。請幫幫我!!! THX提前

回答

3

我認爲問題是,你有沒有更新您的user providerssecurity.yml你還是由in_memory用戶提供者提供用戶的靜態列表。安全系統不知道你自己的班級ShopDesktopBundle:Customer

如果按照第「How to Create a custom User Provider」的菜譜,你應該能夠解決這個問題:

更新User類

你的用戶類Customer必須實現UserInterface(並建議EquatableInterface

class Customer implements UserInterface, EquatableInterface { […] } 

添加UserProvider

你必須創建一個UserProvider,如:

class CustomerUserProvider implements UserProviderInterface { 
    public function loadUserByUsername($username) { […]} 
    public function refreshUser(UserInterface $user) { […] } 
    public function supportsClass($class) { 
     return $class === 'Shop\DesktopBundle\Customer'; 
    } 
} 

創建服務和更新user_providersecurity.yml

最後在service.yml像創建服務:

services: 
    customer_user_provider: 
     class: Shop\DesktopBundle\CustomerUserProvider 

而且更新security.yml像:

security: 
    providers: 
     webservice: 
      id: customer_user_provider 
+0

所以,這是我很難理解,但THX) – TanGio

+0

@Gigel嘗試按照「[如何創建一個自定義的用戶提供(HTTP:// symfony.com/doc/current/cookbook/security/custom_provider.html)「並將其採用到您的設置中,就像我在答案中所做的一樣。如果這變得複雜了,而且看起來你試圖建立一個商店,那麼安全問題很重要,你可以考慮使用像[FOSUserBundle](https://github.com/FriendsOfSymfony/FOSUserBundle)這樣的完整功能和維護的軟件包。 – insertusernamehere

+1

Thx,它的工作;) – TanGio