簡體   English   中英

Silex:帶有自定義UserProvider的不良憑證

[英]Silex : Bad Credentials with custom UserProvider

我是Silex的新手,我使用本教程來設置Doctrine ORM。

但是現在,當我嘗試登錄時,出現“錯誤的憑據”錯誤。 當我使用默認的“ login_check”控制器時會發生這種情況。

如果使用自定義標簽,它可以工作,但是我不知道如何將用戶重定向到他要查找的頁面。 (我在登錄控制器中嘗試過$ request-> headers-> get('referer'),但它為空。)

這是我的自定義login_check控制器:

$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”,或者向我解釋如何將用戶重定向到他未登錄但試圖訪問的頁面,那將是很好的。

謝謝

編輯:

我認為“憑據錯誤”是由錯誤的編碼器設置引起的,我使用
配置我的:

$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'],
        )
    );
});

那是對的嗎 ?

您可以擴展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()將用戶重定向到所需的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM