我是一个symfony新手,我正在尝试创建一个注册和登录表单。到目前为止,我已成功注册。但登录表单不断向我返回“Bad Credentials”错误。我检查了很多线程有这个问题,但我似乎无法搞清楚。
我创建了User.php,它有一些基础知识:
<?php
namespace Money\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* @ORM\Entity
* @ORM\Table(name="user")
* @UniqueEntity(fields="email", message="Email already taken", groups="registration")
* @UniqueEntity(fields="username", message="Username already taken", groups="registration")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", unique=true)
* @Assert\NotBlank(groups="registration")
*/
private $username;
/**
* @ORM\Column(type="string", unique=true)
* @Assert\Email(groups="registration")
* @Assert\NotBlank(groups="registration")
*/
private $email;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(groups="registration")
* @Assert\MinLength(limit=4, groups="registration", message="Your password needs to have at least 4 characters")
*/
private $password;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\Column(type="string", length=32)
*/
private $salt;
/**
* constructor
*/
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
}
/**
* setters
*/
public function setEmail($email)
{
$this->email = $email;
}
public function setPassword($password)
{
$this->password = $password;
}
public function setUsername($username)
{
$this->username = $username;
}
/**
* advanced
*/
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/**
* @inheritDoc
*/
public function getUsername()
{
return $this->username;
}
/**
* @inheritDoc
*/
public function getEmail()
{
return $this->email;
}
/**
* @inheritDoc
*/
public function getSalt()
{
return $this->salt;
}
/**
* @inheritDoc
*/
public function getPassword()
{
return $this->password;
}
/**
* @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);
}
}
然后我有security.yml
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Money\MainBundle\Entity\User:
algorithm: sha512
encode_as_base64: true
iterations: 10
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
user_db:
entity: { class: Money\MainBundle\Entity\User, property: username }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/login$
security: false
secured_area:
pattern: ^/dashboard
form_login:
check_path: /dashboard/login_check
login_path: /login
logout:
path: /logout
target: /
access_control:
- { path: ^/dashboard, roles: ROLE_ADMIN }
我使用
创建登录表单class LoginForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'text', array('attr' => array('placeholder' => 'Username', 'class' => 'span12')));
$builder->add('password', 'password', array('attr' => array('placeholder' => 'Password', 'class' => 'span12')));
}
// public function getDefaultOptions(array $options)
// {
// return array(
// 'data_class' => 'Money\MainBundle\Entity\User'
// );
// }
public function getName()
{
return 'login';
}
}
,登录操作在这里
public function loginAction()
{
//create login form
$form = $this->createForm(new LoginForm(), new User());
//request
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
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(
'MoneyMainBundle:Main:signup.html.twig',
array(
'error' => $error,
'form' => $form->createView(),
'register' => false
)
);
}
我也像这样编码我的密码:
public function signUpAction()
{
//create valid form entity
$form = $this->createForm(new RegisterForm(), new User());
//check request
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
//useful variables
$user = $form->getData();
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
//encode password
$pass = $encoder->encodePassword($user->getPassword(), $user->getSalt());
$user->setPassword($pass);
//save data to db
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('money_main_dashboard'));
}
}
return $this->render('MoneyMainBundle:Main:signup.html.twig', array(
'form' => $form->createView(),
'register' => true,
'error' => false
));
}
最后,日志在这里:
Marker - Feb 4, 2013 12:02:57 AM
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-02-04 00:03:01] request.INFO: Matched route "money_main_login_check" (parameters: "_route": "money_main_login_check") [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-02-04 00:03:01] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.email AS email3, t0.password AS password4, t0.is_active AS is_active5, t0.salt AS salt6 FROM user t0 WHERE t0.username = ? LIMIT 1 ["NONE_PROVIDED"] []
[2013-02-04 00:03:01] security.INFO: Authentication request failed: Bad credentials [] []
[2013-02-04 00:03:01] security.DEBUG: Redirecting to /login [] []
[2013-02-04 00:03:01] event.DEBUG: Listener "Symfony\Component\Security\Http\Firewall::onKernelRequest" stopped propagation of the event "kernel.request". [] []
[2013-02-04 00:03:01] event.DEBUG: Listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest" was not called for event "kernel.request". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse". [] []
[2013-02-04 00:03:01] security.DEBUG: Write SecurityContext in the session [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onKernelTerminate". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-02-04 00:03:01] request.INFO: Matched route "money_main_login" (parameters: "_controller": "Money\MainBundle\Controller\MainController::loginAction", "_route": "money_main_login") [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onKernelTerminate". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-02-04 00:03:01] request.INFO: Matched route "_wdt" (parameters: "_controller": "Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController::toolbarAction", "token": "510eeca5b649c", "_route": "_wdt") [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] []
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onKernelTerminate". [] []
重要的部分应该是教条调试:
[2013-02-04 00:03:01] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.email AS email3, t0.password AS password4, t0.is_active AS is_active5, t0.salt AS salt6 FROM user t0 WHERE t0.username = ? LIMIT 1 ["NONE_PROVIDED"] []
但我不知道为什么它没有收到任何帖子参数
答案 0 :(得分:2)
问题是输入字段中的名称值是login [username]和login [password],我无法将这些名称更改为_username和_password(login_check接受),因此我必须手动创建表单(不抽象类型和表单构建器)现在它可以工作。
答案 1 :(得分:0)
如果你的表单类型类在getName方法中返回一个空字符串或null,那么你的输入应该有proper名称
class LoginType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('_username')->add('_password', 'password');
}
public function getName() {
return '';
}
}
应该做的伎俩
答案 2 :(得分:0)
只需制作树枝模板并为字段指定名称&#39; _username&#39;和&#39; _password&#39;。那么魔法应该为你完成。
答案 3 :(得分:0)
如果您创建了自己的表单类,还可以在security.yml中输入以下内容:
username_parameter: login[username]
password_parameter: login[password]