2015-10-01 23 views
0

我想爲我的Silex應用程序構建一個簡單的身份驗證代碼,但我無法使其工作。無法保護我的路徑與silex securityServiceProvider

我花了幾個小時試圖找出其中的錯誤,但無法找到它.. 下面是代碼:

use Symfony\Component\Security\Core\User\User; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 

$app = new Silex\Application(); 
$app['debug'] = true; 

$app->register(new Silex\Provider\SessionServiceProvider()); 
$app->register(new Silex\Provider\UrlGeneratorServiceProvider()); 

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
    'security.firewalls' => array(
     'admin' => array(
      'pattern' => '^/admin.*', 
      'http' => true, 
      'form' => array(
       'login_path' => '/login', 
       'check_path' => '/admin/login_check', 
      ), 
      'logout' => array(
       'logout_path' => '/admin/logout', 
       'invalidate_session' => true 
      ), 
      'users' => $usersData, 
     ), 
    ) 
)); 

$app->get('/login', function(Request $request) use ($app) { 
    return $app['twig']->render('login.html', array(
     'error'   => $app['security.last_error']($request), 
     'last_username' => $app['session']-  >get('_security.last_username'), 
    )); 
}); 

應用程序是不阻止任何路徑,我真不明白我錯過了,因爲我已經在另一個應用程序中執行了此操作,並且所有工作都正常......

非常感謝您的幫助。

+1

$ usersData是在什麼地方定義的?(無論如何,你需要在安全防火牆中'http'和'form'之間進行選擇,你不能同時擁有這兩個參數) – fain182

+0

感謝@ fain182的幫助,$ usersData從數據庫中恢復, (1){[「admin」] => array(2){[0] => string(10)「ROLE_ADMIN」[1] => string(88)「5FZ2Z8QIkA7UTZ4BYkoC + GsReLf569mSKDsfods6LYQ8t + a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg ==「}}' – fes

+0

我也將http切換爲false,但似乎沒有任何更改..正則表達式與我使用的其他Symfony組件(用戶)一樣按預期工作。 – fes

回答

0

我在配置我的流浪機時遇到了問題,從零開始重建了所有的東西,現在它完美地工作。