简体   繁体   English

如何使用symfony 4创建登录身份验证表单

[英]How to create a login authentification form with symfony 4

Firstly I'm sorry to ask that kind of questions here but the symfony documentation doesn't provide too much complete example if you never been a symfony project before. 首先,很抱歉在这里提出这样的问题,但是,如果您以前从未做过symfony项目,则symfony文档并没有提供太多完整的示例。

So I already installed the symfony/security package and i began like in this tutorial https://symfony.com/doc/current/security/form_login_setup.html 所以我已经安装了symfony / security软件包,并且像本教程中一样开始https://symfony.com/doc/current/security/form_login_setup.html

Packages/security.yaml 包/security.yaml

security:
    providers:
        users:
            entity:
                class: Entity:Users
    firewalls:
        main:
            anonymous: ~
            form_login:
                login_path: login
                check_path: login

Login_path and check_path are the road use by my security controller, but what is the difference between both of them ? Login_path和check_path是我的安全控制器使用的路,但是两者之间有什么区别?

I don't know how i should configure my Entity::Users like which one 我不知道该如何配置Entity :: User喜欢哪一个

https://symfony.com/doc/current/security.html#security-user-providers https://symfony.com/doc/current/doctrine/registration_form.html https://symfony.com/doc/current/security.html#security-user-providers https://symfony.com/doc/current/doctrine/registration_form.html

And the biggest thing that i'm never able to check my the login by myself (I guess that the security should use a specifical users implementations but I'm puzzled :( ) 而且最大的事情是我永远无法自己检查登录名(我猜想安全性应该使用特定的用户实现,但是我很困惑:()

This is my road 这是我的路

config/routes.yaml config / routes.yaml

login:
    path: /
    controller: App\Controller\SecurityController::login

logged:
    path: /
    controller: App\Controller\SecurityController::logged

My security controller 我的安全控制器

src/Controller/SecurityController.php src / Controller / SecurityController.php

<?php  // src/Controller/SecurityController.php

namespace App\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;


class SecurityController extends Controller
{

    public function logged(EntityManagerInterface $em, Request $request, AuthenticationUtils $authUtils) 
    {

        error_log(".OMG.");


        return $this->render('security/logged.html.twig', array(
            'username' => $username,
            'password' => $password,
        ));
    }

    public function login(Request $request, AuthenticationUtils $authUtils)
    {
        error_log(".Login.");
        $username = $request->get('_username');
        $password = $request->get('_password');

    // get the login error if there is one
        $error = $authUtils->getLastAuthenticationError();

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

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

}    

And the template twig that i'm calling inside it 还有我在其中调用的模板树枝

templates/security/login.html.twig templates / security / login.html.twig

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="{{ asset('css/login.css') }}" rel="stylesheet">
</head>

<body>

    <div class="container">


        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>


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

        <form action="{{ path('logged') }}" method="post" class="form-signin">
            <h2 class="form-signin-heading">Please sign in</h2>

            <label for="username" class="sr-only">Username:</label>
            <input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" required autofocus/>

            <label for="password" class="sr-only">Password:</label>
            <input type="password" id="password" name="_password"  class="form-control" placeholder="Password" required/>

            <div class="checkbox">
                <label>
                    <input type="checkbox" value="remember-me"> Remember me
                </label>
            </div>

            {#
                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" />
                #}

            <button type="submit">login</button>
        </form>


    </div> <!-- /container -->
</body>
</html>

The problem here is that I'm trying to call my SecurityController::logged() when I use the form action {{ path('logged') }} but whatever happen i'm never printing ".OMG." 这里的问题是,当我使用表单动作{{path('logged')}}时,我试图调用SecurityController :: logged(),但是无论发生什么,我都不会打印“ .OMG”。 and I'm always printing the ".Login.". 并且我一直在打印“ .Login”。

My goal is just to provide a nice authentification user form... Someone have an advice, an answer to one of my questions ? 我的目标只是提供一个不错的身份验证用户表单...有人提出建议,我的一个问题的答案?

Or even an exemple for doing a easy one but where we can see the ORM/Users the Packages/security, the config/routes, the Controller/SecurityController and the twig file in the same tutorial ? 甚至是一个简单的例子,但在同一教程中我们可以在哪里看到ORM /用户的程序包/安全性,配置/路由,Controller / SecurityController和树枝文件?

Thank you very much for read all of that btw ! 非常感谢您阅读所有这些内容!

You have a lot of questions in one post. 您在一个帖子中有很多问题。 Probably you could create several posts with each question. 可能您可以为每个问题创建多个帖子。

check_path is the post URL for login which is handled by FOS bundle. check_path是登录的URL,由FOS软件包处理。 I would keep it something different than login to avoid confusion. 为了避免混淆,我将使其与登录有所不同。

You have listed your providers but the provider is not mentioned in your login method. 您已列出了提供程序,但登录方法中未提及该提供程序。

Try following code and see if the login works. 尝试使用以下代码,查看登录是否有效。

security:
    providers:
        users:
            entity:
                class: Entity:Users
    firewalls:
        main:
            anonymous: ~
            form_login:
                provider: users
                login_path: login
                check_path: login_check
                post_only:  true
                default_target_path: logged

Also change the post url in your form with {{ path('login_check') }} 还可以使用{{path('login_check')}}更改表单中的帖子网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM