[英]Login with Symfony3
我的安全.yml
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
my_entity_provider:
entity:
class: AppBundle:User
property: email
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
provider: my_entity_provider
anonymous: ~
form_login:
username_parameter: _email
login_path: login
check_path: login
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
#http_basic: ~
# https://symfony.com/doc/current/security/form_login_setup.html
#form_login: ~
我的控制器:
class SecurityController extends Controller{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request, AuthenticationUtils $authUtils)
{
// get the login error if there is one
$error = $authUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authUtils->getLastUsername();
return $this->render('AppBundle:Security:login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
我的树枝:
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<label for="email">Email:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit">login</button>
</form>
所以我尝试使用电子邮件和密码登录,但我总是得到不好的凭据。 有人可以告诉我我做错了什么吗?
您的 username_parameter 与您树枝的输入字段不对应。
还要检查https://symfony.com/doc/current/reference/configuration/security.html#username-parameter
如果您遵循此文档检查,则您也在查询中使用电子邮件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.