1
Silex 2を使用して、DaoAuthenticationProvider
クラスのcheckAuthentication
メソッドを上書きする方法を何時間も見つけられませんか?Silex2: `DaoAuthenticationProvider`クラスの` checkAuthentication`メソッドを上書きします
コンテキストについて:私は認証リスナとプロバイダをカスタムUserToken
で定義しました。
$app['security.authentication_listener.factory.sds'] = $app->protect(function ($name, $options) use ($app) {
// define the authentication provider object
$app['security.authentication_provider.'.$name.'.sds'] = function() use ($app) {
return new CustomAuthenticationProvider($app['user.provider'], $app['security.encoder_factory']);
};
// define the authentication listener object
$app['security.authentication_listener.'.$name.'.sds'] = function() use ($app) {
return new CustomAuthenticationListener($app['security.token_storage'], $app['security.authentication_manager']);
};
return array(
// the authentication provider id
'security.authentication_provider.'.$name.'.sds',
// the authentication listener id
'security.authentication_listener.'.$name.'.sds',
// the entry point id
null,
// the position of the listener in the stack
'pre_auth'
);
});
しかし、私は、カスタム認証プロバイダが正常に認証トークンをretureときDaoAuthenticationProvider
のcheckAuthentication
が自動的に呼び出さカスタマイズする必要があります。
protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
{
$currentUser = $token->getUser();
if ($currentUser instanceof UserInterface) {
if ($currentUser->getPassword() !== $user->getPassword()) {
throw new BadCredentialsException('The credentials were changed from another session.');
}
} else {
if ('' === ($presentedPassword = $token->getCredentials())) {
throw new BadCredentialsException('The presented password cannot be empty.');
}
if (!$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt())) {
throw new BadCredentialsException('The presented password is invalid.');
}
}
}
ソリューション
app.phpにこのように定義します。
$app['security.authentication_provider.dao._proto'] = $app->protect(function ($name) use($app) {
return new \Trilogis\Classes\CustomUserAuthenticationProvider(
$app['security.user_provider.' . $name],
$app['security.user_checker'],
$name,
$app['security.encoder_factory']
);
});
ありがとうございます。あなたの例では、 'MyAuthenticationProvider'は' UserAuthenticationProvider'権利を拡張するクラスですか?そして、このコードは$ app ['security.authentication_listener.factory.sds'] configの一部でなければならず、前/後に宣言しなければなりませんか? – sdespont
1 - 私の例では - はい、2 - 'security.authentication_listener.factory'を再定義する必要はないと思います。リスナーを再定義するだけです。私は答えを変えた。 –
ありがとう、私はこれを理解するために必要なヒントを教えてください。清潔な解決策は私が尋ねた質問の終わりです。 – sdespont