Symfony3.1:传统登录表单不起作用

时间:2016-12-15 10:26:42

标签: login firewall symfony-3.1

我已经通过官方的symfony文档创建了一个登录表单:http://symfony.com/doc/3.1/security/form_login_setup.html

显示表单登录,但是当我尝试连接有效用户时,我什么都没有:没有错误,没有重定向......表单再次显示,没有消息!

这是我的security.yml:

security:
providers:
    in_memory:
        memory: 
           users:
               superman:
                   password: clark
                   roles: ROLE_PRD, ROLE_CAT, ROLE_BATMAN
               batman:
                   password: bruce
                   roles: ROLE_CAT, ROLE_BATMAN
               test:
                   password: test
                   roles: ROLE_CAT, ROLE_BATMAN
encoders:
    Symfony\Component\Security\Core\User\User: plaintext
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    autre:
        pattern: ^/catalogue/login
        security: false
    main:
        provider: in_memory
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
            #default_target_path: /catalogue/admin/
        pattern: ^/catalogue/admin
access_control:
    - { path: ^/catalogue/admin/produit/$, roles: ROLE_BATMAN}
    - { path: ^/catalogue/admin/produit, roles: ROLE_PRD}
    - { path: ^/catalogue/admin/categorie, roles: ROLE_CAT}

这是我的控制器SecurityController.php:

<?php

namespace CatalogueBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


use Symfony\Component\HttpFoundation\Response;

class SecurityController extends Controller
{

    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 new Response("<html> <body> SecuriteController </body>  </html>");

        return $this->render('securite/login.html.twig', array(
            'last_username' => $lastUsername,
            'error'         => $error,
        ));
    }
}

最后的观点:

{% extends 'base.html.twig' %}
{% block body %}

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

<form action="{{ path('login') }}" method="post">
    <label for="username">Username:</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>

{% endblock %}    

路由文件:

login:
  path:     /catalogue/login
  defaults: { _controller: CatalogueBundle:Security:login }

似乎symfony安全系统在提交时没有捕获我的表单数据,我在日志中没有任何内容!

1 个答案:

答案 0 :(得分:0)

我认为你这里只有一个问题。 为了帮助您继续项目,

  1. 我无法看到您在成功时重定向用户的位置(在您的security.yml中有评论。) 您可以做的是在登录后在“dev”导航栏中看到您的用户不再是anonymoys。

  2. 我认为你的security.yml有问题 url'/ catalog / login'将绑定到您的'autre'防火墙,您没有精确确定您的表单登录信息。 由于我的项目中没有更多信息,您可以删除(或评论)您的“autre”防火墙,只需在您的访问控制中添加您的登录路线,并匿名接受...... < / p>

  3. 尝试使用这些信息做某事,如果遇到困难,请回来......

    [编辑] 也许您必须在'autre'防火墙中移动'form_login:'部分,不要忘记您的提供商......

相关问题