2014-02-25 37 views
2

不正確的憑據我是新來的Silex,我用this tutorial建立學說ORM。的Silex:自定義UserProvider

但是現在,當我嘗試登錄時,出現「Bad credentials」錯誤。 這發生在我使用默認的「login_check」控制器時。

如果我使用一個自定義的,它的工作原理,但我不知道如何給用戶,他一直在尋找的頁面重定向。 (我試過蒙山$請求 - >包頭中> GET(在我登錄控制器「引薦」),但它是空的。)

這裏是我的自定義login_check contorller:

$app->post('/login-check-perso', function(Request $request) use ($app){ 

$route = $request->request->filter('route'); 
$password = $request->get('_password'); 
$username = $request->get('_email'); 


$userProvider = new \Lib\Provider\UserProvider($app); 

$user = null; 
try { 
    $user = $userProvider->loadUserByUsername($username); 
} 
catch (UsernameNotFoundException $e) 
{ 
} 

$encoder = $app['security.encoder_factory']->getEncoder($user); 

// compute the encoded password 
$encodedPassword = $encoder->encodePassword($password, $user->getSalt()); 

// compare passwords 
if ($user->getPassword() == $encodedPassword) 
{ 
    // set security token into security 
    $token = new UsernamePasswordToken($user, $password, 'yourProviderKeyHere', array('ROLE_ADMIN')); 
    $app['security']->setToken($token); 

    // redirect or give response here 
} else { 
    // error feedback 
    echo "wrong password"; 
    die(); 
} 
// replace url by the one the user requested while he wasn't logged in 
return $app->redirect('/web/index_dev.php/admin/'); 
})->bind('login_check_perso'); 

因此,如果有人可以解釋如何使用默認的「login_check」,或向我解釋如何將用戶重定向到他正在嘗試訪問的頁面,但未登錄時,它會很棒。

感謝

編輯:

我認爲 「壞證書」 是由錯誤的編碼器設置造成的,我用this
配置礦:

$app['security.encoder.digest'] = $app->share(function ($app) { 
    // use the sha1 algorithm 
    // don't base64 encode the password 
    // use only 1 iteration 
    return new MessageDigestPasswordEncoder('sha1', false, 1); 
}); 

$app['security.encoder_factory'] = $app->share(function ($app) { 
    return new EncoderFactory(
     array(
      'Symfony\Component\Security\Core\User\UserInterface' => $app['security.encoder.digest'], 
      'Entity\User'       => $app['security.encoder.digest'], 
     ) 
    ); 
}); 

是正確的嗎?

回答

0

可以擴展DefaultAuthenticationSuccessHandler,這將在全成登錄後叫,我這樣做是這樣的:

// overwrite the default authentication success handler 
$app['security.authentication.success_handler.general'] = $app->share(function() use ($app) { 
    return new CustomAuthenticationSuccessHandler($app['security.http_utils'], array(), $app); 
    }); 

創建CustomAuthenticationSuccessHandler:

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Security\Http\HttpUtils; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Silex\Application; 

class CustomAuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler 
{ 
    protected $app = null; 

    /** 
    * Constructor 
    * 
    * @param HttpUtils $httpUtils 
    * @param array $options 
    * @param unknown $database 
    */ 
    public function __construct(HttpUtils $httpUtils, array $options, Application $app) 
    { 
     parent::__construct($httpUtils, $options); 
     $this->app = $app; 
    } 

    /** 
    * (non-PHPdoc) 
    * @see \Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler::onAuthenticationSuccess() 
    */ 
    public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
    { 
     $user = $token->getUser(); 
     $data = array(
      'last_login' => date('Y-m-d H:i:s') 
     ); 
     // save the last login of the user 
     $this->app['account']->updateUser($user->getUsername(), $data); 
     return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request)); 
    } 
} 

我使用onAuthenticationSuccess()保存用戶上次登錄日期時間。您可以使用createRedirectResponse()將用戶重定向到您需要的起點。

+0

感謝您的回覆, 但我不知道如何讓它工作。 我不知道如何在我的自定義「login_check」控制器後調用它。 而且它從來沒有被默認的「login_check」調用,因爲我得到了「Bad credentials」錯誤。 我添加了一些信息到我的問題,也許它會有所幫助。 – OlivierG

相關問題