Php Symfony 2.6安全BCryptPasswordEncoder错误
我正在用symfony2.6、php5.4、MySQL 5.6和Twig开发一个web应用程序。我还使用了YAML和bcrypt 目前我正在开发一个登录表单,我遵循了,但是当我测试web应用程序时,我收到了以下错误:Php Symfony 2.6安全BCryptPasswordEncoder错误,php,mysql,symfony,login,bcrypt,Php,Mysql,Symfony,Login,Bcrypt,我正在用symfony2.6、php5.4、MySQL 5.6和Twig开发一个web应用程序。我还使用了YAML和bcrypt 目前我正在开发一个登录表单,我遵循了,但是当我测试web应用程序时,我收到了以下错误: Warning: password_verify() expects parameter 2 to be string, resource given Stack Trace in vendor/symfony/symfony/src/Symfony/Component/S
Warning: password_verify() expects parameter 2 to be string, resource given
Stack Trace in vendor/symfony/symfony/src/Symfony/Component/Security/Core/Encoder/BCryptPasswordEncoder.php at line 89 -
public function isPasswordValid($encoded, $raw, $salt)
{
return !$this->isPasswordTooLong($raw) && password_verify($raw, $encoded);
}
}
这是相关代码:
Security.xml
security:
encoders:
InterempleaBundle\Entity\Usuario:
algorithm: bcrypt
cost: 12
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
mysql_db_provider:
entity:
class: InterempleaBundle:Usuario
property: email
firewalls:
admin_area:
pattern: ^/IniciaSesion
http_basic: ~
provider: mysql_db_provider
form_login:
login_path: index
check_path: /IniciaSesion/login_check
failure_path: index
access_control:
- { path: ^/IniciaSesion, roles: ROLE_ADMIN }
Entity\Usuario.php(用户实体)
登录操作内部安全控制器
...
public function loginAction() {
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$repositorioUsuario = $this->getDoctrine()->getRepository('InterempleaBundle:Usuario');
$usuario = $repositorioUsuario->loadUserByUsername($lastUsername);
return $this->render(
'InterempleaBundle:Usuario:panel_principal.html.twig', array(
// last username entered by the user
'last_username' => $usuario->id,
'error' => $error,
)
);
}
...
我怀疑实体中的salt属性,但是教程说它必须为null
会发生什么?我是不是错过了一步
请随意询问任何其他代码或解释
提前谢谢 根据@Martin Rios的建议,我检查了
$encoded
变量的内容,发现在Symfony2教程中数据库中的密码字段是一个varchar(64),而我有一个二进制(64)。因此,我将数据类型更改为密码字段,使用条令命令重新生成实体,清理缓存,结果成功了 在调用password\u verify函数之前执行var\u dump($encoded)并查看它的输出。如果这个函数需要一个字符串,并且它得到一个对象,那么它将失败。
...
public function loginAction() {
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$repositorioUsuario = $this->getDoctrine()->getRepository('InterempleaBundle:Usuario');
$usuario = $repositorioUsuario->loadUserByUsername($lastUsername);
return $this->render(
'InterempleaBundle:Usuario:panel_principal.html.twig', array(
// last username entered by the user
'last_username' => $usuario->id,
'error' => $error,
)
);
}
...