我尝试登录时一直收到Bad Credentials错误。我做了一切寻找解决方案,但我似乎无法找到我的错误。我的security.yml有什么问题吗?
用户实体:
namespace Reuzze\ReuzzeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Users
*
* @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"}), @ORM\UniqueConstraint(name="user_email_UNIQUE", columns={"user_email"})}, indexes={@ORM\Index(name="fk_users_persons1", columns={"person_id"}), @ORM\Index(name="fk_users_roles1_idx", columns={"role_id"})})
* @ORM\Entity
*/
class Users implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="user_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $userId;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=45, nullable=false)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=60, nullable=false)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="salt", type="string", length=30, nullable=false)
*/
private $salt;
/**
* @var string
*
* @ORM\Column(name="user_email", type="string", length=255, nullable=false)
*/
private $userEmail;
/**
* @var integer
*
* @ORM\Column(name="user_rating", type="integer", nullable=false)
*/
private $userRating;
/**
* @var \DateTime
*
* @ORM\Column(name="user_created", type="datetime", nullable=false)
*/
private $userCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="user_modified", type="datetime", nullable=true)
*/
private $userModified;
/**
* @var \DateTime
*
* @ORM\Column(name="user_deleted", type="datetime", nullable=true)
*/
private $userDeleted;
/**
* @var \DateTime
*
* @ORM\Column(name="user_lastlogin", type="datetime", nullable=true)
*/
private $userLastlogin;
/**
* @var \DateTime
*
* @ORM\Column(name="user_locked", type="datetime", nullable=true)
*/
private $userLocked;
/**
* @var \Reuzze\ReuzzeBundle\Entity\Persons
*
* @ORM\OneToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Persons")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="person_id", referencedColumnName="person_id", unique=true)
* })
*/
private $person;
/**
* @var \Reuzze\ReuzzeBundle\Entity\Roles
*
* @ORM\ManyToOne(targetEntity="Reuzze\ReuzzeBundle\Entity\Roles")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
* })
*/
private $role;
public function __construct(){
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
}
/**
* Set userId
*
* @param integer $userId
* @return Users
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* @return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set username
*
* @param string $username
* @return Users
*/
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 Users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string $salt
* @return Users
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set userEmail
*
* @param string $userEmail
* @return Users
*/
public function setUserEmail($userEmail)
{
$this->userEmail = $userEmail;
return $this;
}
/**
* Get userEmail
*
* @return string
*/
public function getUserEmail()
{
return $this->userEmail;
}
/**
* Set userRating
*
* @param integer $userRating
* @return Users
*/
public function setUserRating($userRating)
{
$this->userRating = $userRating;
return $this;
}
/**
* Get userRating
*
* @return integer
*/
public function getUserRating()
{
return $this->userRating;
}
/**
* Set userCreated
*
* @param \DateTime $userCreated
* @return Users
*/
public function setUserCreated($userCreated)
{
$this->userCreated = $userCreated;
return $this;
}
/**
* Get userCreated
*
* @return \DateTime
*/
public function getUserCreated()
{
return $this->userCreated;
}
/**
* Set userModified
*
* @param \DateTime $userModified
* @return Users
*/
public function setUserModified($userModified)
{
$this->userModified = $userModified;
return $this;
}
/**
* Get userModified
*
* @return \DateTime
*/
public function getUserModified()
{
return $this->userModified;
}
/**
* Set userDeleted
*
* @param \DateTime $userDeleted
* @return Users
*/
public function setUserDeleted($userDeleted)
{
$this->userDeleted = $userDeleted;
return $this;
}
/**
* Get userDeleted
*
* @return \DateTime
*/
public function getUserDeleted()
{
return $this->userDeleted;
}
/**
* Set userLastlogin
*
* @param \DateTime $userLastlogin
* @return Users
*/
public function setUserLastlogin($userLastlogin)
{
$this->userLastlogin = $userLastlogin;
return $this;
}
/**
* Get userLastlogin
*
* @return \DateTime
*/
public function getUserLastlogin()
{
return $this->userLastlogin;
}
/**
* Set userLocked
*
* @param \DateTime $userLocked
* @return Users
*/
public function setUserLocked($userLocked)
{
$this->userLocked = $userLocked;
return $this;
}
/**
* Get userLocked
*
* @return \DateTime
*/
public function getUserLocked()
{
return $this->userLocked;
}
/**
* Set person
*
* @param \Reuzze\ReuzzeBundle\Entity\Persons $person
* @return Users
*/
public function setPerson(\Reuzze\ReuzzeBundle\Entity\Persons $person = null)
{
$this->person = $person;
return $this;
}
/**
* Get person
*
* @return \Reuzze\ReuzzeBundle\Entity\Persons
*/
public function getPerson()
{
return $this->person;
}
/**
* Set role
*
* @param \Reuzze\ReuzzeBundle\Entity\Roles $role
* @return Users
*/
public function setRoles(\Reuzze\ReuzzeBundle\Entity\Roles $role = null)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return \Reuzze\ReuzzeBundle\Entity\Roles
*/
public function getRoles()
{
return array('ROLE_USER');
//return $this->role;
}
public function eraseCredentials(){
}
}
具有注册操作的UserController:
public function registerAction(Request $request){
if ($this->get('security.context')->isGranted('ROLE_USER'))
{
return $this->redirect($this->generateUrl('reuzze_reuzze_homepage'));
}
$user = new Users();
$person = new Persons();
$address = new Addresses();
$region = new Regions();
$role = new Roles();
$address->setRegion($region);
$user->setPerson($person);
$user->setUserRating('1');
$user->setRoles($role);
$person->setAddress($address);
$address->setRegion($region);
$role->setRoleName('Student');
$form = $this->createForm(new RegisterType(), $user);
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if($form->isValid())
{
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
$password = $encoder->encodePassword($user->getpassword(), $user->getsalt());
$user->setpassword($password);
$user->setRoles($role);
$date = new \DateTime('NOW');
$user->setuserCreated($date);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($person);
$entityManager->persist($user);
$entityManager->persist($address);
$entityManager->persist($region);
$entityManager->persist($role);
$entityManager->flush();
return $this->redirect($this->generateUrl('reuzze_reuzze_homepage'));
}
}
return $this->render('ReuzzeReuzzeBundle:User:register.html.twig', array(
'form' => $form->createView()
));
}
具有登录操作的UserController
public function loginAction(Request $request)
{
$session = $request->getSession();
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(
SecurityContext::AUTHENTICATION_ERROR
);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('ReuzzeReuzzeBundle:User:login.html.twig', array(
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}
RegisterType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('person' , new PersonType())
->add('username' , 'text', array(
'label' => 'Username',
'attr' => array('placeholder' => 'Username')
))
->add('userEmail' , 'email', array(
'label' => 'Email',
'attr' => array('placeholder' => 'E-mail address')
))
->add('password' , 'repeated', array(
'type' => 'password',
'first_name' => 'password',
'second_name' => 'confirm',
'first_options' => array(
'attr' => array('class' => 'form-control', 'placeholder' => 'Password'),
'label' => 'Password',
),
'second_options' => array(
'label' => 'Repeat Password',
'attr' => array('class' => 'form-control', 'placeholder' => 'Repeat Password'),
),
'invalid_message' => 'The passwords are not identical!',
)
);
}
public function getName()
{
return 'register';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Reuzze\ReuzzeBundle\Entity\Users',
'validation_groups' => array('registration'),
));
}
Security.yml
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
Reuzze\ReuzzeBundle\Entity\Users:
algorithm: sha512
encode_as_base64: false
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
users:
entity: { class: ReuzzeReuzzeBundle:Users, property: username }
firewalls:
secured_area:
pattern: ^/
form_login:
check_path: /login_check
login_path: /login
#username_parameter: email
#password_parameter: password
logout:
path: /logout
target: /
anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/logout, roles: ROLE_USER }
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
登录视图
{% extends 'ReuzzeReuzzeBundle::layout.html.twig' %}
{# {% block header_title %}Home{% endblock %} #}
{% block content %}
{% if error %}
<div class="form-error">{{ error.message|trans }}</div>
{% endif %}
<form action="{{ path('reuzze_reuzze_login_checkpage') }}" method="post">
<label for="username">Username</label>
<input class="form-control" type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password</label>
<input class="form-control" type="password" id="password" name="_password" />
<button class="btn btn-primary" type="submit">Login</button>
</form>
{% endblock %}
的routing.yml
reuzze_reuzze_homepage:
pattern: /
defaults: { _controller: ReuzzeReuzzeBundle:Default:home }
reuzze_reuzze_registerpage:
pattern: /register
defaults: { _controller: ReuzzeReuzzeBundle:User:register }
reuzze_reuzze_loginpage:
pattern: /login
defaults: { _controller: ReuzzeReuzzeBundle:User:login }
reuzze_reuzze_login_checkpage:
pattern: /login_check
defaults: { _controller: ReuzzeReuzzeBundle:User:loginCheck }
reuzze_reuzze_logoutpage:
pattern: /logout
defaults: { _controller: ReuzzeReuzzeBundle:User:logout }
答案 0 :(得分:0)
Imho你错过了防火墙中的提供商定义:
firewalls:
secured_area:
pattern: ^/
provider: users
form_login:
check_path: /login_check