简体   繁体   中英

Symfony2 - Show bad credentials error

I have been writing a custom auth provider in Symfony2. Everything works so far, but when I enter a wrong password a get an Internal Server error displaying: "LDAP authentication failed".

Now, this is the message that I want to display, but I'd like to display it above my login form and NOT throw an internal server error. In my listener, I have the following:

try {
        $authToken= $this->authenticationManager->authenticate($token);
        $this->securityContext->setToken($authToken);

        return;
} catch (AuthenticationException $failed) {
        throw new BadCredentialsException($failed->getMessage(), 0);
}

So is there anyone who can tell me what I need to do to show the user a message, instead of throwing an internal server error?

Thanks in advance.

You can manually add error to your login form in the controller. For example:

$form->get('username')
    ->addError(new FormError($message));

You should have a controler action that catch the same path of your form to catch the exception.

authentication process will be tryed before the form will be displayed.

You can see this example : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/SecurityController.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM