首先,我尝试使用in_memory提供程序的简单方法,就像在本文档中一样:https://symfony.com/doc/current/security.html它对我有用,然后我继续学习本教程:https://symfony.com/doc/current/security/entity_provider.html并最终无休止地结束了浏览器http基本用户数据请求循环。
这是我的代码,也许有人可以找到微小的缺失分号:D
网址:https://gitlab.com/AceVik/ajoli
Neccessery文件。 security.yml
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
our_db_provider:
entity:
class: App\Entity\User
# property: username
#in_memory:
# memory:
# users:
# admin:
# password: admin
# roles: 'ROLE_ADMIN'
firewalls:
#secured_area:
# logout:
# path: /logout
# target: /
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
# pattern: ^/
http_basic: ~
provider: our_db_provider
# provider: in_memory
encoders:
App\Entity\User: plaintext
# algorithm: bcrypt
# cost: 12
#Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# 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: ^/admin, roles: ROLE_ADMIN }
- { path: ^/profile, roles: ROLE_USER }
user.php的
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=254, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
public function getUsername()
{
return $this->username;
}
public function setUsername($username) {
$this->username = $username;
$this->email = $username . '@example.com';
}
public function setPassword($password) {
$this->password = $password;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, ['allowed_classes' => false]);
}
}
UserRepository.php
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
public function loadUserByUsername($username)
{
return $this->createQueryBuilder('u')
->where('u.username = :username')
->setParameter('username', $username)
->getQuery()
->getOneOrNullResult();
}
}
答案 0 :(得分:2)
尤,
您的图片似乎有问题。我装了我自己的码头图像,它为我工作。
要进行调试,您可以打开 BasicAuthenticationListener (在供应商目录中搜索它)。并在} catch(AuthenticationException $ e){上设置断点 或者在其下添加退出(var_dump($ e)); 以检查抛出的异常。
答案 1 :(得分:0)
找到解决方案。
我只是将Symfony从4.0.9更新到4.0.11,它解决了这个问题。看起来,这是一个Symfony错误:https://symfony.com/blog/symfony-4-0-11-released
现在我有一个登出问题:D我试着通过我自己解决它,但如果有人看到错误,请告诉我。我的gitlab存储库仍然是公开的。