Symfony 4 guard не аутентифицирует пользователя

#php #symfony4 #symfony-guard

#php #symfony4 #symfony-guard

Вопрос:

Я использую guard в качестве уровня аутентификации для своего приложения symfony 4 flex.

всякий раз, когда я ввожу свое имя пользователя и пароль, он автоматически перенаправляет меня на страницу входа, без ошибок просто перенаправляет меня. В моих журналах показано, что я не смог войти в систему, но форма ничего этого не показывает

security.yaml

 security:
    encoders:
        AppEntityUser:
            algorithm: bcrypt
            cost: 12
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: ~ }
        chain_provider:
            chain:
                providers: [db_provider_username, db_provider_email]
        db_provider_username:
            entity:
                class: AppEntityUser
                property: username
        db_provider_email:
            entity:
                class: AppEntityUser
                property: email
        oauth:
            id: knpu.oauth2.user_provider

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true

        secured_area:
            anonymous: ~
            provider: chain_provider
            guard:
                authenticators:
                    - AppSecurityLoginFormAuthenticator
                    - AppSecurityGoogleAuthenticator
                entry_point: AppSecurityLoginFormAuthenticator
            remember_me:
                secret:   '%kernel.secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /
            logout:
                path: app_logout
            logout_on_user_change: true



            # activate different ways to authenticate

            # http_basic: true
            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: true
            # https://symfony.com/doc/current/security/form_login_setup.html

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    #access_control:
        #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        # - { path: ^/profile, roles: ROLE_USER }

  

LoginFormAthenticator.php

 <?php

namespace AppSecurity;

use AppEntityUser;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingGeneratorUrlGeneratorInterface;
use SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface;
use SymfonyComponentSecurityCoreExceptionCustomUserMessageAuthenticationException;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
use SymfonyComponentSecurityCoreExceptionInvalidCsrfTokenException;
use SymfonyComponentSecurityCoreSecurity;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreUserUserProviderInterface;
use SymfonyComponentSecurityCsrfCsrfToken;
use SymfonyComponentSecurityCsrfCsrfTokenManagerInterface;
use SymfonyComponentSecurityGuardAuthenticatorAbstractFormLoginAuthenticator;
use SymfonyComponentSecurityHttpUtilTargetPathTrait;

class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    private $entityManager;
    private $urlGenerator;
    private $csrfTokenManager;
    private $passwordEncoder;

    public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->entityManager = $entityManager;
        $this->urlGenerator = $urlGenerator;
        $this->csrfTokenManager = $csrfTokenManager;
        $this->passwordEncoder = $passwordEncoder;
    }

    public function supports(Request $request)
    {
        $request->request->has("username") amp;amp; $request->request->has("password");
        //return 'app_login' === $request->attributes->get('_route') amp;amp; $request->isMethod('POST');
    }

    public function getCredentials(Request $request)
    {
        $credentials = [
            'username' => $request->request->get('username'),
            'password' => $request->request->get('password'),
            'csrf_token' => $request->request->get('_csrf_token'),
        ];
        $request->getSession()->set(
            Security::LAST_USERNAME,
            $credentials['username']
        );

        return $credentials;
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $token = new CsrfToken('authenticate', $credentials['csrf_token']);
        if (!$this->csrfTokenManager->isTokenValid($token)) {
            throw new InvalidCsrfTokenException();
        }

        $user = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $credentials['username']]);

        if (!$user) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('Username could not be found.');
        }

        return $user;
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
            return new RedirectResponse($targetPath);
        }

        // For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
        //throw new Exception('TODO: provide a valid redirect inside '.__FILE__);
        return new RedirectResponse($this->urlGenerator->generate('app_homepage'));
    }

    protected function getLoginUrl()
    {
        return $this->urlGenerator->generate('app_login');
    }
}

  

SecurityController.php

 <?php

namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityHttpAuthenticationAuthenticationUtils;
use SymfonyContractsTranslationTranslatorInterface;

class SecurityController extends AbstractController
{
    /**
     * @Route("/login", name="app_login")
     */
    public function login(AuthenticationUtils $authenticationUtils, TranslatorInterface $translator): Response
    {
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'types' => 'login', 'message' => $translator->trans('All Categories')]);
    }
}

  

login.html.twig

 {% extends 'base.html.twig' %}

{% block title %}NALO Stream : {{ 'Login Page'|trans }}{% endblock %}

{% block body %}
<div class="row">
    <div class="col s5" style="margin: 60px auto; float: none !important;">
        <div class="card">
           <div class="card-content white-text">
               <p class="card-title blue lighten-3 card-panel">{{ 'Sign In'|trans }}</p>
               {% if error %}
                   <div class="card-panel red lighten-3" style="margin-top: 20px;text-align: center">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
               {% endif %}

               <form method="post" style="text-align: center" action="{{ path('app_login') }}">
                   <div class="input-field">
                        <input type="text" name="username" id="username" value="{{ last_username }}" autofocus required/>
                        <label for="username">{{ 'Username'| trans }}</label>
                   </div>

                   <div class="input-field">
                       <input type="password" name="password" id="password" required/>
                       <label for="password">{{ 'Password'| trans }}</label>
                   </div>

                   <div class="row" style="margin-top:30px;text-align: center">
                       <div class="col s6">
                           <label>
                               <input type="checkbox" class="filled-in" checked="checked" name="_remember_me" id="remember-me"/>
                               <span>{{ 'Remember Me'|trans }}</span>
                           </label>
                       </div>
                       <div class="col s6" style="text-align: center">
                           <a href="{{ path('app_user_forget_password') }}" >{{ 'Forget Password?'|trans }}</a>
                       </div>
                   </div>
                   <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">

                   <button class="waves-block waves-effect waves-light btn" type="submit" style="width:100%">
                       <i class="material-icons left">https</i> {{ 'Sign In'|trans }}
                   </button>

                   <p style="margin-top: 30px">Not a member? <a href="{{ path('app_register') }}">{{ 'Sign up'|trans }}</a></p>
                   <br>
                   <p>Sign In With:</p>
                   <br>
                   <a href="{{ path('app_connect_google') }}" class="btn-floating btn-large waves-effect waves-light btn-facebook blue accent-3 white-text"><i class="fab fa-google-plus"></i></a>
               </form>
           </div>
        </div>
    </div>
</div>
{% endblock %}

  

Комментарии:

1. есть ли у вас решение?

Ответ №1:

добавьте это перед отображением формы в login.html.twig

             {% if error %}
                <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
            {% endif %}
  

Ответ №2:

Ваш LoginFormAthenticator::supports метод должен вернуть true , если аутентификатор должен обработать запрос.
У вас есть:

 public function supports(Request $request)
{
    $request->request->has("username") amp;amp; $request->request->has("password");
    //return 'app_login' === $request->attributes->get('_route') amp;amp; $request->isMethod('POST');
}
  

Это ничего не возвращает, поэтому аутентификатор никогда не запускается.
Может быть, вы хотели сделать что-то вроде следующего?

 public function supports(Request $request)
{
    return ($request->isMethod('POST') amp;amp; $request->request->has("username") amp;amp; $request->request->has("password"));
}