Ich habe das Lernprogramm auf der Symfony-Website (http://symfony.com/doc/current/security.html) verfolgt, um eine Anmeldung für mein Symfony 3.1-Projekt hinzuzufügen. Es funktioniert gut mit der HTTP-Basic-Authentifizierung, aber wenn ich form_login mit dem Tutorial hier (http://symfony.com/doc/current/security/form_login_setup.html) hinzufügen, hält es zur Anmeldeseite umgeleitet und nicht in der AnmeldungAnmeldung mit einem Anmeldeformular in Symfony 3.1 nicht möglich
Hier ist meine security.yml.
security:
providers:
in_memory:
memory:
users:
ryan:
password: ryanpass
roles: 'ROLE_USER'
admin:
password: kitten
roles: 'ROLE_ADMIN'
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
login_firewall:
anonymous: ~
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
provider: in_memory
form_login:
login_path: /login
check_path: /login_check
logout: ~
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
Security Controller:
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction(Request $request) {
;
}
login.html.twig:
{% extends 'base.html.twig' %}
{% block body %}
<div id="login_form">
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
<p><label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" /></p>
<p><label for="password">Password:</label>
<input type="password" id="password" name="_password" /></p>
{#
If you want to control the URL the user
is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<p><button type="submit">login</button></p>
</form>
</div>
{% endblock %}
Das Lernprogramm fließt nicht so gut, wie es vielleicht sollte. Deine Firewall-Definitionen sind vermasselt. Siehe hier: http://stackoverflow.com/questions/38767102/symfony-login-for-#comment64906068_38767102 – Cerad
Ja, sie scheinen ein bisschen übermäßig komplex und chaotisch. Sieht so aus, als ob man die Encoder über die Provider setzt und den Anon für/Login im Bereich Firewalls entfernt – Furison