Silex "There is no user provider for user"如何配置安全性


 $app->register(new SecurityServiceProvider(), array(
    'security.firewalls' => array(
        'admin' => array(
            'pattern' => '^/',
            'form' => array(
                'login_path' => '/login',
                'check_path' => '/login_check',
                'always_use_default_target_path' => true,
                'default_target_path' => '/profile'
            ),
            'logout' => true,
            'anonymous' => true,
            'users' => function () use ($app) {
                return new AppUserProvider($app['dbs']['mysql']);
            },
        ),
    ),
'security.access_rules' => array(
    array('^/profile', 'ROLE_USER')
)
));

在一个Silex 2应用程序中,我已经能够在login_check中验证用户的凭据,但然后在重定向上是可怕的(对我来说)运行时异常"ContextListener.php行74没有用户提供程序为用户"SymfonyComponentSecurityCore user user"。

(我的'标准'用户提供程序类)

class UserProvider implements UserProviderInterface {
private $conn;
public function __construct(Connection $conn)
{
    $this->conn = $conn;
}
public function loadUserByUsername($username)
{
  //  $app['monolog']->addInfo(sprintf("User '%s' registered.", $username));
    $stmt = $this->conn->executeQuery('SELECT * FROM users_wet4 WHERE email = ?', array(strtolower($username)));
    if (!$user = $stmt->fetch()) {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }
    return new User($user['email'], $user['password'], explode(',', $user['roles']), true, true, true, true);
}
public function refreshUser(UserInterface $user)
{
    if (!$user instanceof AppSecurityUser) {
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
    }
    return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
    return $class === 'SymfonyComponentSecurityCoreUserUser';
}
}

我已经搜索了这个问题的每个实例,看起来Symfony安装中最常见的解决方案是将安全性配置为指向自定义用户提供者,但我没有看到任何与上述方法不同的silex示例:

    'users' => function () use ($app) {
            return new AppUserProvider($app['dbs']['mysql']);
        },

(我在symfony的security.xml文件中看到过,缺少的是这种提供商配置吗?)

    providers:
    main:
        entity: { class: FredUtilisateurBundle:User, property: login }

有谁可以帮助我与'无用户提供者'的逻辑被认证后?

好了,问题就在这里:

 if (!$user instanceof AppSecurityUser) {  

应该是User。

 if (!$user instanceof User) {

或任何您可能创建的自定义用户

 AppMyUser

相关内容