通过连接耗尽内存

时间:2019-04-14 23:35:44

标签: symfony4

(首先:对不起,我的英语)如果我提交的连接表单的凭据不正确,则会出现预期的错误“凭据无效”。 。但是,如果我输入正确的内容,则网页将继续旋转,并且我的计算机用尽了内存并崩溃,因此我必须重新启动它。 请提供任何帮助,因为我没有收到任何错误消息,女巫可以帮助我弄清楚发生了什么。

这是我的security.yaml文件

security:
  encoders:
    App\Entity\User: bcrypt
  # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
  providers:
    database_users:
      entity: { class: App\Entity\User, property: username }
  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    main:
      anonymous: true
      pattern: ^/

      # activate different ways to authenticate

      #            http_basic: ~
      # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

      form_login:
        check_path: security_login
        login_path: security_login
        csrf_token_generator: security.csrf.token_manager
        default_target_path: micro_post_index
      #                always_use_default_target_path: false
      #                use_referer: true
      #                failure_path:
      #                username_parameter: _username
      #                password_parameter: _password
      #                csrf_parameter: _
      logout:
        path: security_logout
        target: micro_post_index
      # https://symfony.com/doc/current/security/form_login_setup.html

  # Easy way to control access for large sections of your site
  # Note: Only the *first* access control that matches will be used
  access_control:
  #         - { path: ^/micro-post, roles: ROLE_USER }
  # - { path: ^/profile, roles: ROLE_USER }

-这是securiyController:

    namespace App\Controller;

    use App\Repository\UserRepository;
    use Doctrine\ORM\EntityManagerInterface;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

    class SecurityController
    {
        /**
         * @var \Twig_Environment
         */
        private $twig;

        public function __construct(\Twig_Environment $twig)
        {
            $this->twig = $twig;
        }

        /**
         * @Route("/login", name="security_login")
         */
        public function login(AuthenticationUtils $authenticationUtils)
        {
            return new Response(
                $this->twig->render(
                    'security/login.html.twig',
                    [
                        'last_username' => $authenticationUtils->getLastUsername(),
                        'error' => $authenticationUtils->getLastAuthenticationError(
                        ),
                    ]
                )
            );
        }

        /**
         * @Route("/logout", name="security_logout")
         */
        public function logout()
        {

        }

        /**
         * @Route("/confirm/{token}", name="security_confirm")
         */
        public function confirm(
            string $token,
            UserRepository $userRepository,
            EntityManagerInterface $entityManager
        ) {
            $user = $userRepository->findOneBy(
                [
                    'confirmationToken' => $token,
                ]
            );

            if (null !== $user) {
                $user->setEnabled(true);
                $user->setConfirmationToken('');

                $entityManager->flush();
            }

            return new Response(
                $this->twig->render(
                    'security/confirmation.html.twig',
                    [
                        'user' => $user,
                    ]
                )
            );
        }
    }

