【发布时间】:2017-03-29 16:34:08
【问题描述】:
$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 \App\UserProvider($app['dbs']['mysql']);
},
),
),
'security.access_rules' => array(
array('^/profile', 'ROLE_USER')
)
));
在 Silex 2 应用程序中,我已经能够在 login_check 中验证用户凭据,但随后重定向是可怕的(对我而言)运行时异常“ContextListener.php 第 74 行没有用户提供程序”用户“Symfony\组件\安全\核心\用户\用户”。
(我的“标准”用户提供程序类)
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 \App\Security\User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
return $class === 'Symfony\Component\Security\Core\User\User';
}
}
我已经搜索过这个问题的每个实例,看起来 Symfony 安装中最常见的解决方案是将安全性配置为指向自定义用户提供程序,但我没有看到任何与 silex 不同的示例上面的方式:
'users' => function () use ($app) {
return new \App\UserProvider($app['dbs']['mysql']);
},
(我在 symfony 的 security.xml 文件中看到了这一点,这种提供程序配置是否缺少?)
providers:
main:
entity: { class: FredUtilisateurBundle:User, property: login }
有没有人可以在通过身份验证后帮助我解决“无用户提供者”的逻辑?
【问题讨论】:
标签: silex