Symfony 6 认证

问题描述 投票:0回答:1

大家早上好 请我阻止身份验证。调试日志: 我的调试日志

这是我的实体代码

class User implements UserInterface, \Serializable,PasswordAuthenticatedUserInterface,PasswordHasherAwareInterface

{ /** * @varint * * @ORM\Id * @ORM\GenerateValue * @ORM\Column(类型=“整数”) */ 私人 $id;

/**
 * @var string
 *
 * @ORM\Column(type="string", nullable=true)
 */
private $fullName;

/**
 * @var
 *
 * @ORM\Column(name="gender", type="string", length=10, nullable=true)
 */
protected $gender;

/**
 * @var
 *
 * @ORM\Column(name="firstname", type="string", length=50, nullable=true)
 */
protected $firstname;

/**
 * @var
 *
 * @ORM\Column(name="lastname", type="string", length=50, nullable=true)
 * @Assert\NotBlank()
 */
protected $lastname;

/**
 * @var string
 *
 * @ORM\Column(type="string", unique=true)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(type="string", unique=true)
 * @Assert\NotBlank()
 * @Assert\Email()
 */
private $email;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=64)
 */
private $password;


/**
 * @var
 *
 * @ORM\Column(name="notes", type="text", nullable=true)
 */
protected $notes;

/**
 * @var
 *
 * @ORM\Column(name="locked", type="boolean")
 */
protected $locked = false;


/**
 * @var array
 *
 * @ORM\Column(name="roles", type="json")
 */
private $roles = [];

/**
 * @var \DateTime
 * 
 * @ORM\Column(name="lastLogin", type="datetime", nullable=true)
 */
protected $lastLogin;

/**
 * Random string sent to the user email address in order to verify it.
 *
 * @var string
 * 
 * @ORM\Column(name="confirmationToken", type="string", length=255, nullable=true)
 */
protected $confirmationToken;

/**
 * @var \DateTime
 * 
 * @ORM\Column(name="passwordRequestedAt", type="datetime", nullable=true)
 */
protected $passwordRequestedAt;

/**
 * @var
 *
 * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
 * @ORM\JoinTable(name="sf_user_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $group;

/**
 * @ORM\Column(type="date", nullable=true)
 */
private $birth_date;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $blood_group;

/**
 * @ORM\Column(type="string", length=255)
 */
private $address;

/**
 * @ORM\Column(type="string", length=255)
 */
private $phone;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $license;

/**
 * @ORM\ManyToOne(targetEntity=Specialty::class, inversedBy="users")
 */
private $speciality;

/**
 * @ORM\OneToMany(targetEntity=Schedule::class, mappedBy="user")
 */
private $schedules;


/**
 * User constructor.
 */
public function __construct()
{
    $this->locked = false;
    $this->roles = array();
    $this->groups = new ArrayCollection();
    $this->schedules = new ArrayCollection();
}


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


public function setFullName(string $fullName): void
{
    $this->fullName = $fullName;
}

// le ? signifie que cela peut aussi retourner null
public function getFullName() 
{
    return $this->fullName;
}

public function getUsername() 
{
    return $this->username;
}

public function setUsername(string $username): void
{
    $this->username = $username;
}

public function getGender() 
{
    return $this->gender;
}

public function setGender(string $gender): void
{
    $this->gender = $gender;
}    

public function getNotes() 
{
    return $this->notes;
}

public function setNotes(string $notes): void
{
    $this->notes = $notes;
}

public function getEmail(): ?string
{
    return $this->email;
}

public function setEmail(?string $email): void
{
    $this->email = $email;
}

public function getPassword(): ?string
{
    return $this->password;
}

public function setPassword($password = null): void
{
    $this->password = $password;
}

public function getFirstname(): ?string
{
    return $this->firstname;
}

public function setFirstname(string $firstname): void
{
    $this->firstname = $firstname;
}

public function getLastname(): ?string
{
    return $this->lastname;
}

public function setLastname(string $lastname): void
{
    $this->lastname = $lastname;
}

public function getLocked(): ?bool
{
    return $this->locked;
}

public function setLocked(bool $locked): void
{
    $this->locked = $locked;
}

/**
 * Retourne les rôles de l'user
 */
public function getRoles(): array
{
    $roles = $this->roles;

    // Afin d'être sûr qu'un user a toujours au moins 1 rôle
    if (empty($roles)) {
        $roles[] = 'ROLE_USER';
    }

    foreach ($this->getGroups() as $group) {
        $roles = array_merge($roles, $group->getRoles());
    }
   // die(var_dump($roles, array_unique($roles)));
    return array_unique($roles);
}

public function setRoles(array $roles): void
{
    $this->roles = $roles;
}

/**
 * Retour le salt qui a servi à coder le mot de passe
 *
 * {@inheritdoc}
 */
public function getSalt(): ?string
{
    // See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html
    // we're using bcrypt in security.yml to encode the password, so
    // the salt value is built-in and you don't have to generate one

    return null;
}

/**
 * Removes sensitive data from the user.
 *
 * {@inheritdoc}
 */
public function eraseCredentials(): void
{
    // Nous n'avons pas besoin de cette methode car nous n'utilions pas de plainPassword
    // Mais elle est obligatoire car comprise dans l'interface UserInterface
    // $this->plainPassword = null;
}

/**
 * {@inheritdoc}
 */
public function serialize(): string
{
    return serialize([$this->id, $this->username, $this->password, $this->locked]);
}

/**
 * {@inheritdoc}
 */
public function unserialize($serialized): void
{
    [$this->id, $this->username, $this->password, $this->locked] = unserialize($serialized);
}

/**
 * {@inheritdoc}
 */
public function setLastLogin(\DateTime $time = null)
{
    $this->lastLogin = $time;

    return $this;
}

/**
 * {@inheritdoc}
 */
public function setConfirmationToken($confirmationToken)
{
    $this->confirmationToken = $confirmationToken;

    return $this;
}

/**
 * {@inheritdoc}
 */
public function setPasswordRequestedAt(\DateTime $date = null)
{
    $this->passwordRequestedAt = $date;

    return $this;
}

/**
 * Gets the last login time.
 *
 * @return \DateTime
 */
public function getLastLogin()
{
    return $this->lastLogin;
}

/**
 * {@inheritdoc}
 */
public function getConfirmationToken()
{
    return $this->confirmationToken;
}

 /**
 * Gets the timestamp that the user requested a password reset.
 *
 * @return null|\DateTime
 */
public function getPasswordRequestedAt()
{
    return $this->passwordRequestedAt;
}

/**
 * {@inheritdoc}
 */
public function isPasswordRequestNonExpired($ttl)
{
    return $this->getPasswordRequestedAt() instanceof \DateTime &&
           $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
}

/**
 * @param $group
 * @return $this
 */
public function addGroup($group)
{
    $this->groups[] = $group;

// $group->addUser($this);

    return $this;
}


/**
 * @param $groups
 */

public function setGroup($group = null)
{
    $this->addGroup($group);
}


/**
 * @param $groups
 */

public function clearGroups()
{
    $this->groups->clear();
}
/**
 * @param $groups
 */

 /**
 * @param $groups
 */

public function setGroups(ArrayCollection $groups = null)
{
    if ($groups !== null) {
        $this->groups->clear();
        foreach ($groups as $group) {
            $this->addGroup($group);
        }
    }
}

/**
 * @return ArrayCollection
 */
public function getGroups()
{
    return $this->groups;
}

public function hasRole($role) {
    foreach($this->getGroups() as $grp) {
        if($grp->hasRole($role)) {
            return true;
        }
    }
    return false;
}

/**
 * Get group
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getGroup()
{
    return $this->group;
}

public function hasGroup($name = '')
{
    return in_array($name, $this->getGroupNames());
}

public function containGroup(Group $group)
{
    return $this->groups->contains($group);
}

public function __toString() {
    return $this->getUsername().' - '.$this->getFirstname().' '.$this->getLastname();
}

public function removeGroup(Group $group): self
{
    if ($this->groups->contains($group)) {
        $this->groups->removeElement($group);
    }

    return $this;
}

public function getUserIdentifier(): string 
{
    return (string)$this->email;
}

public function getPasswordHasherName(): ?string
{
    return null; // use the default hasher
}

public function getBirthDate(): ?\DateTimeInterface
{
    return $this->birth_date;
}

public function setBirthDate(?\DateTimeInterface $birth_date): self
{
    $this->birth_date = $birth_date;

    return $this;
}

public function getBloodGroup(): ?string
{
    return $this->blood_group;
}

public function setBloodGroup(?string $blood_group): self
{
    $this->blood_group = $blood_group;

    return $this;
}

public function getAddress(): ?string
{
    return $this->address;
}

public function setAddress(string $address): self
{
    $this->address = $address;

    return $this;
}

public function getPhone(): ?string
{
    return $this->phone;
}

public function setPhone(string $phone): self
{
    $this->phone = $phone;

    return $this;
}

安全.yaml

security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
    App\Entity\Security\User: 'auto'
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
    main:
        entity: 
            class: App\Entity\Security\User
            property: email 
firewalls:
    main:
        provider: main
        pattern: "^/gestion"
        form_login:
            provider: main
            login_path: adminlogin
            check_path: adminlogin
            default_target_path: /gestion
            target_path_parameter: go_to
        logout:
            path: adminlogout
            target: adminlogin
            invalidate_session: true
        remember_me:
            secret: "%env(APP_SECRET)%"
            lifetime: 2232000
            path: /gestion
        entry_point: form_login
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

        # activate different ways to authenticate
        # https://symfony.com/doc/current/security.html#the-firewall

        # https://symfony.com/doc/current/security/impersonating_user.html
        # switch_user: true

# 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: ^/gestion/login, roles: PUBLIC_ACCESS }
    - { path: ^/gestion, roles: [IS_AUTHENTICATED_FULLY, ROLE_ADMIN] }

role_hierarchy:
    ROLE_ADMIN: ROLE_PATIENT

当@测试时: 安全: 密码哈希值: 应用程序\实体\安全\用户:'自动' # 默认情况下,密码哈希器是资源密集型的并且需要时间。这是 # 对于生成安全密码哈希很重要。然而,在测试中,安全哈希 # 不重要,浪费资源并增加测试时间。下列 # 将工作系数降低到尽可能低的值。 Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 算法:自动 cost: 4 # bcrypt 的最低可能值 time_cost: 3 # 氩气的最低可能值 memory_cost: 10 # 氩气的最低可能值

每次我尝试登录时,在访问被拒绝后我都会收到身份验证成功 在此输入代码

php symfony
1个回答
0
投票

您已成功通过身份验证,但无法访问所需的网址,可能的原因是您的访问控制,请验证用于连接的用户的角色。

© www.soinside.com 2019 - 2024. All rights reserved.