这是用户实体:         

    namespace App\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Serializable;
    use Symfony\Component\Security\Core\User\UserInterface;

    /**
     * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
     * @ORM\AttributeOverrides({
     *      @ORM\AttributeOverride(name="username",
     *          column=@ORM\Column(
     *              name     = "username",
     *              length   = 191,
     *              unique   = true
     *          )
     *      ),
     *      @ORM\AttributeOverride(name="email",
     *          column=@ORM\Column(
     *              name     = "email",
     *              length   = 191,
     *              unique   = true
     *          )
     *      )
     * })
     */
    class User implements UserInterface, Serializable
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;
        /**
         * @ORM\Column(type="string", length=50, unique=true)
         */
        private $username;
        /**
         * @ORM\Column(type="string")
         */
        private $password;
        /**
         * @ORM\Column(type="string", length=254, unique=true)
         */
        private $email;
        /**
         * @ORM\Column(type="string", length=50
         *     )
         */
        private $fullName;

        /**
         * @return mixed
         */
        public function getEmail()
        {
            return $this->email;
        }

        /**
         * @param mixed $email
         */
        public function setEmail($email): void
        {
            $this->email = $email;
        }

        /**
         * @return mixed
         */
        public function getFullName()
        {
            return $this->fullName;
        }

        /**
         * @param mixed $fullName
         */
        public function setFullName($fullName): void
        {
            $this->fullName = $fullName;
        }

        public function getId(): ?int
        {
            return $this->id;
        }

        /**
         * Returns the roles granted to the user.
         *
         *     public function getRoles()
         *     {
         *         return ['ROLE_USER'];
         *     }
         *
         * Alternatively, the roles might be stored on a ``roles`` property,
         * and populated in any number of different ways when the user object
         * is created.
         *
         * @return (Role|string)[] The user roles
         */
        public function getRoles()
        {
            return [
                'ROLE_USER'
            ];
        }

        /**
         * Returns the password used to authenticate the user.
         *
         * This should be the encoded password. On authentication, a plain-text
         * password will be salted, encoded, and then compared to this value.
         *
         * @return string The password
         */
        public function getPassword()
        {
            return $this->password;
        }

        /**
         * @param mixed $password
         */
        public function setPassword($password): void
        {
            $this->password = $password;
        }

        /**
         * Returns the salt that was originally used to encode the password.
         *
         * This can return null if the password was not encoded using a salt.
         *
         * @return string|null The salt
         */
        public function getSalt()
        {
            return null;
        }

        /**
         * Returns the username used to authenticate the user.
         *
         * @return string The username
         */
        public function getUsername()
        {
            return $this->username;
        }

        /**
         * @param mixed $username
         */
        public function setUsername($username): void
        {
            $this->username = $username;
        }

        /**
         * Removes sensitive data from the user.
         *
         * This is important if, at any given point, sensitive information like
         * the plain-text password is stored on this object.
         */
        public function eraseCredentials()
        {
            // TODO: Implement eraseCredentials() method.
        }

        /**
         * String representation of object
         * @link https://php.net/manual/en/serializable.serialize.php
         * @return string the string representation of the object or null
         * @since 5.1.0
         */
        public function serialize()
        {
            return $this->serialize([
                $this->id,
                $this->username,
                $this->password
            ]);
        }

        /**
         * Constructs the object
         * @link https://php.net/manual/en/serializable.unserialize.php
         * @param string $serialized <p>
         * The string representation of the object.
         * </p>
         * @return void
         * @since 5.1.0
         */
        public function unserialize($serialized)
        {
            list($this->id,
                $this->username,
                $this->password) = unserialize($serialized);
        }
    }

最后是login.html.twig文件:

    {% extends 'base.html.twig' %}        
    {% block body %}
        {% if error %}
            <div class="alert alert-danger">
                {{ error.messageKey|trans(error.messageData, 'security') }}
            </div>
        {% endif %}

        <form action="{{ path('security_login') }}" method="post">
            <div>
                <div class="form-group">
                    <label class="form-control-label required" for="username">Username</label>
                    <input type="text" id="username" name="_username" required="required" class="form-control"
                           value="{{ last_username }}">
                </div>
                <div class="form-group">
                    <label class="form-control-label required" for="password">Password</label>
                    <input type="password" id="password" name="_password" required="required" class="form-control">
                </div>
                <div class="form-group">
                    <button type="submit" id="Login" name="Login" class="btn-secondary btn">Login</button>
                </div>
                <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
                <input type="hidden" name="_target_path" value="{{ app.request.get('redirect_to') }}">
            </div>
        </form>
    {% endblock %}

1 个答案:

答案 0 :(得分:0)

我只是弄清楚这个错误的根源,在User类中,序列化函数将其称为self,这就是为什么应用程序卡在其中然后崩溃的原因。正确的代码应为:

For string: $.Colors[*].name
Before: $.Colors
Separator: [*]
After: .name

For string: $.Colors[*].Color[*].name
Before: $.Colors
Separator: [*]
After: .Color[*].name

For string: $.Colors[?(@.type == 'Primary')].Color[*].name
Before: $.Colors
Separator: [?(@.type == 'Primary')]
After: .Color[*].name

希望这可以帮助其他人。