2013-02-05 26 views
8

Tôi đang phát triển một ứng dụng Silex và giờ tôi đang trong giai đoạn bảo mật. Tôi đã đọc tất cả các tài liệu tôi đã tìm thấy trên mạng về chủ đề này, nhưng tôi có nhiều nghi ngờ, và tôi muốn ai đó sẽ giúp tôi, nếu có thể.Xác thực người dùng với UserProvider được db hỗ trợ trong Silex

Về cơ bản, tôi đã theo dõi this tutorial from Johann Reinke.

và tự nhiên các Silex documentation:

Ngoài ra tất cả mọi thứ tôi tìm thấy trên Google.

Nhưng vẫn còn, tôi nghĩ rằng Silex vẫn còn thiếu nhiều tài liệu, tôi bị mất theo nhiều cách.

Mã của tôi:

$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'), 
), 
))); 

Tôi có rất nhiều nghi ngờ trong bộ điều khiển:

$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'); 

Đây là lớp học, cung cấp tài khoản của tôi:

// 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'; 
} 
} 

Và hình thức của tôi:

<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> 

Và đây là bảng mysql của tôi:

id   int(15) 
idemail varchar(255) 
nombre varchar(255) 
apellidos varchar(255) 
telefono int(11) 
activo tinyint(4) 
pass varchar(40) 
roles varchar(255) 
iva   tinyint(4) 
nifcif  varchar(255) 

tôi luôn có được một "chứng chỉ Bad" phản ứng khi cố gắng đăng nhập. Bất kỳ ý tưởng? Cảm ơn và chúc mừng!

+1

Liên kết đầu tiên ở trên bị hỏng hoặc chứa vi-rút (firefox của tôi chỉ bị băn khoăn). Bạn có thể lấy nó xuống không? – Dwebtron

Trả lời

4

Tại 40 ký tự, trường mật khẩu của bạn "pass" có thể cắt ngắn mật khẩu được mã hóa. Hãy thử thay đổi trường thành varchar (255)

0

Tôi đã gặp sự cố tương tự khi sử dụng bộ mã hóa mặc định, mà tôi nghi ngờ có liên quan đến collation cơ sở dữ liệu hoặc có thể với nhiều mã hóa base64.

Tôi đã thay thế nó bằng sha1 đơn giản, khá không an toàn, nhưng điều này đưa ra một ý tưởng chung (bạn có thể thay thế bằng sha2 để ở bên an toàn). Thêm thông tin này sau khi đăng ký nhà cung cấp bảo mật của bạn:

$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); 
}); 

Và bạn sẽ phải đảm bảo mật khẩu được lưu trữ của mình sử dụng cùng một thuật toán băm.

Các vấn đề liên quan