我正在尝试使用symfony2安装个人登录表单,并且它始终返回错误的凭据用户。 这是我的代码: 我的控制员:
namespace BO\UserBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception;
use BO\UserBundle\Entity\User;
use BO\UserBundle\Form\UserAddType;
class UserController extends Controller{
public function addUserAction(){
if(!$this->get('security.context')->isGranted('ROLE_ADMIN'))
throw new Exception\AccessDeniedHttpException("Operation denied");
$oUser = new User();
$oFormUser = $this->createForm(
new UserAddType(),
$oUser,
array(
'action' => $this->generateUrl("bo_user_add"),
'method' => "POST"
)
);
$oEM = $this->getDoctrine()->getManager ();
if ($this->getRequest()->getMethod() == "POST"){
$oFormUser->handleRequest($this->get('request'));
if($oFormUser->isValid()){
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($oUser);
$password = $encoder->encodePassword($oUser->getPassword(),$oUser->getSalt());
$oUser->setPassword($password);
$oEM->persist($oUser);
$oEM->flush();
return $this->redirectToRoute("bo_user_log_in");
}
}
return $this->render("BOUserBundle:user:add.html.twig",array("form" => $oFormUser->createView()));
}
}
和我的用户实体
namespace BO\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity as unique;
use Symfony\Component\Security\Core\User\EquatableInterface;
/**
* User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="BO\UserBundle\Entity\UserRepository")
* @unique(fields="email", message="email already exists")
* @unique(fields="username",message="user's already exists")
*/
class User implements UserInterface, \Serializable{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
* @assert\NotBlank()
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="email", type="string", unique=true, length=255)
* @assert\Email(message="Invalid Email")
*/
private $email;
/**
* @var string
* @assert\NotBlank(message="ne peut pas être null")
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var string
* @ORM\Column(name="salt", type="string", length= 255)
* @assert\NotBlank(message="salt ne peut pas être null")
*/
private $salt;
/**
* @ORM\ManyToMany(targetEntity="BO\UserBundle\Entity\Role", inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*/
private $roles;
/**
*
* @var boolean
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function __construct() {
$this->roles = new ArrayCollection();
$this->salt = md5(uniqid(null, true));
$this->isActive = true;
}
public function eraseCredentials(){
}
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*@inheritDoc
* @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
*@inheritDoc
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string $salt
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*@inheritDoc
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Get roles
*@inheritDoc
* @return string
*/
public function getRoles()
{
return $this->roles;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*@inheritDoc
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Add roles
*
* @param \BO\UserBundle\Entity\Role $roles
* @return User
*/
public function addRole(\BO\UserBundle\Entity\Role $roles)
{
$this->roles[] = $roles;
return $this;
}
/**
* Remove roles
*
* @param \BO\UserBundle\Entity\Role $roles
*/
public function removeRole(\BO\UserBundle\Entity\Role $roles)
{
$this->roles->removeElement($roles);
}
/**
* @see \Serializable::serialize()
*/
public function serialize(){
return serialize(array($this->id));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($s){
list($this->id,) = unserialize($s);
}
public function isEqualTo(UserInterface $UI){
return $this->username === $UI->getUsername();
}
}
最后是我的security.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
BO\UserBundle\Entity\User:
algorithm: sha1
encode_as_base64: false
iterations: 0
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
printsign_user:
entity: { class: BO\UserBundle\Entity\User, property: username }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
printsign:
pattern: ^/
anonymous: true
provider: printsign_user
form_login:
login_path: bo_user_log_in
check_path: bo_user_log_check
default_target_path: bo_user_add
logout:
path: bo_user_log_out
target: bo_user_log_in
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
- { path: ^/articles, roles: ROLE_USER }
观点:
{% extends "::base.html.twig"%}
{% form_theme form "BOUserBundle:override:UserLogin.html.twig" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
{% if zErrors is not null %}
{{ zErrors.message }}
{{ last_user }}
{% endif%}
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
<input type="submit" value="envoyer" class="btn btn-primary" />
{{ form_end(form) }}
{% endblock %}
因此,即使我使用textplain进行编码或sha512,它仍然是凭证。 非常感谢你的帮助。 感谢