2013-07-17 29 views
5

Tôi có nhà cung cấp người dùng tùy chỉnh và thực thể người dùng mà tôi đã sử dụng thành công trong Symfony 2.2. Nhưng bây giờ tôi nâng cấp lên 2.3, và tôi nhận ra chức năng "nhớ tôi" bị hỏng. Vì vậy, tôi đã tạo một ứng dụng sf2 mới và thử nghiệm chức năng. Bài kiểm tra được chuyển khi tôi sử dụng các giá trị mặc định của Acme \ DemoBundle. Nhưng khi tôi thêm nhà cung cấp của tôi, nó lại bắt đầu thất bại. Đây là bài kiểm tra:"Nhớ thông tin đăng nhập của tôi" không thành công khi sử dụng nhà cung cấp người dùng tùy chỉnh, thực thể người dùng, v.v.

<?php 

namespace Acme\DemoBundle\Tests\Controller; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
use Symfony\Component\BrowserKit\Cookie; 

class DemoControllerTest extends WebTestCase 
{ 
    public function testRemember() 
    { 
     $client = static::createClient(); 

     $securedPageUri = '/user/settings/account'; 
     $securedPageFilter = 'html:contains("New Password")'; 
     $loginPageFilter = 'html:contains("Login")'; 
     $username = '[email protected]'; 
     $password = 'test'; 
     /* 
     $securedPageUri = '/demo/secured/hello/World'; 
     $securedPageFilter = 'html:contains("Hello resource secured for admin only.")'; 
     $loginPageFilter = 'html:contains("Login")'; 
     $username = 'admin'; 
     $password = 'adminpass'; 
     */ 

     // Go to Secured page, and be redirected to Login page 
     $client->request('GET', $securedPageUri); 
     $crawler = $client->followRedirect(); 
     $this->assertGreaterThan(0, $crawler->filter($loginPageFilter)->count()); 

     // Try to log in, and be redirected to Secured page 
     $form = $crawler->selectButton('Login')->form(); 
     $form['_username'] = $username; 
     $form['_password'] = $password; 
     $form['_remember_me'] = 1; 
     $client->submit($form); 
     $crawler = $client->followRedirect(); 
     $this->assertGreaterThan(0, $crawler->filter($securedPageFilter)->count()); 

     // Remove all the cookies, but keep the "remember me" cookie 
     $remembermeCookie = $client->getCookieJar()->get('REMEMBERME'); 
     $client->restart(); 
     $client->getCookieJar()->set($remembermeCookie); 

     // Go to Secured page, this time we should be allowed in 
     $client->followRedirects(); 
     $crawler = $client->request('GET', $securedPageUri); 
     //$this->assertTrue($client->getResponse()->isSuccessful()); 
     $this->assertEquals(0, $crawler->filter($loginPageFilter)->count(), "Redirected to Login page"); // THIS IS WHERE THE TEST FAILS 
     $this->assertGreaterThan(0, $crawler->filter($securedPageFilter)->count()); 
    } 
} 

Bài kiểm tra hoạt động tốt, tôi đã thử kiểm tra thủ công: Tôi đăng nhập, xóa cookie phiên và cố gắng truy cập trang được bảo mật bằng ghi nhớ cookie của tôi. Ghi nhớ cookie của tôi bị xóa và tôi được chuyển hướng đến trang đăng nhập: S

Mọi ý tưởng tại sao điều này có thể xảy ra? Nhà cung cấp của tôi không làm gì lạ, nó chỉ lấy người dùng từ cơ sở dữ liệu như bình thường. Tại sao Trái đất lại ảnh hưởng đến chức năng "nhớ tôi"? Đã có bất kỳ thay đổi nào mà tôi không biết? Tôi không sử dụng nhà cung cấp auth tùy chỉnh, chỉ là nhà cung cấp người dùng.

Oh và đây là nhật ký, với grep an ninh

[2013-07-17 15:18:49] security.DEBUG: Username "[email protected]" was reloaded from user provider. [] [] 
[2013-07-17 15:18:49] security.DEBUG: Write SecurityContext in the session [] [] 
[2013-07-17 15:18:49] security.DEBUG: Remember-me cookie detected. [] [] 
[2013-07-17 15:18:49] security.WARNING: User class for remember-me cookie not supported. [] [] 
[2013-07-17 15:18:49] security.DEBUG: Clearing remember-me cookie "REMEMBERME" [] [] 
[2013-07-17 15:18:49] security.INFO: Populated SecurityContext with an anonymous Token [] [] 
[2013-07-17 15:18:49] security.DEBUG: Access is denied (user is not fully authenticated) by "/srv/www/dev/public/remember/vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php" at line 73; redirecting to authentication entry point [] [] 
[2013-07-17 15:18:49] security.DEBUG: Calling Authentication entry point [] [] 
[2013-07-17 15:18:49] security.DEBUG: Write SecurityContext in the session [] [] 
[2013-07-17 15:18:49] security.INFO: Populated SecurityContext with an anonymous Token [] [] 
[2013-07-17 15:18:49] security.DEBUG: Write SecurityContext in the session [] [] 

CẬP NHẬT: Và chỉ khi tôi dán nhật ký tôi nhận thấy rằng cảnh báo. Dù sao, bạn có biết cách khắc phục điều đó không?

CẬP NHẬT 2: Nếu tôi sử dụng nhà cung cấp dịch vụ người dùng mặc định, nhưng vẫn là lớp người dùng của riêng tôi, nó hoạt động tốt. Thông báo lỗi rất gây hiểu nhầm.

+0

Bạn đã bao giờ giải quyết vấn đề này chưa? –

Trả lời

1

Hãy xem Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices.php#L130 nơi cảnh báo bảo mật xuất phát.

Dịch vụ chỉ cung cấp phương thức trừu tượng processAutoLoginCookie mà bạn có thể cần phải thêm vào nhà cung cấp của mình để xử lý cookie.

+0

Nhưng tôi chưa từng chạm vào điều đó. Tôi không triển khai nhà cung cấp xác thực, chỉ một nhà cung cấp người dùng mới có thể lưu/lấy người dùng từ cơ sở dữ liệu và thực thể người dùng. Ngoài ra, hãy xem "cập nhật 2" của tôi. – ChocoDeveloper

Các vấn đề liên quan