Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Silex中使用db支持的UserProvider进行用户身份验证_Php_Security_Silex - Fatal编程技术网

Php 在Silex中使用db支持的UserProvider进行用户身份验证

Php 在Silex中使用db支持的UserProvider进行用户身份验证,php,security,silex,Php,Security,Silex,我正在开发一个Silex应用程序,现在正处于安全阶段。我已经阅读了我在网上找到的关于这个主题的所有文档,但是我有很多疑问,如果可能的话,我希望有人能帮助我 基本上我是跟着 自然地: 还有我在谷歌上找到的一切 但是,我认为Silex仍然缺乏很多文档,我在很多方面都迷失了方向 我的代码: $app->register(new Silex\Provider\SessionServiceProvider(), array( 'session.storage.save_path' =>

我正在开发一个Silex应用程序,现在正处于安全阶段。我已经阅读了我在网上找到的关于这个主题的所有文档,但是我有很多疑问,如果可能的话,我希望有人能帮助我

基本上我是跟着

自然地:

还有我在谷歌上找到的一切

但是,我认为Silex仍然缺乏很多文档,我在很多方面都迷失了方向

我的代码:

 $app->register(new Silex\Provider\SessionServiceProvider(), array(
  'session.storage.save_path' => __DIR__.'/../vendor/sessions',
 ));

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver'    => 'pdo_mysql',
'host'      => 'localhost',
'dbname'    => 'dbname',
'user'      => 'someuser',
'password'  => 'somepass',
'charset'   => 'utf8',
),
));



$app['security.encoder.digest'] = $app->share(function ($app) {
    return new MessageDigestPasswordEncoder('sha1', false, 1);
});


$app['security.firewalls'] = array(
    'acceso' => array(
    'pattern' => '^/confirmar',
    'form' => array('login_path' => '/acceso', 'check_path' => '/confirmar/comprobar_acceso'),
    'logout' => array('logout_path' => '/confirmar/salir'),
    'users' => $app->share(function() use ($app) {
     return new Acme\User\UserProvider($app['db']);
    }),
),
);


$app->register(new Silex\Provider\SecurityServiceProvider(array(
'security.firewalls' => $app['security.firewalls'],
'security.access_rules' => array(
array('^/confirmar', 'ROLE_USER'),
),
)));
我对控制员有很多疑问:

$app->match('/acceso', function(Request $request) use ($app) {

$username = $request->get('_username');
$password = $request->get('_password');

if ('POST' == $request->getMethod())
    {
    $user = new Acme\User\UserProvider($app['db']);
    $encoder = $app['security.encoder_factory']->getEncoder($user);
    // compute the encoded password
    $encodedPassword = $encoder->encodePassword($password, $user->getSalt());

    // compare passwords
        if ($user->password == $encodedPassword)
            {
            // set security token into security
            $token = new UsernamePasswordToken($user, $password, '', array('ROLE_USER'));
            $app['security']->setToken($token);
           //return $app->redirect('/jander');
           // redirect or give response here
         } else {
         // error feedback
         }

         }


return $app['twig']->render('login.twig', array(
    'error'         => $app['security.last_error']($request),
    'last_username' => $app['session']->get('_security.last_username'),
));
})
->bind('acceso');
这是我的类,用户提供程序:

// src/Acme/User/UserProvider.php
namespace Acme\User;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\DBAL\Connection;




class UserProvider implements UserProviderInterface
{
private $conn;

public function __construct(Connection $conn)
{
    $this->conn = $conn;
}

public function loadUserByUsername($username)
{
    $stmt = $this->conn->executeQuery('SELECT * FROM compradores WHERE idemail = ?', array(strtolower($username)));
    if (!$user = $stmt->fetch()) {
        throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
    }

    return new User($user['idemail'], $user['pass'], explode(',', $user['roles']), true, true, true, true);
}

public function refreshUser(UserInterface $user)
{
    if (!$user instanceof 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';
}
}
我的表格:

<form action="{{ path('confirmar_comprobar_acceso') }}" method="post">
{{ error }}
<input type="text" name="_username" value="{{ last_username }}" />
<input type="password" name="_password" value="" />
<input type="submit" />
</form>

尝试登录时,我总是收到“错误凭据”响应。有什么想法吗?谢谢,干杯

在40个字符时,密码字段“pass”可能会截断加密的密码。尝试将字段更改为varchar(255)

我在使用默认编码器时遇到了类似的问题,我怀疑这与数据库排序或多个base64编码有关

我将其替换为简单的sha1,这相当不安全,但这给出了一个总体思路(为了安全起见,您可以将其替换为sha2)。在注册安全提供商后添加以下内容:

$app['security.encoder.digest'] = $app->share(function ($app) {
    // use the sha1 algorithm
    // don't base64 encode the password
    // use only 1 iteration
    return new MessageDigestPasswordEncoder('sha1', false, 1);
});

您必须确保存储的密码使用相同的哈希算法。

上面的第一个链接要么已断开,要么包含病毒(我的firefox刚刚崩溃)。你能把它取下来吗?
$app['security.encoder.digest'] = $app->share(function ($app) {
    // use the sha1 algorithm
    // don't base64 encode the password
    // use only 1 iteration
    return new MessageDigestPasswordEncoder('sha1', false, 1);
});