symfony 2.4总是得到错误的凭据错误

时间:2014-02-16 16:45:41

标签: php symfony login

我正在使用Symfony 2.4框架编写一个网站,但出于某种原因,登录过程无效。

我总是收到消息:凭据错误

app / logs / dev.log输出

[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-12-15 02:16:23] request.INFO: Matched route "security_login_check" (parameters: "_route": "security_login_check") [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-12-15 02:16:23] doctrine.DEBUG: SELECT t0.id AS id1, t0.user_name AS user_name2, t0.group_id AS group_id3, t0.pwd AS pwd4, t0.salt AS salt5, t0.email AS email6, t0.description AS description7, t0.is_pay AS is_pay8, t0.reg_at AS reg_at9, t0.last_login_at AS last_login_at10, t0.pay_at AS pay_at11, t0.pay_expire_at AS pay_expire_at12, t0.delete_flag AS delete_flag13, t0.raw_add_time AS raw_add_time14, t0.raw_update_time AS raw_update_time15, t0.group_id AS group_id16 FROM User t0 WHERE t0.user_name = ? LIMIT 1 ["test002"] []
[2013-12-15 02:16:23] security.INFO: Authentication request failed: Bad credentials [] []
[2013-12-15 02:16:23] security.DEBUG: Redirecting to /login [] []

使用上面的doctrine sql,我可以在数据库中找到数据。

这是我的security.yml

security:
encoders:
    Bestxtech\UserBundle\Entity\User: sha512

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Entity\User, property: userName }

acl:
    connection: default

firewalls:
    main:
        pattern:    ^/
        form_login:
            check_path: /login_check
            login_path: /login
        logout:
            path:   /logout
            target: /
        anonymous: true

        remember_me:
            key:      "%secret%"
            lifetime: 31536000
            path:     /
            domain:   ~

access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

我的User.php类

<?php
namespace Bestxtech\UserBundle\Model;

/**
 * User Model
 * 
 * @author Terry Gao <gavntery@gmail.com>
 */
abstract class User implements UserInterface
{
    /**
     * @var integer
     */
    protected $id;

    /**
     * @var string
     */
    protected $userName;

    /**
     * @var integer
     */
    protected $groupId;

    /**
     * @var string
     */
    protected $pwd;

    /**
     * @var string
     */
    protected $salt;

    /**
     * @var string
     */
    protected $email;

    /**
     * @var string
     */
    protected $description;

    /**
     * @var boolean
     */
    protected $isPay;

    /**
     * @var \DateTime
     */
    protected $regAt;

    /**
     * @var \DateTime
     */
    protected $lastLoginAt;

    /**
     * @var \DateTime
     */
    protected $payAt;

    /**
     * @var \DateTime
     */
    protected $payExpireAt;

    /**
     * @var boolean
     */
    protected $deleteFlag;

    /**
     * @var \DateTime
     */
    protected $rawAddTime;

    /**
     * @var \DateTime
     */
    protected $rawUpdateTime;

