几天来,我一直在努力使用symfony 3.4登录表单 . 无论我做什么,总会出现错误 Invalid credentials.
.
因为它,我现在已经完全厌倦了 . 没有关于这个问题的更多细节 .
下面是一个新的,干净的项目的详细信息,可以预测这个错误 .
~\src\AppBundle\Controller\LoginController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends Controller
{
/**
* @Route("/logowanie", name="logowanie")
* @param Request $request
* @param AuthenticationUtils $authenticationUtils
* @return \Symfony\Component\HttpFoundation\Response
*/
public function logowanieAction(Request $request, AuthenticationUtils $authenticationUtils) {
$error = $authenticationUtils->getLastAuthenticationError();
$lastUserName = $authenticationUtils->getLastUsername();
return $this->render('Login/logowanie.html.twig', array(
'error' => $error,
'lastUserName' => $lastUserName
));
}
}
~\app\Resources\views\Login\logowanie.html.twig
{% extends "base.html.twig" %}
{% block title %}AppBundle:Login:logowanie{% endblock %}
{% block body %}
{% if error %}
{{ error.messageKey }}
{% endif %}
<div class="panel-body">
<form action="{{ path('logowanie') }}" method="post">
<div class="form-group">
<label for="login">Login:</label>
<input type="text" name="_login" id="login" value="{{ lastUserName }}" class="form-control">
</div>
<div class="form-group">
<label for="password">Hasło:</label>
<input type="password" name="_password" id="password" class="form-control">
</div>
<button class="btn btn-info pull-right">Zaloguj</button>
</form>
</div>
{% endblock %}
~\src\AppBundle\DataFixtures\ORM\LoadUserData.php
namespace AppBundle\DataFixtures\ORM;
use AppBundle\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LoadUserData extends Fixture implements ContainerAwareInterface {
private $container;
/**
* Load data fixtures with the passed EntityManager
*
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
$user = new User();
$user->setLogin('admin');
$user->setEmail('admin@admin.admin');
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($user, '123qwe');
$user->setPassword($password);
echo get_class($encoder);
$manager->persist($user);
$manager->flush();
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}
~\app\config\security.yml
# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
db_provider:
entity:
class: AppBundle:User
property: login
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
form_login:
login_path: logowanie
check_path: logowanie
夹具加载成功,"admin"已添加到数据库(MySQL上的VPS) .
一切正常,但不是身份验证 .
app_dev没有错误,日志只显示"INFO"
有任何想法吗?
谢谢 .
EDIT:
〜/ src目录/的appbundle /实体/ user.php的
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements UserInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="login", type="string", length=255, unique=true)
*/
private $login;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set login
*
* @param string $login
*
* @return User
*/
public function setLogin($login)
{
$this->login = $login;
return $this;
}
/**
* Get login
*
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return (Role|string)[] The user roles
*/
public function getRoles()
{
return [
'ROLE_USER'
];
}
/**
* Returns the salt that was originally used to encode the password.
*
* This can return null if the password was not encoded using a salt.
*
* @return string|null The salt
*/
public function getSalt()
{
// TODO: Implement getSalt() method.
}
/**
* @ORM\Column(name="isActive", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
}
/**
* Returns the username used to authenticate the user.
*
* @return string The username
*/
public function getUsername()
{
return $this->login;
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
}
EDIT2:
• echo get_class($encoder)
in fixture load return: Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
•更新了用户类
•我尝试使用http_basic方法登录并且可以正常工作,但是通过表单登录仍然显示无效的凭据 .