Symfony 4 simple_form自定义身份验证器不起作用

时间:2018-05-13 13:51:01

标签: symfony authentication

我想通过实现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]);
    }
}

我的security.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后,用户将被重定向到主页,但他/她不会进行身份验证。

1 个答案:

答案 0 :(得分:0)

尝试纠正 authenticator: App\Security\TestAuthenticator中的authenticator: App\Security\Authenticator