Я хочу создать свой собственный аутентификатор входа в форму, внедрив Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface . Ниже мой код и конфигурация:
Мой аутентификатор:
namespace App\Security;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
class TestAuthenticator implements SimpleFormAuthenticatorInterface
{
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
$username = $token->getUser();
$password = $token->getCredentials();
$user = $this->userRepository->findOneBy(['username' => $username, 'password' => $password]);
if (!$user instanceof User) {
throw new AuthenticationException();
}
return new UsernamePasswordToken(
$user,
$user->getPassword(),
$providerKey,
$user->getRoles()
);
}
public function supportsToken(TokenInterface $token, $providerKey)
{
return $token instanceof UsernamePasswordToken
&& $token->getProviderKey() === $providerKey;
}
public function createToken(Request $request, $username, $password, $providerKey)
{
return new UsernamePasswordToken($username, $password, $providerKey);
}
}
Мой контроллер:
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller
{
/**
* @Route("/home", name="home")
*/
public function index()
{
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}
/**
* @Route("/login", name="login")
*/
public function login()
{
return $this->render('home/login.html.twig');
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheck()
{
return new JsonResponse(["status" => true]);
}
/**
* @Route("/logout", name="logout")
*/
public function logout()
{
return new JsonResponse(["status" => true]);
}
}
Моя безопасность.yml
security:
encoders:
App\Entity\User:
algorithm: plain_text
providers:
our_db_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
simple_form:
login_path: /login
check_path: /login_check
remember_me: true
authenticator: App\Security\TestAuthenticator
remember_me:
lifetime: 31536000
always_remember_me: true
remember_me_parameter: _remember_me
path: /
domain: ~
secret: "asdasdasd"
logout:
path: /logout
target: /login
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/forgot, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
Проблема в том, что после выполнения authenticateToken
пользователь будет перенаправлен на домашнюю страницу, но не пройдет аутентификацию.
Попробуйте исправить
authenticator: App\Security\TestAuthenticator
вauthenticator: App\Security\Authenticator
.