如何使用symfony 4创建登录身份验证表单 [英] How to create a login authentification form with symfony 4

查看:76
本文介绍了如何使用symfony 4创建登录身份验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我很抱歉在这里问这种问题,但是,如果您以前从未做过symfony项目,symfony文档并没有提供太多完整的示例。

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 / security软件包,我就像本教程中一样开始
https://symfony.com/doc/current/security/form_login_setup.html

Packages / security.yaml

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

Login_path和check_path是我的安全控制器使用的道路,但是两者之间有什么区别?

Login_path and check_path are the road use by my security controller, but what is the difference between both of them ?

我不知道该如何配置Entity :: User喜欢哪个

I don't know how i should configure my Entity::Users like which one

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

最大的事情是我永远无法自己检查登录信息
(我想安全性应该使用特定的用户实现,但我m不解:()

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 :( )

这是我的路

config / routes.yaml >

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

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

我的安全控制器

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,
        ));
    }

}    

m在其中调用

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>

这里的问题是我在使用时试图调用SecurityController :: logged()表单操作{{path('logged')}},但是无论发生什么,我都不会打印 .OMG。我一直在打印 .Login。

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." and I'm always printing the ".Login.".

我的目标只是提供一个不错的身份验证用户表单...有人建议,一个答案我的问题之一?

My goal is just to provide a nice authentification user form... Someone have an advice, an answer to one of my questions ?

甚至是一个简单的例子,但在这里我们可以看到ORM /用户的程序包/安全性,配置/路由,Controller / SecurityController和树枝文件在同一教程中?

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 ?

非常感谢您阅读所有这些内容!

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是登录的URL,由FOS软件包处理。为了避免混淆,我将使其与登录有所不同。

check_path is the post URL for login which is handled by FOS bundle. 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

也可以通过{{path('login_check')}}}

Also change the post url in your form with {{ path('login_check') }}

这篇关于如何使用symfony 4创建登录身份验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