2016-11-16 97 views
0

我有一個Lexik捆綁的UserProvider,並檢查用戶是否通過會話存在,但是當我做出某些請求時會出現問題,會話失去了人們知道的值,因爲發生這種情況。Symfony在服務中丟失會話

服務

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 /會話/ dev的文件夾中有2 Phpsession幾乎相同的會話寄存器。

enter image description here

更新1

重要信息這只是發生在WebKit瀏覽器

回答

0

請在您ApiBundle\Security\Userprovidersession組件。

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

} 
+0

我已經加入抱歉,我沒有進入的問題 – user26776

+0

ok了進口。我有更新答案請現在就試試。 –

+0

謝謝,但它不起作用 – user26776