mevisoft / signed-url
为Laravel提供的带签名的(唯一)URL包。
2.0.1
2019-10-12 23:03 UTC
Requires
- php: >=7.2
- illuminate/http: >=5.8
- illuminate/support: >=5.8
- spatie/url-signer: 1.*
This package is auto-updated.
Last update: 2024-09-13 09:38:04 UTC
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 mevisoft/signed-url
该包旨在为Laravel项目提供简单且强大的货币值格式化和转换工具。在框架的旧版本中,只需添加服务提供者,可选地注册外观
// config/app.php 'providers' => [ ... Akaunting\SignedUrl\Provider::class, ]; 'aliases' => [ ... 'SignedUrl' => Akaunting\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' => 'Akaunting\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状态码。
变更日志
请参阅发布以获取更多信息,了解最近有哪些更改。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过security@akaunting.com发送电子邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅LICENSE以获取更多信息。