我正在使用 Symfony 2.7,但在登录用户时遇到了问题。我使用自定义实体,并使用适当的变量和方法创建了一个自定义实体“操作员”。我在安全文件中使用了两种登录方法。一个是in_memory
,我将它用于正常工作的管理员登录,另一个是user_db
. 我使用 bcrypt 算法来加密密码。当我尝试登录用户时,它会显示“无效凭据”消息。我究竟做错了什么?谢谢!
安全.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Application\AdminBundle\Entity\Operator:
algorithm: bcrypt
providers:
chain_provider:
chain:
providers: [ in_memory, user_db ]
user_db:
entity:
class: ApplicationAdminBundle:Operator
property: username
安全控制器.php
<?php
namespace Application\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
return $this->render('ApplicationAdminBundle:security:login.html.twig',
array(
'error' => $error,
)
);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
}
}
“操作员”实体和 setPassword()、getPassword() 方法
public function setPassword($password)
{
$this->password = password_hash ($password, PASSWORD_BCRYPT);
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}