drlnwji79147769 2017-06-29 13:25
浏览 121
已采纳

Silex2:覆盖`DaoAuthenticationProvider`类的`checkAuthentication`方法

Using Silex 2, I am stuck for hours to find a way to overwrite checkAuthentication method of the DaoAuthenticationProvider class?

About the context: I defined an authentication listener and provider using a custom 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'
    );
});

But I need to customize the checkAuthentication of DaoAuthenticationProvider called automatically when the custom authentication provider reture a successfully authenticated token.

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.');
        }
    }
}

Solution

Define like this in 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']
    );
});
  • 写回答

1条回答 默认 最新

  • douxi2011 2017-06-29 13:47
    关注

    You can make custom authentication provider, you can extend it from DaoAuthenticationProvider. And redefine authentication provider definition in application:

    ...
    
    $app['security.authentication_provider.sds.dao'] = function() {
        return new MyAuthenticationProvider(
            $app['security.user_provider.sds'],
            $app['security.user_checker'],
            'sds',
            $app['security.encoder_factory'],
            $app['security.hide_user_not_found']
        );
    };
    
    $app['security.authentication_listener.sds.form'] = function() {
        return new CustomAuthenticationListener($app['security.token_storage'], $app['security.authentication_manager']);
    };
    
    ...
    
    $app->run();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?