我正在尝试在 symfony3 中对密码进行编码,但我遇到了一些问题。
这是我得到的错误。



我正在与 guard 一起进行身份验证。
这是认证类名 loginformauthentificator。

namespace GMAOBundle\Security;


use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;



class LoginFormAuthentificator extends  AbstractGuardAuthenticator
{
private $em;
private $router;
private $passwordEncoder;


public function __construct($em, $router,UserPasswordEncoder  $PasswordEncoder )
{
    $this->em = $em;
    $this->router = $router;
    //$this->passwordEncoder = new UserPasswordEncoder();
    $this->passwordEncoder = $PasswordEncoder;
}


public function getCredentials(Request $request)
{
    if ($request->getPathInfo() != '/login' || !$request->isMethod('POST')) {
        return;
    }
    $request->getSession()->set(Security::LAST_USERNAME,$request->request->get('_username'));

    return [
        'username' => $request->request->get('_username'),
        'password' => $request->request->get('_password'),
    ];
}


public function getUser($credentials, UserProviderInterface $userProvider)
{
    $username = $credentials['username'];

    return $this->em->getRepository('GMAOBundle:Employe')
        ->findOneBy(['EmailEmploye' => $username]);
}

/**
 * @param mixed $credentials
 * @param UserInterface $user
 * @return bool
 */
public function checkCredentials($credentials, UserInterface $user)
{

    $password=$credentials['password'];
    if ($this->passwordEncoder->isPasswordValid($user, $password)) {
        return true;
    }
    return false;
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
    $url = $this->router->generate('login');
    return new RedirectResponse($url);
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    $url = $this->router->generate('gmao_homepage');
    return new RedirectResponse($url);
}

public function supportsRememberMe()
{
    return true;
}

public function start(Request $request, AuthenticationException $authException = null)
{
    $url = $this->router->generate('login');
    return new RedirectResponse($url);
}


}

