隐藏你的 ID 吧!

hyid#

hidden your id. 隐藏你的 ID.

使用场景#

当我们的应用需要提供一个无状态通过 ID 获取信息的接口,例如 GET /user/{id} ,此类接口只要递增 ID 进行请求,就可以得到我们数据库中所有公开信息,这很显然不是我们想看到的。
hyid 可以帮助你隐藏我们不希望用户看到的 ID 字段,或者其他数字字段。

安装#

composer require 96qbhy/hyid

laravel or lumen#

  1. 注册服务提供者 : Qbhy\Hyid\ServiceProvider::class
  2. 发布配置文件 (lumen 可以自行复制 config/hyid.php 或者安装 vendor:publish 命令): php artisan vendor:publish --provider=Qbhy\Hyid\ServiceProvider

使用#

class User extends Model{
    use Qbhy\Hyid\HyidAble;

    // or 
    public function getUserId($userId){
        return hyid($userId);
    }

    // or
    public function toArray(){
        $data = parent::toArray();

        $data['id'] = hyid()->encode($data['id'])

        return $data;
    }
}
// decode
    public function userinfo($id){
        return User::query()->findOrFail(hyid()->decode($id))->toArray();
    }
// 非 laravel or lumen 下,可以自行实例化 Hyid 类
$secret = 'qbhy';
$offset = 1996;
$randomLength = 6;
$hyid = new Hyid($secret,$offset,$randomLength);

$encodedId = $hyid->encode(1);
$id = $hyid->decode($encodedId);

96qbhy@gmail.com
qbhy/hyid

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 6年前 自动加精
qbhy
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 15
qbhy

该方法可以很好的隐藏数据库内原始 ID 值,防止不法分子利用接口恶意获取数据。

6年前 评论

我是直接将模型的 routeKey 改成了 username.

6年前 评论
qbhy

@overtrue username 加唯一索引确实可以的,但是数据大了还是 ID 比较好。

6年前 评论

和 hashid 一样吧

6年前 评论
qbhy

@fun206 看了下 hashids,功能好像确实是差不多

6年前 评论
godruoyi

用的是 base64_encode,当 id 数值很大的时候, encode 的值也很长一串,不支持固定长度的 encode 啊

6年前 评论
cyrnicolase

ID 直接用 uuid

6年前 评论

楼主也太大意了,composer require 96qbhy/hyid 这也写错了,我说怎么安装不了

6年前 评论

@godruoyi 固定长度理论上是不可逆的,因为密文数量有限,而原文数量无限,存在一个密文多个原文的可能

6年前 评论
qbhy

@lovecn 谢谢提醒

6年前 评论
qbhy

@Janpun 嗯,还有改进的空间

6年前 评论

在使用了 laravel-ide-helper 进行 php artisan ide-helper:meta 的时候报错了。

Symfony\Component\Debug\Exception\FatalThrowableError  : Argument 1 passed to Qbhy\Hyid\Hyid::__construct() must be of the type string, null given, called in  /data/www/vendor/96qbhy/hyid/src/ServiceProvider.php on line 37

  at  /data/www/vendor/96qbhy/hyid/src/Hyid.php:43
    39|      * @param string $secret
    40|      * @param int    $offset
    41|      * @param int    $randomLength
    42|      */
  > 43|     public function __construct(string $secret, int $offset, int $randomLength = 4)
    44|     {
    45|         $this->secret       = $secret;
    46|         $this->offset       = $offset;
    47|         $this->randomLength = $randomLength;
  Exception trace:
  1   Qbhy\Hyid\Hyid::__construct()
       /data/www/vendor/96qbhy/hyid/src/ServiceProvider.php:37
  2   Qbhy\Hyid\ServiceProvider::Qbhy\Hyid\{closure}(Object(Illuminate\Foundation\Application), [])
      /data/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:776
  Please use the argument -v to see more details.
6年前 评论
qbhy

@MrCong 配置没问题吗? must be of the type string, null given。这个错误的意思是你传了错误的参数类型,看看 env 有没有添加 HYID_SECRETHYID_OFFSET

6年前 评论

个人建议直接用 uuid

6年前 评论