的一種方式用戶通過用戶名,然後驗證密碼。如果某個用戶存在給定的用戶名,並且輸入的密碼與該用戶的密碼匹配,則認證該用戶。 例子:
public function userLogin($username, $password)
{
$em = $this->getEntityManager();
$query = $em->createQuery('SELECT u FROM VenomCoreBundle:User u WHERE u.username = :username OR u.email = :username AND u.isDeleted <> 1 ')
->setParameter('username', $username);
try {
$entity = $query->getSingleResult();
if (count($entity) > 0) {
$encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
$passwordEnc = $encoder->encodePassword($password, $entity->getSalt());
$userPassword = $entity->getPassword();
if ($passwordEnc == $userPassword) {
$tokenValue = $entity->getUserToken();
$profile = $entity->getUserProfile();
if(!$profile) {
return false;
}
$userName = $profile->getFullName();
$response = array(
'token' => $tokenValue,
'username' => $userName
);
} else {
return false;
}
}
} catch (\Doctrine\Orm\NoResultException $e) {
return false;
}
return $response;
}
對鏈接問題我的回答有幫助嗎?還是隻是混淆了事情? – Cerad 2013-03-07 19:29:28
您使用的是什麼樣的web服務?密碼是如何編碼的?也許你必須按照http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html中所述創建一個身份驗證提供程序......儘管這是一項很多工作! 退房http://stackoverflow.com/questions/14928514/symfony2-authentication-via-3rd-party-rest-api/14947332#14947332 ..它應該幫助你開始。 – hacfi 2013-03-08 01:08:40
非常感謝您的答案!我會盡力像@hacfi建議的[這裏](http://stackoverflow.com/questions/14928514/symfony2-authentication-via-3rd-party-rest-api/14947332#14947332)。 – SupaCoco 2013-03-08 14:30:29