覆盖 laravel 4 的身份验证方法以使用自定义哈希函数

2024-01-19

我的数据库中有一个包含用户的表。他们的密码是用我自己的自定义哈希函数生成的。

如何重写 laravel 4 中的身份验证方法以使用我自己的哈希类?

这就是我一直在努力做的事情:

    class CustomUserProvider implements Illuminate\Auth\UserProviderInterface {


    public function retrieveByID($identifier)
    {
        return $this->createModel()->newQuery()->find($identifier);
    }

    public function retrieveByCredentials(array $credentials)
    {
        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // Eloquent User "model" that will be utilized by the Guard instances.
        $query = $this->createModel()->newQuery();

        foreach ($credentials as $key => $value)
        {
            if ( ! str_contains($key, 'password')) $query->where($key, $value);
        }

        return $query->first();
    }

    public function validateCredentials(Illuminate\Auth\UserInterface $user, array $credentials)
    {
        $plain = $credentials['password'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

}

class CodeIgniter extends Illuminate\Auth\Guard {


}

App::bind('Illuminate\Auth\UserProviderInterface', 'CustomUserProvider');



Auth::extend('codeigniter', function()
{
    return new CodeIgniter( App::make('CustomUserProvider'), App::make('session'));
});

当我运行 Auth::attempt 方法时,出现此错误: ErrorException:警告:G:\Dropbox\Workspaces\www\video\vendor\laravel\framework\src\Illuminate\Foundation\Application.php 第 352 行中 isset 中存在非法偏移类型或空


这就是最终解决问题的方法:

库\CustomHasherServiceProvider.php

use Illuminate\Support\ServiceProvider;

class CustomHasherServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('hash', function()
        {
            return new CustomHasher;
        });
    }

}

库\CustomHasher.php

class CustomHasher implements Illuminate\Hashing\HasherInterface {

private $NUMBER_OF_ROUNDS = '$5$rounds=7331$';


public function make($value, array $options = array())
{

    $salt = uniqid();
    $hash = crypt($password, $this->NUMBER_OF_ROUNDS . $salt);
    return substr($hash, 15);
}

public function check($value, $hashedValue, array $options = array())
{
    return $this->NUMBER_OF_ROUNDS . $hashedValue === crypt($value, $this->NUMBER_OF_ROUNDS . $hashedValue);
}

}

然后我在 app/config/app.php 的提供者数组中将 'Illuminate\Hashing\HashServiceProvider' 替换为 'CustomHasherServiceProvider'

并添加了“app/libraries”以在composer.json中自动加载类映射

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

