【问题标题】:Silex2 : overwrite `checkAuthentication` method of the `DaoAuthenticationProvider` classSilex2:覆盖“DaoAuthenticationProvider”类的“checkAuthentication”方法
【发布时间】:2017-06-29 13:25:47
【问题描述】:

使用 Silex 2,我被困了好几个小时才能找到覆盖 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'
    );
});

但是我需要自定义 DaoAuthenticationProvidercheckAuthentication ,当自定义身份验证提供程序返回成功身份验证的令牌时自动调用。

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

【问题讨论】:

    标签: php symfony authentication silex


    【解决方案1】:

    您可以自定义身份验证提供程序,您可以从 DaoAuthenticationProvider 扩展它。并在应用程序中重新定义身份验证提供程序定义:

    ...
    
    $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();
    

    【讨论】:

    • 感谢您的帮助。在您的示例中,MyAuthenticationProvider 是一个扩展UserAuthenticationProvider 的类,对吗?并且这段代码必须是 $app['security.authentication_listener.factory.sds'] 配置的一部分或在之前/之后声明?
    • 1 - 在我的例子中 - 是的,2 - 我认为没有必要重新定义 security.authentication_listener.factory。只需重新定义侦听器。我改变了答案。
    • 谢谢你,你给了我解决这个问题所需的提示。干净的解决方案在我问的问题的末尾。
    猜你喜欢
    • 2020-07-10
    • 2011-04-17
    • 2012-05-13
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多