【问题标题】:How to disable laravel 5.2 password bcrypt如何禁用 laravel 5.2 密码 bcrypt
【发布时间】:2017-06-22 20:19:34
【问题描述】:

当我尝试这样登录时,我想禁用 laravel 密码 bcrypt

Auth::guard('client')->attempt(
'id' => $request['id'],
'password' => $request['password'])

但这似乎比我想象的要困难,我知道我不应该这样做但我暂时需要这样工作,但是 laravel 强迫我使用加密密码。我需要能够在我的数据库上使用纯密码。

我一直在网上搜索,但找不到解决方案。

【问题讨论】:

  • 哦不:未来还会有另一个安全漏洞。
  • 永远不要在数据库中使用纯文本密码。

标签: laravel hash password-protection bcrypt password-encryption


【解决方案1】:

尝试扩展 SessionGuard 并覆盖函数 hasValidCredentials()

在 App\CoreExtensions 中创建一个名为“SessionGuardExtended”的文件

use Illuminate\Auth\SessionGuard;
use Illuminate\Contracts\Auth\Authenticatable;

class SessionGuardExtended extends SessionGuard
{
    /**
     * Determine if the user matches the credentials.
     *
     * @param  mixed  $user
     * @param  array  $credentials
     * @return bool
     */
    protected function hasValidCredentials($user, $credentials)
    {
        return ! is_null($user) && $credentials['password'] == $user->getAuthPassword();
    }
}

Edit config/auth.php 编辑驱动并使用 sessionExtended

'web' => [
    'driver' => 'sessionExtended',
    'provider' => 'users',
],

在 AppServiceProvider 中写代码启动函数

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Auth::extend(
        'sessionExtended',
        function ($app) {
            $provider = new EloquentUserProvider($app['hash'], config('auth.providers.users.model'));
            return new SessionGuardExtended('sessionExtended', $provider, app()->make('session.store'), request());
        }
    );
}

参考:Extending Laravel 5.2 SessionGuard

【讨论】:

    【解决方案2】:

    你可以扩展 Illuminate\Auth\EloquentUserProvider,即:

    <?php
    
    namespace App\Services\Auth;
    
    use Illuminate\Auth\EloquentUserProvider as BaseUserProvider;
    use Illuminate\Contracts\Auth\Authenticatable as UserContract;
    
    class UserProvider extends BaseUserProvider {
        /**
         * Create a new database user provider.
         *
         * @param string $model
         *
         * @return void
         */
        public function __construct($model)
        {
            $this->model = $model;
        }
    
        /**
         * Validate a user against the given credentials.
         *
         * @param \Illuminate\Contracts\Auth\Authenticatable $user
         * @param array                                      $credentials
         *
         * @return bool
         */
        public function validateCredentials(UserContract $user, array $credentials)
        {
            $plain = $credentials['password'];
    
            // $matches = some method of matching $plain with $user->getAuthPassword();
    
            return $matches;
        }
    }
    

    然后在服务提供商的 IoC 中注册它,如下所示:

    <?php // ...
    
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        // ...
    
        $this->app['auth']->extend(
            'legacy',
            function () {
                return new \Illuminate\Auth\Guard(
                    new \App\Services\Auth\UserProvider(
                        $this->app['config']['auth.model']
                    ),
                    $this->app['session.store']
                );
            }
        );
    
        // ...
    }
    

    然后在 config/auth.php 中将您当前的驱动程序设置为旧版。

    PS:您可能希望在提供程序中包含类,

    【讨论】:

    • 扩展是什么意思?哪个班?什么是loC?
    【解决方案3】:

    你可以使用

    Auth::guard('client')->login($user);

    在这个 $user 中是模型的一个实例,它实现了 Illuminate\Contracts\Auth\Authenticatable 合约。

    在用户模型中已经实现了。

    也许会有所帮助

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2019-03-10
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 2016-07-03
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多