2012-11-30 67 views
4

當我嘗試在symfony2中登錄時,我總是收到消息:「Bad credentials」。我正在做這個基於http://symfony.com/doc/current/cookbook/security/custom_provider.html。請幫我弄清楚,問題出在哪裏?提前致謝。symfony2自定義用戶提供程序打印「錯誤憑據」

security.yml看起來像這樣

security: 
    encoders: 
     Zags\UserBundle\Security\User\User: plaintext 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

    providers: 
     webservice: 
      id: zags_user_provider 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     login_firewall: 
      pattern: ^/login$ 
      anonymous: ~ 

     secured_area: 
      pattern: ^/ 
      anonymous: ~ 
      form_login: 
       login_path: /login 
       check_path: /login_check 
      #anonymous: ~ 
      #http_basic: 
      # realm: "Secured Demo Area" 

    access_control: 
     - { path: ^/gender_type, roles: ROLE_USER } 
     #- { path: ^/_internal/secure, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 } 

我已經添加了這些線路的routing.yml

login: 
    pattern: /login 
    defaults: { _controller: ZagsUserBundle:Security:login } 
login_check: 
    pattern: /login_check 

user.php的類看起來像這樣

<?php  
namespace Zags\UserBundle\Security\User; 

use Symfony\Component\Security\Core\User\UserInterface; 

class User implements UserInterface 
{ 
    private $username; 
    private $password; 
    private $salt; 
    private $roles; 

    public function __construct($username, $password, $salt, array $roles) 
    { 
     $this->username = $username; 
     $this->password = $password; 
     $this->salt = $salt; 
     $this->roles = $roles; 
    } 

    public function getRoles() 
    { 
     return $this->roles; 
    } 

    public function getPassword() 
    { 
     return $this->password; 
    } 

    public function getSalt() 
    { 
     return $this->salt; 
    } 

    public function getUsername() 
    { 
     return $this->username; 
    } 

    public function eraseCredentials() 
    { 
    } 

    public function equals(UserInterface $user) 
    { 
     return $user->getUsername() === $this->username; 
    } 
} 
?> 

因此,這是我的UserProvider.php類

<?php 

namespace Zags\UserBundle\Security\User; 

use Symfony\Component\Security\Core\User\UserProviderInterface; 
use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; 
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; 

class UserProvider implements UserProviderInterface 
{ 
    public function loadUserByUsername($username) 
    { 
     // make a call to your webservice here 
     $userData = array("username" => "latysh", "password" => "123", "salt" => "123", "roles" => array('ROLE_USER')); 
     // pretend it returns an array on success, false if there is no user 

     if ($userData) { 
      $username = $userData['username']; 
      $password = $userData['password']; 
      $salt = $userData['salt']; 
      $roles = $userData['roles']; 
      // ... 

      return new User($username, $password, $salt, $roles); 
     } 

     throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); 
    } 

    public function refreshUser(UserInterface $user) 
    { 
     if (!$user instanceof User) { 
      throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); 
     } 

     return $this->loadUserByUsername($user->getUsername()); 
    } 

    public function supportsClass($class) 
    { 
     return $class === 'Zags\UserBundle\Security\User\User'; 
    } 
} 
?> 

和services.yml看起來像這樣

parameters: 
    zags_user_provider.class: Zags\UserBundle\Security\User\UserProvider 

services: 
    zags_user_provider: 
     class: "%zags_user_provider.class%" 

SecurityController.php

<?php 
namespace Zags\UserBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Core\SecurityContext; 

class SecurityController extends Controller 
{ 
    public function loginAction() 
    { 
     $request = $this->getRequest(); 
     $session = $request->getSession(); 

     // get the login error if there is one 
     if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); 
     } else { 
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
      $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
     } 

     return $this->render('ZagsUserBundle:Security:login.html.twig', array(
      // last username entered by the user 
      'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
      'error'   => $error, 
     )); 
    } 
} 
?> 

和login.html.twig

{% if error %} 
    <div>{{ error.message }}</div> 
{% endif %} 

<form action="{{ path('login_check') }}" method="post"> 
    <label for="username">Username:</label> 
    <input type="text" id="username" name="_username" value="{{ last_username }}" /> 

    <label for="password">Password:</label> 
    <input type="password" id="password" name="_password" /> 

    {# 
     If you want to control the URL the user is redirected to on success (more details below) 
     <input type="hidden" name="_target_path" value="/account" /> 
    #} 

    <button type="submit">login</button> 
</form> 
+0

你確定你應該用那種方法嗎?密碼是用這種鹽編碼的嗎? – machour

+0

HI machour我沒有用鹽編碼密碼。讓我試試看,非常感謝! –

回答

5

已發現回答我的問題。感謝machour作出迴應。問題出在SALT。所以我更新了User.php類

public function getSalt() 
    { 
     return ''; 
    } 

然後它正確登錄或我應該編碼密碼與鹽成功登錄。如果有人知道如何去做,請將它寫爲答案,YAHOO))

+0

這是Symfony安全使用新手的一個很好的例子。我有一個問題,你通過你的示例代碼清理了我。謝謝。 – Donovan

2
$factory = $this->get('security.encoder_factory'); 
$encoder = $factory->getEncoder($user); 
$pass = $encoder->encodePassword($user->getPassword(), $user->getSalt()); 
相關問題