【问题标题】:How to change the authentication error message?如何更改身份验证错误消息?
【发布时间】:2020-08-22 04:03:24
【问题描述】:

当用户尝试使用错误的用户名/密码登录时,会出现错误消息“凭据无效”!

如何更改getLastAuthenticationError() 错误信息!

例如! 而不是“无效凭据”,我希望它是“错误消息 123”

控制器代码:

/**
 * @Route("/login",name="security_login")
 */
public function login(AuthenticationUtils  $authenticationUtils)
{
     // get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();


// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();

    return $this->render('123/login.html.twig',[
        'lastUsername'=>$lastUsername,
        'error' => $error]);
}

security.yaml 代码

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: null }
        in_database:
            entity: 
                class: App\Entity\User  
                property: username
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: lazy
            provider: in_database
            form_login: 
                login_path: security_login
                check_path: security_login

            logout:
                path:   security_logout
                target: user_index

视图代码:

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path('security_login') }}" method="post">
    <div class="form-group">
        <input placeholder="username" requied name="_username" value ="{{lastUsername}}"
         type="text" class="form-control">
    </div>
    <div class="form-group">
        <input placeholder="Mode de passe..." requied name="_password"
         type="password" class="form-control">
    </div>
     <div class="form-group">
       <button type="submit" class="btn btn-success">Connexion</button>
    </div>
</form>

【问题讨论】:

    标签: php symfony authentication twig


    【解决方案1】:

    您可以使用自定义翻译或构建自己的身份验证器,并在其中抛出 CustomUserMessageAuthenticationException

    要翻译消息,您可以查看the original translation file 以供参考。在您的翻译/文件夹中,只需添加一个security.en.yaml,然后您就可以覆盖该消息:

    # translations/security.en.yaml
    'Invalid credentials.': 'Your username or password are invalid.'
    

    对于CustomUserMessageException,请查看How to Build a Login Form 上的文档页面。

    第 3 步展示了如何编写GuardAuthenticator,还展示了如何使用自定义异常:

    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(['email' => $credentials['email']]);
        if (!$user) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('Email could not be found.');
        }
        return $user;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2016-01-20
      • 1970-01-01
      相关资源
      最近更新 更多