2017-07-18 6 views
1

Ich habe Web-Anwendung mit Symfony 3 erstellt, ich habe Recaptcha zu meinem Login-Formular hinzugefügt mit EWZRecaptchaBundle, wie ich einen Listener vor dem Login hinzufügen kann, um die Validierung von Recaptcha zu überprüfen.Hörer vor dem Login hinzufügen

<form method="post" action="{{ path('mysubmited') }}" id="form-validation" name="form-validation"> 

<div class="form-group form-input-icon form-input-icon-right"> 
<i class="icmn-spinner11 cat__core__spin"></i> 
<div> {{ form_widget(form.username) }}</div> 
</div> 

<div class="form-group"> 

<div>{{ form_widget(form.password) }}</div> 
</div> 
<div class="offset-md-3 col-md-4"> 
{% form_theme form 
'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %} 
{{ form_widget(form.recaptcha) }} 
</div> 
<div class="form-actions"> 
<button type="submit" class="btn btn-primary">Connexion</button> 
<label class="ml-3"> 
<a href="#" class="swal-btn-lost-password"> Mot de passe oublié ?</a> 
</label> 
</div> 
</form> 

security.yml

form_login: 
     check_path: /mysubmited 
     login_path: /login 
     username_parameter: "login_form[username]" 
     password_parameter: "login_form[password]" 
     #recaptcha_parameter: "login_form[recaptcha]" 
     csrf_parameter: "login_form[_token]" 
     csrf_token_id: a_private_string 
     provider: my_provider 
     default_target_path: homepage-auth 

SecurityController.php

/** 
    * Login check action. 
    * 
    * @Route("/mysubmited", name="mysubmited") 
    * @throws \RuntimeException 
    */ 
    public function mysubmitedAction(Request $request) 
    { 

     throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.'); 
    } 

Antwort

1

Hier ist ein Beispiel - die Magie dünn ist "Authentication Ereignisse". Haben Sie auch einen Blick hier ->https://symfony.com/doc/current/components/security/authentication.html

namespace AppBundle\Listener; 


use Doctrine\ORM\EntityManager; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Symfony\Component\HttpFoundation\Session\Session; 
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; 

class LoginListener 
{ 

    private $session; 
    private $tokenstorageInterface; 
    private $em; 
    private $container; 

    public function __construct(TokenStorageInterface $tokenStorageInterface) 
    { 
     $this->tokenstorageInterface = $tokenStorageInterface; 
    } 


    public function onLogin(InteractiveLoginEvent $event) 
    { 
     //toDo onLogin 
    } 

} 

Dann Sie einen Dienst wie diese (app/config/services.yml) definieren:

# Learn more about services, parameters and containers at 
# http://symfony.com/doc/current/book/service_container.html 
parameters: 
# parameter_name: value 
    account.login_listener.class: AppBundle\Listener\LoginListener 


services: 
# service_name: 
#  class: AppBundle\Directory\ClassName 
#  arguments: ["@another_service_name", "plain_value", "%parameter_name%"] 

    # Listeners 
    account.login_listener: 
     class: "%account.login_listener.class%" 
     arguments: ["@security.token_storage"] 
     tags: 
      - { name: kernel.event_listener, event: security.interactive_login, method: onLogin } 
+0

Dieser Listener ist nach dem Login, aber ich möchte den geheimen Schlüssel von Recaptcha vor dem Login überprüfen. –

-1

Ich verstehe nicht, warum Sie nicht sind Indem Sie das Anmeldeformular korrekt mit FormType verwenden, können Sie das Recaptcha-Paket dort problemlos verarbeiten.

LoginType.php

namespace AppBundle\Form\Type; 

use \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType; 
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue; 

class LoginType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
       ->add('username', \Symfony\Component\Form\Extension\Core\Type\TextType::class, array(
       )) 
       ->add('password', \Symfony\Component\Form\Extension\Core\Type\PasswordType::class, array(
       )) 
       ->add('recaptcha', EWZRecaptchaType::class, array(
        'attr' => array(
         'options' => array(
          'theme' => 'light', 
          'type' => 'image' 
         ) 
        ), 
        'mapped' => false, 
        'constraints' => array(
         new RecaptchaTrue() 
        ), 
        'label' => false 
     )); 
    } 
} 

Sobald if ($form->isValid() && $form->isSubmitted()) { wird es genannt wird auch prüfen, ob Captcha korrekt ist.

Einfach und sauber.

+0

er wahrscheinlich verwendet FOS-Bundle, die Sicherheitsereignis verwenden, um Login-Check zu tun. – anru

Verwandte Themen