patieru / signed-url
Laravel 的签名(唯一)URL 包。
1.0.4
2019-10-03 19:42 UTC
Requires
- php: >=7.2
- illuminate/http: >=5.2
- illuminate/support: >=5.2
- patieru/url-signer: 0.0.4
README
此包可以创建有限生命周期的 URL。这是通过向 URL 添加到期日期和签名来完成的。
这是创建有效期为 30 天的签名 URL 的方法
SignedUrl::sign('https://myapp.com/protected-route', 30);
输出将如下所示
https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx
可以使用 validate
-函数验证 URL。
SignedUrl::validate('https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx');
该包还提供一个中间件来保护路由。
安装
如您所猜测,该包可以通过 Composer 安装
composer require patieru/signed-url
此包旨在为 Laravel 项目提供一种简单而强大的方式来格式化和转换货币值。在框架的旧版本中,只需添加服务提供者,并可选择注册外观
// config/app.php 'providers' => [ ... Patieru\SignedUrl\Provider::class, ]; 'aliases' => [ ... 'SignedUrl' => Patieru\SignedUrl\Facade::class, ];
配置
配置文件可以通过以下方式可选地发布
php artisan vendor:publish --provider=signed-url
这是文件的内容
return [ /* * This string is used the to generate a signature. You should * keep this value secret. */ 'signatureKey' => env('APP_KEY'), /* * The default expiration time of a URL in days. */ 'default_expiration_time_in_days' => 1, /* * These strings are used a parameter names in a signed url. */ 'parameters' => [ 'expires' => 'expires', 'signature' => 'signature', ], /* |-------------------------------------------------------------------------- | Middleware |-------------------------------------------------------------------------- | | This option indicates the middleware to change language. | */ 'middleware' => 'Patieru\SignedUrl\Middleware\ValidateSignedUrl', ];
用法
签名 URL
可以使用 sign
-方法签名 URL。
SignedUrl::sign('https://myapp.com/protected-route');
默认情况下,URL 的生命周期为一天。此值可以在配置文件中更改。如果您想自定义生命周期,可以指定 URL 应该有效的天数
//the generated URL will be valid for 5 days. SignedUrl::sign('https://myapp.com/protected-route', 5);
为了细粒度控制,您还可以将 DateTime
实例作为第二个参数传递。该 URL 将在此时间之前有效。此示例使用 Carbon 以便于使用
//This URL will be valid up until 2 hours from the moment it was generated. SignedUrl::sign('https://myapp.com/protected-route', Carbon\Carbon::now()->addHours(2) );
验证 URL
要验证签名 URL,只需调用 validate()
-方法。这将返回一个布尔值。
SignedUrl::validate('https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx');
使用中间件保护路由
该包还提供一个中间件来保护路由
Route::get('protected-route', ['middleware' => 'signed', function () { return 'Hello secret world!'; }]);
如果没有有效的签名调用路由,您的应用将终止并返回 403 状态码。
变更日志
请参阅 版本 了解最近有哪些变化。
贡献
请参阅 贡献 了解详情。
安全
如果您发现任何安全相关的问题,请通过security@schooloak.com 发送电子邮件,而不是使用问题跟踪器。
致谢
许可证
MIT 许可证(MIT)。请参阅 LICENSE 了解更多信息。