这是用户类。

  namespace GMAOBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;


  /**
  * @ORM\Entity
  * @ORM\Table(name="employe")
  */
  class Employe implements UserInterface
  {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */private $idEmploye;
/**
 * @ORM\Column(type="string")
 */private $NomEmploye;
/**
 * @ORM\Column(type="string")
 */private $PrenomEmploye;
/**
 * @ORM\Column(type="date", nullable=true)
 */private $DateNaissanceEmploye;
/**
 * @ORM\Column(type="string")
 */private $cinEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $AdresseEmploye;
/**
 * @ORM\Column(type="string")
 */private $MatriculeEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $SituationFamiliale;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $SexeEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $CnssEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $FonctionEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $NumeroMutuelle;
/**
 * @ORM\Column(type="date", nullable=true)
 */private $DateDebutEmploye;
/**
 * @ORM\Column(type="date", nullable=true)
 */private $DateTitularisationEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $NumeroTelEmploye;
/**
 * @ORM\Column(type="string", nullable=true)
 */private $NumeroLigneEmploye;
/**
 * @ORM\Column(type="string")
 */private $EmailEmploye;

/**
 * A non-persisted field that's used to create the encoded password.
 * @var string
 */private $plainPassword;

/**
 * @ORM\Column(type="string")
 */private $MdpEmploye;

/**
 * @ORM\Column(type="string")
 */private $active;



/**
 * @ORM\Column(type="json_array")
 */private $roles = [];

/**
 * @ORM\ManyToOne(targetEntity="Departement")
 * @ORM\JoinColumn(name="Departement",
   referencedColumnName="id_departement")
 */private $Departement;

/**
 * @ORM\ManyToOne(targetEntity="Equipe", cascade={"persist"})
 * @ORM\JoinColumn(name="Equipe", referencedColumnName="id" ,nullable=true)
 */private $equipe;

/**
 * @ORM\Column(type="string", nullable=true)
 */private $imgsrc;
 /**
 * @ORM\Column(type="string")
 *private $responsable_projet;
/**
 * @ORM\Column(type="string")
 *private $m_GMAO_Projet;
/**
 * @ORM\Column(type="string")
 *public $m_PMP_Objectif;
/**
 * @ORM\Column(type="string")
 *public $m_Services;*/



/**
 * User Interface Methode Override
 */

function __construct()
{
}

function __destruct()
{
}

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

public function getRoles()
{
    $roles = $this->roles;
        if(!in_array('ROLE_USER',$roles))
        {
            $roles[]='ROLE_USER';
        }

    return $roles;
}

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

public function getSalt()
{
}

public function eraseCredentials()
{
    $this->plainPassword = null;
}




/**
 * GETTERS AND SETTERS
 **/


/**
 * @return mixed
 */
public function getIdEmploye()
{
    return $this->idEmploye;
}

/**
 * @param mixed $idEmploye
 */
public function setIdEmploye($idEmploye)
{
    $this->idEmploye = $idEmploye;
}

/**
 * @return mixed
 */
public function getNomEmploye()
{
    return $this->NomEmploye;
}

/**
 * @param mixed $NomEmploye
 */
public function setNomEmploye($NomEmploye)
{
    $this->NomEmploye = $NomEmploye;
}

/**
 * @return mixed
 */
public function getPrenomEmploye()
{
    return $this->PrenomEmploye;
}

/**
 * @param mixed $PrenomEmploye
 */
public function setPrenomEmploye($PrenomEmploye)
{
    $this->PrenomEmploye = $PrenomEmploye;
}

/**
 * @return mixed
 */
public function getDateNaissanceEmploye()
{
    return $this->DateNaissanceEmploye;
}

/**
 * @param mixed $DateNaissanceEmploye
 */
public function setDateNaissanceEmploye($DateNaissanceEmploye)
{
    $this->DateNaissanceEmploye = $DateNaissanceEmploye;
}

/**
 * @return mixed
 */
public function getcinEmploye()
{
    return $this->cinEmploye;
}

/**
 * @param mixed $cinemploye
 */
public function setcinEmploye($Cinemploye)
{
    $this->cinEmploye = $Cinemploye;
}

/**
 * @return mixed
 */
public function getAdresseEmploye()
{
    return $this->AdresseEmploye;
}

/**
 * @param mixed $AdresseEmploye
 */
public function setAdresseEmploye($AdresseEmploye)
{
    $this->AdresseEmploye = $AdresseEmploye;
}

/**
 * @return mixed
 */
public function getMatriculeEmploye()
{
    return $this->MatriculeEmploye;
}

/**
 * @param mixed $MatriculeEmploye
 */
public function setMatriculeEmploye($MatriculeEmploye)
{
    $this->MatriculeEmploye = $MatriculeEmploye;
}

/**
 * @return mixed
 */
public function getSituationFamiliale()
{
    return $this->SituationFamiliale;
}

/**
 * @param mixed $SituationFamiliale
 */
public function setSituationFamiliale($SituationFamiliale)
{
    $this->SituationFamiliale = $SituationFamiliale;
}

/**
 * @return mixed
 */
public function getSexeEmploye()
{
    return $this->SexeEmploye;
}

/**
 * @param mixed $SexeEmploye
 */
public function setSexeEmploye($SexeEmploye)
{
    $this->SexeEmploye = $SexeEmploye;
}

/**
 * @return mixed
 */
public function getCnssEmploye()
{
    return $this->CnssEmploye;
}

/**
 * @param mixed $CnssEmploye
 */
public function setCnssEmploye($CnssEmploye)
{
    $this->CnssEmploye = $CnssEmploye;
}

/**
 * @return mixed
 */
public function getFonctionEmploye()
{
    return $this->FonctionEmploye;
}

/**
 * @param mixed $FonctionEmploye
 */
public function setFonctionEmploye($FonctionEmploye)
{
    $this->FonctionEmploye = $FonctionEmploye;
}

/**
 * @return mixed
 */
public function getNumeroMutuelle()
{
    return $this->NumeroMutuelle;
}

/**
 * @param mixed $NumeroMutuelle
 */
public function setNumeroMutuelle($NumeroMutuelle)
{
    $this->NumeroMutuelle = $NumeroMutuelle;
}

/**
 * @return mixed
 */
public function getDateDebutEmploye()
{
    return $this->DateDebutEmploye;
}

/**
 * @param mixed $DateDebutEmploye
 */
public function setDateDebutEmploye($DateDebutEmploye)
{
    $this->DateDebutEmploye = $DateDebutEmploye;
}

/**
 * @return mixed
 */
public function getDateTitularisationEmploye()
{
    return $this->DateTitularisationEmploye;
}

/**
 * @param mixed $DateTitularisationEmploye
 */
public function setDateTitularisationEmploye($DateTitularisationEmploye)
{
    $this->DateTitularisationEmploye = $DateTitularisationEmploye;
}

/**
 * @return mixed
 */
public function getNumeroTelEmploye()
{
    return $this->NumeroTelEmploye;
}

/**
 * @param mixed $NumeroTelTmploye
 */
public function setNumeroTelEmploye($NumeroTelEmploye)
{
    $this->NumeroTelEmploye = $NumeroTelEmploye;
}

/**
 * @return mixed
 */
public function getNumeroLigneEmploye()
{
    return $this->NumeroLigneEmploye;
}

/**
 * @param mixed $NumeroLigneEmploye
 */
public function setNumeroLigneEmploye($NumeroLigneEmploye)
{
    $this->NumeroLigneEmploye = $NumeroLigneEmploye;
}

/**
 * @return mixed
 */
public function getEmailEmploye()
{
    return $this->EmailEmploye;
}

/**
 * @param mixed $EmailEmploye
 */
public function setEmailEmploye($EmailEmploye)
{
    $this->EmailEmploye = $EmailEmploye;
}

/**
 * @return mixed
 */
public function getMdpEmploye()
{
    return $this->MdpEmploye;
}

/**
 * @param mixed $MdpEmploye
 */
public function setMdpEmploye($MdpEmploye)
{
    $this->MdpEmploye = $MdpEmploye;
}

/**
 * @return mixed
 */
public function getActive()
{
    return $this->active;
}

/**
 * @param mixed $active
 */
public function setActive($active)
{
    $this->active = $active;
}

/**
 * @return mixed
 */
public function getDepartement()
{
    return $this->Departement;
}

/**
 * @param mixed $Departement
 */
public function setDepartement($Departement)
{
    $this->Departement = $Departement;
}

/**
 * @return mixed
 */
public function getImgsrc()
{
    return $this->imgsrc;
}

/**
 * @param mixed $imgsrc
 */
public function setImgsrc($imgsrc)
{
    $this->imgsrc = $imgsrc;
}


public  function setRoles($roles)
{
    $this->roles =$roles;
}

/**
 * @return mixed
 */
public function getEquipe()
{
    return $this->equipe;
}

/**
 * @param mixed $Equipe
 */
public function setEquipe($Equipe)
{
    $this->equipe = $Equipe;
}

/**
 * @return string
 */
public function getPlainPassword()
{
    return $this->plainPassword;
}

/**
 * @param string $plainPassword
 */
public function setPlainPassword($plainPassword)
{
    $this->plainPassword = $plainPassword;
    $this->password = null;
}
}

我不知道这个错误代表什么。

最佳答案

如果您使用依赖注入(inject),则必须将密码编码器作为第三个参数发送。

例子:
服务.yml

services:
     app.loginform.authenticator:
     class: GMAOBundle\Security\LoginFormAuthentificator
       arguments:
           - "@doctrine.orm.entity_manager"
           - "@router"
           - "@passwordencoder"

或者,您可以“ Autowiring ”。
services:
     app.loginform.authenticator:
     class: GMAOBundle\Security\LoginFormAuthentificator
     autowire: true

或者,您正在使用 services.yml 来注入(inject)依赖项。

如果您将 loginFormAuthenticator 创建为对象,则必须将其实例化为:
$loginAuthenticator = new  LoginFormAuthentificator($entitymanager, $router, $passwordEncoder);

当您调用该类时,您在某些参数中缺少密码编码器。

关于php - 使用 symfony3 : Catchable Fatal Error: Argument 3 passed to 编码密码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45143899/

10-16 22:35