【发布时间】:2014-02-16 16:45:40
【问题描述】:
我正在使用 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 [] []
使用上面的学说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 %}
谢谢!
【问题讨论】:
-
在 security.yml 文件的 security 下直接添加 hide_user_not_found: false 。如果是未找到用户或密码问题,错误消息将显示。
-
这是一个密码问题,SQL返回一个用户,正如OP所提到的。你如何创建你的用户?您是否正确散列密码?
-
@Cerad 错误消息说“提供的密码无效”。
-
@Julien 是的。在 security.yml 中,它使用 sha512 作为编码器,我确信它使用编码器和 salt 来保存用户密码。但是如何确保它在登录时使用编码器和盐呢?谢谢大家!
-
至少你知道找到用户是成功的一半。我会直接进入 Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder 并在 isPasswordValid 中放入一些调试语句。