Gavntery
Gavntery

Reputation: 51

symfony 2.4 always get Bad credentials error

I am writing a website using the Symfony 2.4 framework but for some reason, the login process is not working.

I always get message: Bad credentials

app/logs/dev.log output

[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 [] []

Use the above doctrine sql, i can find the data in database.

Here is my 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 }

My User.php class

<?php
namespace Bestxtech\UserBundle\Model;

/**
 * User Model
 * 
 * @author Terry Gao <[email protected]>
 */
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));
    }
}

And the 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 %}

Thanks!

Upvotes: 1

Views: 1773

Answers (3)

Gavntery
Gavntery

Reputation: 51

Finally, I found the reason. The code is all fine, but the longth of the pwd column in the dadabase 'User' table is 45 which is too shout. Change to 100, then it's ok.

Upvotes: 2

Julien
Julien

Reputation: 9442

There seem to be an error in the namespaces of your user class

encoders:
    Bestxtech\UserBundle\Model\User: sha512

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

instead of

encoders:
    Bestxtech\UserBundle\Entity\User: sha512

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

Upvotes: 0

Antoine Subit
Antoine Subit

Reputation: 9913

You must extend your User entity from the BaseUser entity of FOSUserBundle to access the basics attributes of a user as username or username_canonical

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

Upvotes: 0

Related Questions