登录教程:http://symfony.com/doc/current/security/form_login_setup.html
我在尝试登录时遇到错误:凭据无效。
这里有dev.log:
[2017-01-16 22:57:51] request.INFO: Matched route "login". {"route":"login","route_parameters":{"_controller":"AppBundle\\Controller\\SecurityController::loginAction","_route":"login"},"request_uri":"http://localhost:8000/login","method":"GET"} []
[2017-01-16 22:57:51] security.INFO: Populated the TokenStorage with an anonymous Token. [] [][2017-01-16 23:00:46] request.INFO: Matched route "login". {"route":"login","route_parameters":{"_controller":"AppBundle\\Controller\\SecurityController::loginAction","_route":"login"},"request_uri":"http://localhost:8000/login","method":"POST"} []
[2017-01-16 23:00:46] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at /Users/mac/symfony-project/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:73, Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException(code: 0): Username \"admin\" does not exist. at /Users/mac/symfony-project/vendor/symfony/symfony/src/Symfony/Component/Security/Core/User/InMemoryUserProvider.php:109)"} []
转储($ authenticationUtils-> getLastAuthenticationError()):
SecurityController.php on line 28:
BadCredentialsException {#100 ▼
-token: UsernamePasswordToken {#99 ▼
-credentials: "abcdef"
-providerKey: "main"
-user: "admin"
-roles: []
-authenticated: false
-attributes: []
}
#message: "Bad credentials."
#code: 0
#file: "../vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthentication ▶"
#line: 73
Security.yml
security:
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
cost: 12
providers:
in_memory:
memory: ~
user_provider:
entity:
class: AppBundle\Entity\User
property: username
firewalls:
main:
anonymous: ~
form_login:
login_path: login
check_path: login
SecurityController:
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$form = $this->createForm(LoginType::class);
$authenticationUtils = $this->get('security.authentication_utils');
$a = 1;
$a = 0;
if ($a == 1)
$error = $authenticationUtils->getLastAuthenticationError();
else
die(dump($authenticationUtils->getLastAuthenticationError()));
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', array(
'form' => $form->createView(),
'last_username' => $lastUsername,
'error' => $error,
));
}
登录表单:
<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit">login</button>
</form>
用户实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @UniqueEntity(fields="username", message="Username already taken")
*/
class User implements UserInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Assert\NotBlank()
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @Assert\NotBlank()
* @Assert\Length(max=255)
*/
private $plainPassword;
public function __construct()
{
$this->createdAt = new \DateTime();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* 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;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return User
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Get plain password
*
* @return string
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* Set plain password
*
* @param string $password
*
* @return Users
*/
public function setPlainPassword($password)
{
$this->plainPassword = $password;
return $this;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
public function getSalt()
{
}
}
哪里可能是问题?