【发布时间】: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