php - Symfony 4 守卫不验证用户

标签 php symfony4

我正在使用 guard 作为我的 symfony 4 flex 应用程序的身份验证层。

每当我输入我的用户名和密码时,它会自动将我重定向到登录页面,没有错误只是重定向我。在我的日志中,它显示我无法登录,但表单没有显示任何内容

安全.yaml

security:
    encoders:
        App\Entity\User:
            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: App\Entity\User
                property: username
        db_provider_email:
            entity:
                class: App\Entity\User
                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:
                    - App\Security\LoginFormAuthenticator
                    - App\Security\GoogleAuthenticator
                entry_point: App\Security\LoginFormAuthenticator
            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 App\Security;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

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") && $request->request->has("password");
        //return 'app_login' === $request->attributes->get('_route') && $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 App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;

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')]);
    }
}

登录.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 %}

最佳答案

login.html.twig 中的表单渲染之前添加这个

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

关于php - Symfony 4 守卫不验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55470034/

相关文章:

php - MySql 按 menu_id 分组

php - symfony 4 php模板位置

symfony - 意外的 token "name"值 "if"("end of statement block"预期)

twig - 如何强制 symfony 表单只显示和接受正整数?

PHP 命名空间不起作用 - 疑似 PHP 配置问题

php - 在 WordPress 高级主题上自定义 WordPress 标题导航菜单

php - Ajax 使用方法 load() 获取选择字段中的选项值