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

Posted

技术标签:

【中文标题】如何使用 symfony 4 创建登录验证表单【英文标题】:How to create a login authentification form with symfony 4 【发布时间】:2018-06-27 08:25:37 【问题描述】:

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

所以 我已经安装了 symfony/security 包,我从本教程开始 https://symfony.com/doc/current/security/form_login_setup.html

包/security.yaml

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

Login_path 和 check_path 是我的安全控制器使用的道路,但它们有什么区别?

我不知道我应该如何配置我的 Entity::Users 喜欢哪个

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

最重要的是,我永远无法自己检查我的登录信息 (我想安全应该使用特定的用户实现,但我很困惑:()

这是我的路

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

    

还有我在里面调用的模板树枝

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>

这里的问题是,当我使用表单操作 path('logged') 时,我试图调用我的 SecurityController::logged() 但无论发生什么,我都不会打印“.OMG”。而且我总是打印“.Login”。

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

或者甚至是一个简单的例子,但是我们可以在同一个教程中看到 ORM/Users、Packages/security、config/routes、Controller/SecurityController 和 twig 文件?

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

【问题讨论】:

这么多问题。从第一个开始。 login_path 是一个 GET 请求,用于显示您的登录表单。安全系统使用它在需要时自动重定向未登录的用户。 check_login 是一个 POST 请求。这是登录表单应该发布到的内容。 S 安全侦听器处理此以登录用户,因此您将没有控制器。 谢谢!我找到了一种肮脏的方式来做我正在寻找的事情,我使用“http_basic”而不是“form_login”,并且我在安全控制器中进行了检查。如果我们不应该有一个安全控制器,为什么在我让的第一个文档链接中要求?我想与 FOS::UserBundles 建立适当的安全连接更容易:( 正是我处于类似情况,您必须找到分成几部分的文档并自己组合它们。任何试图从文档中理解的初学者都会很容易被推迟。 blog.dev-web.io/2017/12/16/… 如果您将它与翻译一起使用,这可能会对您有所帮助! 【参考方案1】:

您在一篇文章中有很多问题。可能您可以针对每个问题创建多个帖子。

check_path 是由 FOS 包处理的登录后 URL。我会保留与登录不同的内容以避免混淆。

您已列出您的提供商,但您的登录方法中未提及该提供商。

尝试以下代码,看看登录是否有效。

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')

更改表单中的帖子网址

【讨论】:

以上是关于如何使用 symfony 4 创建登录验证表单的主要内容,如果未能解决你的问题,请参考以下文章

使用 Auth0 和 PHP 创建后登录用户

Symfony 5 登录表单操作返回无效的 CSRF 令牌

Symfony 验证仅在新表单上

带有新身份验证方法的 Symfony 简单登录表单不起作用

如何以编程方式登录/验证用户?

Symfony 4:如何在防火墙中为用户/管理员提供多个提供者?