내가 더 적절한 방법, 이럴 생각할 수있는,이 문제를 해결하려면 다음
이용자 엔티티가 추가 속성이있을 것이다 $ loginAttempts이 로그인이 실패 할 때마다 incrementLoginAttempts() 메소드에 의해 증가됩니다. ORM을 통해 0으로 초기화되고 isLocked() 메소드는 5 회 시도했는지 여부를 알려줍니다. 그런 다음
<?php
// AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* @ORM\Entity
* @ORM\Table(name="`user`")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
public function __construct()
{
parent::__construct();
}
/**
* @ORM\Column(type="integer",options={"default"=0})
*/
private $loginAttempts;
...
public function getLoginAttempts()
{
return $this->loginAttemps;
}
public function incrementLoginAttempts()
{
if($this->loginAttempts<5){
$this->loginAttempts++;
}
return $this;
}
public function isLocked()
{
return ($this->loginAttempts == 5)
}
public function resetLoginAttempts()
{
$this->loginAttempts =0;
return $this;
}
는 incrementLoginAttempts를()마다 로그인이 실패 SecuritySubscriber 이벤트에
EventSubscriber를 만들고, 화재, 동시에 검사에서 사용자가 이미 잠금되었거나 아직
<?php
// src/AppBundle/EventSubscriber/SecuritySubscriber.php
namespace AppBundle\EventSubscriber;
use AppBundle\Entity\User;
class SecuritySubscriber implements EventSubscriberInterface
{
private $entityManager;
private $tokenStorage;
private $authenticationUtils;
public function __construct(EntityManager $entityManager, TokenStorageInterface $tokenStorage, AuthenticationUtils $authenticationUtils)
{
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
$this->authenticationUtils = $authenticationUtils;
}
public static function getSubscribedEvents()
{
return array(
AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
);
}
public function onAuthenticationFailure(AuthenticationFailureEvent $event)
{
$existingUser = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $username]);
if ($existingUser) {
$existingUser->incrementLoginAttempts();
$this->entityManager->persist($existingUser);
$this->entityManager->flush();
if($existingUser->isLocked()){
// Do your logic here
// Do not forget to un $existingUser->resetLoginAttempts() when necessary
}
}
}
}
서비스로
# app/config/services.yml
services:
app.security.authentication_event_listener:
class: AppBundle\EventSubscriber\SecuritySubscriber
arguments:
- "@doctrine.orm.entity_manager"
- "@security.token_storage"
- "@security.authentication_utils"
P.S을 가입자를 등록하는 것을 잊지 마세요되지 않은 경우 : 코드는 테스트되지 않았습니다.
이 기능은 Symfony의 Security Core 및 FOSUserBundle 내부에서 사용된다는 것을 알고 계셨습니까? 나는 잘 모르겠지만 당신이 말했듯이 그렇게 쉽지는 않다고 생각합니다. 제발 또 다른 대답을 알고 싶습니다. –
기본 클래스에서는 아무 것도 할 수 없습니다. 나는 당신이 당신의'User' 클래스에서이 메소드를 오버라이드했다고 가정합니다. 너도 하나도 없어? – Azuloo