[英]Symfony 3 doesn't authenticate user from database
我目前正在学习Symfony,但对存储在数据库中的用户的身份验证有疑问。 我从这里开始遵循教程,但是我总是收到“无效的凭据”错误。 到目前为止,这里是我的代码(对不起名称,但这是“客户”想要的)。
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中的用户登录即可。 但这不是客户想要的。 有人可以帮我吗?
这是用户的实体类。 由于代码的长度,我离开了getter和setter方法:
<?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);
}
}
感谢您提供的实体,
为什么在您的防火墙中您的提供者发表评论? 你应该有:
default:
anonymous: ~
form_login:
provider: mysql_provider
login_path: login
check_path: login
require_previous_session: false
default_target_path: /auth/bnme
我终于解决了! 我犯的错误是如此严重,以至于我会轻易地放弃我所有的声誉点。 因为我不配他们。 这是我的用户实体中的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();
}
}
我会把它们全部扔掉,然后用木头或类似的东西工作.....
尝试将mysql_provider更改为default_provider; 还要检查您的用户(Benutzer)实体是否实现了UserInterface。 使用自定义存储库来加载用户不会有什么坏处,但这不应解决您的问题。
另外,在我的代码中,我使用来自服务“ security.encoder_factory”的EncoderFactory :: getEncoder(User)编码密码。 那可能就是您的问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.