覆盖 laravel 4 的身份验证方法以使用自定义哈希函数 的相关文章

  • 使用 md5 加密的 PHP 和 Mysql 查询出现问题

    我使用普通的 php mysql 插入查询并使用 md5 加密密码 这是插入查询 sql mysql query INSERT INTO user username password role approved values usernam
  • PHPExcel下载文件

    我想下载使用 PHPExcel 生成的 Excel 文件 我按照以下代码PHPExcel 强制下载问题 https stackoverflow com questions 26265108 phpexcel force download i
  • PHP解析xml文件错误

    我正在尝试使用 simpleXML 来获取数据http rates fxcm com RatesXML http rates fxcm com RatesXML Using simplexml load file 我有时会遇到错误 因为这个
  • 在 PHP 中将 CSV 写入不带括号的文件

    是否有本机函数或实体类 库用于将数组写入 CSV 文件中的一行而无需封装 fputcsv将默认为 如果没有为封装参数传入任何内容 谷歌让我失望了 返回一大堆有关的页面的结果 fputcsv PEAR 的库做的事情或多或少与fputcsv 工
  • 重复使用相同的卷曲手柄。性能大幅提升?

    在 PHP 脚本中 我对不同的 URL 执行了许多不同的curl GET 请求 一百个 将重复使用来自curl init提高性能 还是与请求的响应时间相比可以忽略不计 我这么问是因为在当前的架构中保持相同的句柄并不容易 交叉发布自我应该关闭
  • 覆盖供应商自动加载编辑器

    有没有办法让您创建的自动加载文件在调用供应商自动加载之前运行 我们似乎遇到了 SimpleSAML 的自动加载覆盖我们创建的自动加载文件之一的问题 我是 Composer 的新手 似乎无法在网上找到任何解决方案 我尝试将我们的自动加载文件包
  • 如何将 PHPMailer 与 Codeigniter 3 集成

    嗨 我正在尝试使用PHPMailer 库 https github com PHPMailer PHPMailer来自我的 Codeigniter 应用程序中的 GitHub 我下载了代码并解压到我的application library文
  • php - 我应该加密电子邮件地址吗?

    当用户注册时 我应该将他们的电子邮件按原样存储在数据库中还是对其进行哈希处理 我希望稍后能够解密 那么我应该使用 md5 吗 谢谢你 No md5 is 单向哈希函数 http en wikipedia org wiki Cryptogra
  • 使用日语“Enter”键进行搜索功能

    我在日语方面遇到了问题 我有一个允许用户搜索数据的表单 当用户输入要搜索的字符串并按 Enter 键时 搜索功能就会执行 我的代码是 formSearch input keyup function event var key event c
  • CakePHP Xml 实用程序库触发 DOMDocument 警告

    我正在使用 CakePHP 在视图中生成 XMLXML核心库 http book cakephp org 2 0 en core utility libraries xml html xml Xml build data array ret
  • PHP - hash_pbkdf2 函数

    我正在尝试使用此 php 函数执行一个函数来哈希密码 http be php net manual en function hash pbkdf2 php http be php net manual en function hash pb
  • Android GCM 服务器的 API 密钥

    我有点困惑我应该为 GCM 服务器使用哪个 API 密钥 在文档中它说使用 android api 密钥 这对我不起作用并且总是给出未经授权的 http developer android com google gcm gs html ht
  • 如何在 HTML / Javascript 页面中插入 PHP 下拉列表

    好吧 这是我的第二篇文章 请接受我是一个完全的新手 愿意学习 花了很多时间在各个网站上寻找答案 而且我几乎已经到达了我需要到达的地方 至少在这一点上 我有一个网页 其中有许多 javascript 函数 这些函数一起使用 google 地图
  • 安装后如何使用 npm 包 (chart.js)?

    我正在制作一个练习 Laravel 站点 并且我已经通过 npm install 安装了 Chart js 这是一个愚蠢的问题 但现在我如何从这里使用它 或通过 npm 安装的任何东西 这些文件安装在节点模块文件夹中 我应该在页眉中使用标签
  • postgreSQL 在 WAMP 上的集成

    我刚刚在 Windows 7 上安装了 postgreSQL 我正在尝试将 postgreSQL 与 WAMP 服务器集成 为此 我在 httpd conf 和 php ini 文件中进行了以下更改 1个加载模块c path to libp
  • 如何在html中制作多行类型的文本框?

  • Zend Framework Zend_Form 装饰器: 位于按钮元素内部?

    我有一个像这样创建的按钮元素 submit new Zend Form Element Button submit submit gt setLabel My Button submit gt setDecorators array Vie
  • mysqli bind_param 中的 NULL 是什么类型?

    我正在尝试将参数绑定到 INSERT INTO MySQLi 准备好的语句 如果该变量存在 否则插入 null 然后我知道 type variable i corresponding variable has type integer d
  • PHP cURL 在本地工作,在 AWS 服务器上出现错误 77

    最新更新 脚本作为管理员用户通过 SSH shell 作为 php script php 成功运行 当由 nginx 用户运行时 curl 命令无法执行 https 请求 所以我猜测这是nginx用户无法正确使用curl的问题 我已经检查了
  • PHP 和 NLP:嵌套括号(解析器输出)到数组?

    想要将带有嵌套括号的文本转换为嵌套数组 以下是 NLP 解析器的输出示例 TOP S NP PRP I VP VBP love NP NP DT a JJ big NN bed PP IN of NP NNS roses 原文 我喜欢一大床

随机推荐