我正在学习Symfony,并且对存储在数据库中的用户进行身份验证时遇到问题。我从here开始遵循教程,但我总是收到“无效凭据”错误。到目前为止我的代码来了(对不起名字,但这就是“客户”想要的)。
security.yml:
security:
encoders:
AppBundle\Entity\Benutzer:
algorithm: bcrypt
providers:
mysql_provider:
entity:
class: AppBundle:Benutzer
property: bNID
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
anonymous: ~
form_login:
login_path: login
check_path: login
require_previous_session: false
default_target_path: /auth/bnme
provider: mysql_provider
logout:
path: /logout
target: /
这是我将用户加载到数据库的方式
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Benutzer;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class UserController extends Controller
{
/**
* @Route("/user", name="user")
*/
public function createAction(){
//Get Entity Object
$user = new Benutzer();
//Encrypt password
$plainpw = 'Initial-PW-55218';
$encoder = $this->container->get('security.password_encoder');
$pwd = $encoder->encodePassword($user, $plainpw);
//Set Entity fields
$user->setANREDE("Herr");
$user->setBNID("GPUCS@web.de");
$user->setINIPW($pwd);
$user->setBNTYP("SA");
$user->setKOEPID(1);
$user->setNACHNAME("KomBau-System");
$user->setVORNAME("UL");
//get Entity Manager
$em = $this->getDoctrine()->getManager();
//write Entity in database
$em->persist($user);
$em->flush();
return new Response('Created user with id: '.$user->getBNID());
}
}
这是我的loginAction:
public function loginAction(Request $request){
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$system = $this->getDoctrine()->getRepository('AppBundle:System')->find(1);
return $this->render('default/sstm.html.twig', array('error' => $error, 'system' => $system));
}
使用硬编码到security.yml的用户登录有效。但这不是客户想要的。有人可以帮我吗?
这是用户的实体类。由于代码的长度,我离开了吸气剂和制定者:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
*
* @ORM\Table(name="Benutzer_TAB")
* @ORM\Entity
*
* This class represents database table Benutzer_TAB which holds user informations
*/
class Benutzer implements UserInterface, \Serializable
{
/**
* @var int
*
* @ORM\Column(name="KOEPID", type="integer", length=8)
*/
private $kOEPID;
/**
* @var string
*
* @ORM\Column(name="BNID", type="string", length=255, unique=true)
* @ORM\id
*
* User-ID as email adress
*/
private $bNID;
/**
* @var string
*
* @ORM\Column(name="BNTYP", type="string", length=2)
*
* Usertype (Systemadmin or normal user)
*/
private $bNTYP;
/**
* @var string
*
* @ORM\Column(name="ANREDE", type="string", length=4)
*/
private $aNREDE;
/**
* @var string
*
* @ORM\Column(name="VORNAME", type="string", length=255)
*/
private $vORNAME;
/**
* @var string
*
* @ORM\Column(name="NACHNAME", type="string", length=255)
*/
private $nACHNAME;
/**
* @var string
*
* @ORM\Column(name="INIPW", type="string", length=255)
*/
private $iNIPW;
/**
* @var string
*
* @ORM\Column(name="BNPW", type="string", length=255, nullable=true)
*/
private $bNPW;
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getRoles()
*/
public function getRoles() {
return array('ROLE_USER');
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getPassword()
*/
public function getPassword() {
if(!$this->getBNPW()){
$this->getINIPW();
} else {
$this->getBNPW();
}
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getSalt()
*/
public function getSalt() {
return null;
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::getUsername()
*/
public function getUsername() {
$this->getBNID();
}
/**
* {@inheritDoc}
* @see \Symfony\Component\Security\Core\User\UserInterface::eraseCredentials()
*/
public function eraseCredentials() {
// TODO: Auto-generated method stub
}
/**
* {@inheritDoc}
* @see Serializable::serialize()
*/
public function serialize() {
return serialize(array(
$this->kOEPID,
$this->bNID,
$this->iNIPW,
));
}
/**
*
* {@inheritDoc}
*
* @see Serializable::unserialize()
*/
public function unserialize($serialized) {
list(
$this->kOEPID,
$this->bNID,
$this->iNIPW,) = unserialize($serialized);
}
}
答案 0 :(得分:1)
感谢实体,
为什么在您的防火墙中您的提供商是评论? 你应该:
default:
anonymous: ~
form_login:
provider: mysql_provider
login_path: login
check_path: login
require_previous_session: false
default_target_path: /auth/bnme
答案 1 :(得分:1)
我终于解决了!我犯的错误是如此严重,以至于我轻松地将我的所有声誉都放弃了。因为我不值得他们。这是我在用户实体中的getPassword()函数的代码:
public function getPassword() {
if(!$this->getBNPW()){
$this->getINIPW();
} else {
$this->getBNPW();
}
}
这就是它应该是这样的:
public function getPassword() {
if(!$this->getBNPW()){
return $this->getINIPW();
} else {
return $this->getBNPW();
}
}
我会把它扔掉,用木头或类似的东西......
答案 2 :(得分:0)
尝试将mysql_provider更改为default_provider;还要检查您的User(Benutzer)实体是否实现了UserInterface。使用自定义存储库来加载用户并没有什么坏处,但它不应该是解决问题的方法。
此外,在我的代码中,我使用服务'security.encoder_factory'编码EncoderFactory :: getEncoder(User)的密码。这可能是你的问题所在。