    /**
     * @var \Bestxtech\UserBundle\Entity\Usergroup
     */
    protected $group;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->regAt = new \DateTime("now");
        $this->salt    = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
        $this->deleteFlag = 0;
        $this->rawAddTime = new \DateTime("now");
    }

    /**
     * {@inheritDoc}
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * {@inheritDoc}
     */
    public function setUserName($userName)
    {
        $this->userName = $userName;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getUserName()
    {
        return $this->userName;
    }

    /**
     * {@inheritDoc}
     */
    public function setGroupId($groupId)
    {
        $this->groupId = $groupId;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getGroupId()
    {
        return $this->groupId;
    }

    /**
     * {@inheritDoc}
     */
    public function setPwd($pwd)
    {
        $this->pwd = $pwd;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getPwd()
    {
        return $this->pwd;
    }

    public function getPassword() 
    {
        return $this->getPwd();
    }

     /**
     * {@inheritDoc}
     */
    public function getSalt()
    {
//        return $this->salt;
        return '';
    }

    /**
     * {@inheritDoc}
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * {@inheritDoc}
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * {@inheritDoc}
     */
    public function setIsPay($isPay)
    {
        $this->isPay = $isPay;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getIsPay()
    {
        return $this->isPay;
    }

    /**
     * {@inheritDoc}
     */
    public function setRegAt($regAt)
    {
        $this->regAt = $regAt;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getRegAt()
    {
        return $this->regAt;
    }

    /**
     * {@inheritDoc}
     */
    public function setLastLoginAt($lastLoginAt)
    {
        $this->lastLoginAt = $lastLoginAt;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getLastLoginAt()
    {
        return $this->lastLoginAt;
    }

    /**
     * {@inheritDoc}
     */
    public function setPayAt($payAt)
    {
        $this->payAt = $payAt;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getPayAt()
    {
        return $this->payAt;
    }

    /**
     * {@inheritDoc}
     */
    public function setPayExpireAt($payExpireAt)
    {
        $this->payExpireAt = $payExpireAt;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getPayExpireAt()
    {
        return $this->payExpireAt;
    }

    /**
     * {@inheritDoc}
     */
    public function setDeleteFlag($deleteFlag)
    {
        $this->deleteFlag = $deleteFlag;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getDeleteFlag()
    {
        return $this->deleteFlag;
    }

    /**
     * {@inheritDoc}
     */
    public function setRawAddTime($rawAddTime)
    {
        $this->rawAddTime = $rawAddTime;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getRawAddTime()
    {
        return $this->rawAddTime;
    }

    /**
     * {@inheritDoc}
     */
    public function setRawUpdateTime($rawUpdateTime)
    {
        $this->rawUpdateTime = $rawUpdateTime;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getRawUpdateTime()
    {
        return $this->rawUpdateTime;
    }

    /**
     * {@inheritDoc}
     */
    public function setGroup(\Bestxtech\UserBundle\Entity\Usergroup $group = null)
    {
        $this->group = $group;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function getGroup()
    {
        return $this->group;
    }

    /**
     * {@inheritDoc}
     */
    public function getRoles()
    {
        return array('ROLE_USER');
    }

    /**
     * {@inheritDoc}
     */
    public function eraseCredentials()
    {
        $this->pwd = null;
    }
}

SecurityController.php

<?php
namespace Bestxtech\UserBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;

/**
 * Security Controller
 */
class SecurityController extends Controller
{
    public function loginAction()
    {
//        $this->get('bestxtech.breadcrumb')->add('Sign In');

        $request = $this->getRequest();
        $session = $request->getSession();

        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            if ($session->has(SecurityContext::AUTHENTICATION_ERROR)) {
                $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
                $session->remove(SecurityContext::AUTHENTICATION_ERROR);
            } else {
                $error = '';
            }
        }

        if ($error) {
            $this->get('session')->getFlashBag()->set('error', $error->getMessage());
        }

        $lastUsername = $session->get(SecurityContext::LAST_USERNAME);
        $targetPath   = $request->headers->get('referer');

        return $this->render('BestxtechUserBundle:Security:login.html.twig', array('last_username' => $lastUsername, 'target_path' => $targetPath));
    }
}

和login.html.twig

{% extends 'BestxtechUserBundle::layout.html.twig' %}

{% block user_body %}
    <div class="box-header">
        <h2>Sign In</h2>
    </div>
    <div class="box-body">
        <form class="form-horizontal" action="{{ path("security_login_check") }}" method="post">
            <input type="hidden" name="_target_path" value="{{ target_path }}" />
            <div class="control-group">
                <label class="control-label">Username</label>

                <div class="controls">
                    <input type="text" class="input-large" id="username" name="_username" value="{{ last_username }}" required="required"/>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label">Password</label>

                <div class="controls">
                    <input type="password" class="input-large" id="password" name="_password" required="required"/>
                </div>
            </div>

            <div class="control-group">
                <div class="controls">
                    <label for="remember_me" class="checkbox"><input type="checkbox" id="remember_me" name="_remember_me" value="on"/> Remember me
                    </label>
                    <button class="btn btn-success" type="submit">Sign In</button>
                </div>
            </div>
        </form>
    </div>

{% endblock %}

{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
    $(document).ready(function(){
        var username = $('#username');
        var password = $('#password');
        if (username.val().length) {
            password.focus();
        }
        else {
            username.focus();
        }
    });
</script>
{% endblock %}

谢谢!

3 个答案:

答案 0 :(得分:2)

最后,我找到了原因。代码都很好,但数据库'User'表中的pwd列的长度是45,这太大了。改为100,然后没关系。

答案 1 :(得分:0)

您必须将User实体从FOSUserBundle的BaseUser实体扩展为以usernameusername_canonical

的形式访问用户的基本属性

/**
 * User Model
 * 
 * @ORM\Entity
 * @ORM\Table(name="User")
 * @author Terry Gao <gavntery@gmail.com>
 */
abstract class User implements UserInterface extends BaseUser
{
    // Your own logic ...
}

答案 2 :(得分:0)

用户类的名称空间似乎有错误

encoders:
    Bestxtech\UserBundle\Model\User: sha512

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Model\User, property: userName }

而不是

encoders:
    Bestxtech\UserBundle\Entity\User: sha512

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Entity\User, property: userName }