我正在制作一个Symfony2网站但是我在使用户身份验证时遇到了一些问题。注册表单效果很好,但是当我尝试登录时,我得到了" Bad凭证"错误。
当然,我检查了我的数据库字段,并将它们设置为255 char以获得sha512哈希值。我甚至试图用明文存储密码,但我得到了同样的错误。
我正在使用在Mac OS上运行的Symfony 2.6.4和PHP 5.5.14。我在网上或论坛上找不到任何对我有用的东西,所以我希望你能在这里找到问题所在:/
这是我的代码:
#app/config/security.yml
security:
encoders:
Pass10\PersonBundle\Entity\Person: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
providers:
person:
entity: { class: Pass10PersonBundle:Person, property: username }
firewalls:
secured_area:
pattern: ^/user
provider: person
anonymous: true
form_login:
login_path: /user/login
check_path: /user/login_check
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
路由文件:
#src/pass10/PersonBundle/Resources/Config/routing.yml
pass10_person_login:
path: /login
defaults: { _controller: Pass10PersonBundle:Person:login }
pass10_person_login_check:
path: /login_check
pass10_person_logout:
path: /logout
defaults: { _controller: Pass10PersonBundle:Person:logout }
pass10_person_signup:
path: /signup
defaults: { _controller: Pass10PersonBundle:Person:signup }
用户实体(我只显示相关代码):
/**
* Pass10\PersonBundle\Entity\Person
*
* @ORM\Table(name="person")
* @ORM\Entity(repositoryClass="Pass10\PersonBundle\Entity\PersonRepository")
*/
class Person implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="string", length=30, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=100)
*/
private $salt;
/**
* @ORM\Column(type="string", length=255)
*/
private $password;
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->createdAt = date_create(date('Y-m-d H:i:s'));
}
/**
* @inheritDoc
*/
public function getUsername()
{
return $this->username;
}
/**
* @inheritDoc
*/
public function getSalt()
{
return $this->salt;
}
/**
* @inheritDoc
*/
public function getRoles()
{
return array('ROLE_USER');
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
}
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
) = unserialize($serialized);
}
/**
* Set password
*
* @param string $password
* @return Person
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
}
以下是持久保存用户的代码:
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($person);
$password = $encoder->encodePassword($person->getPassword(), $person->getSalt());
$person->setPassword($password);
$em = $this->getDoctrine()->getManager();
$em->persist($person);
$em->flush();
控制器中的loginAction:
public function loginAction(Request $request){
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
} else {
$error = $session->get(Security::AUTHENTICATION_ERROR);
$session->remove(Security::AUTHENTICATION_ERROR);
}
$person = new Person();
$form = $this->createForm('login', $person);
return $this->render('Pass10PersonBundle:Person:login.html.twig', array(
'form' => $form->createView(),
// last username entered by the user
'last_username' => $session->get(Security::LAST_USERNAME),
'error' => $error,
));
}
视图
{% extends '::base.html.twig' %}
{% block main_content %}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
{{ form(form, {'action': path('pass10_person_login_check')}) }}
{% endblock %}
答案 0 :(得分:0)
我感觉您的登录表单不正确,查看它所说的documentation symfony
您的工作是显示登录表单和可能发生的任何登录错误,但安全系统本身负责检查提交的用户名和密码并验证用户身份。
,用户名和密码的要求如下文所述。
表单看起来像什么,但有一些要求:
您的登录表单似乎缺少_username
和_password
。
尝试在登录视图中使用以下代码
{% if error %}
<div>{{ error.messageKey|trans(error.messageData) }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<button type="submit">login</button>
</form>
我希望这会有所帮助。
请务必将{{ path('login_check') }}
更改为您已设置的