我有一个针对Lexik软件包的UserProvider,并检查用户是否通过会话存在,但是当我发出特定请求时会出现问题,会话失去了有人知道的值,因为这种情况发生了。
服务
app.user_provider:
class: ApiBundle\Security\Userprovider
arguments: ["@session","@switchconnection","@doctrine.orm.entity_manager" , "@doctrine.dbal.default_connection" , "@doctrine"]
我的提供者
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use ApiBundle\Entity\Utilizador;
use Symfony\Component\HttpFoundation\Session\Session;
public function __construct(Session $session,$switchconnection ,\Doctrine\ORM\EntityManager $em , \Doctrine\DBAL\Connection $dbalConnection , \Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->session = $session;
$this->switchconnection = $switchconnection;
$this->em = $em;
$this->connection = $dbalConnection;
$this->doctrine = $doctrine;
}
public function loadUserByUsername($username)
{
if($this->session->get("currentuser") == $username){
$this->switchconnection->switchDatabase($this->session->get("dbconnection"), $this->connection , $this->doctrine);
$conn = $this->em->getConnection();
$stmt = $conn->prepare(".....");
$stmt->bindParam(1, $username);
$stmt->execute();
$results = $stmt->fetchAll();
$count = count($results);
if($count != 0)
{
$user= new Utilizador();
$user->setUsername($results[0]['username']);
$user->setEmail($results[0]['email']);
return $user;
}
}
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username) );
}
我还检查过var / session / dev文件夹中有一个会话寄存器,其中2个Phpsession几乎相同。
更新1
重要信息仅在webkit浏览器中发生
答案 0 :(得分:0)
请在session
。
ApiBundle\Security\Userprovider
个组件
use Symfony\Component\HttpFoundation\Session\Session;
我想你忘了添加会话组件。
<强>更新-1 强>
用户service_container
将其传递给服务人员。对我来说它的工作正常。
<强> services.yml 强>
app.user_provider:
class: ApiBundle\Security\Userprovider
arguments: ["@service_container", "@session","@switchconnection","@doctrine.orm.entity_manager" , "@doctrine.dbal.default_connection" , "@doctrine"]
<强>提供商强>
protected $container;
protected $session;
public function __construct($container, Session $session,$switchconnection ,\Doctrine\ORM\EntityManager $em , \Doctrine\DBAL\Connection $dbalConnection , \Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->container = $container;
$this->session = $container->get('session');
}