juliomotol / laravel-auth-timeout
Laravel认证超时
Requires
- php: ^8.1
- illuminate/auth: ^9.0|^10.0|^11.0
- illuminate/events: ^9.0|^10.0|^11.0
- illuminate/session: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- larastan/larastan: ^2.0.1
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0|^7.0|^8.0
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest: ^1.21|^2.34
- pestphp/pest-plugin-laravel: ^1.1|^2.3
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5|^10.0
- dev-master
- v4.x-dev
- v4.1.0
- v4.0.1
- v4.0.0
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.1
- v2.2.0
- 2.1.0
- v2.0.0
- v1.x-dev
- v1.0.0
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-5
- dev-dependabot/github_actions/actions/checkout-4
- dev-dependabot/github_actions/aglipanci/laravel-pint-action-2.3.0
- dev-develop
- dev-feature/configurable-redirectTo-callback
This package is auto-updated.
Last update: 2024-09-02 15:17:36 UTC
README
在Laravel中处理认证超时。
升级到v4时,请参阅CHANGELOG.md。
对于Laravel 8+的支持,请参阅v3。
对于Laravel 6+的支持,请参阅v2。
为什么使用Laravel Auth Timeout?
有时我们希望在用户在一定时间内没有进行任何请求时注销用户。有一个解决方案(见下文)
/* Somewhere in config/session.php */
'lifetime' => 15,
但这会影响整个会话。但不必这样,这正是Laravel Auth Timeout的用武之地。
Laravel Auth Timeout是一个小型中间件包,它检查用户是否在设定时间内进行了任何请求。如果他们达到了空闲时间限制,则在下一次请求时注销。感谢Brian Matovu的文章。
安装
您可以通过composer安装此包
composer require juliomotol/laravel-auth-timeout
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="laravel-auth-timeout-config"
这是发布配置文件的内容
<?php return [ /** * The session name used to identify if the user has reached the timeout time. */ 'session' => 'last_activity_time', /** * The minutes of idle time before the user is logged out. */ 'timeout' => 15, /** * The event that will be dispatched when a user has timed out. */ 'event' => JulioMotol\AuthTimeout\Events\AuthTimedOut::class, ];
使用方法
快速入门
对于简单使用,在您的Kernel.php
中注册CheckAuthTimeout
。
protected $routeMiddleware = [ ... 'auth.timeout' => \JulioMotol\AuthTimeout\Middlewares\CheckAuthTimeout::class, ... ];
然后在路由上使用该中间件。
Route::get('/admin', [ 'uses' => 'FooBarController@Foobar', 'middleware' => ['auth.timeout'] ]);
使用不同的守卫
您可能有多个守卫,只想将CheckAuthTimeout
应用于某些守卫。我们为您提供了支持,CheckAuthTimeout
接受一个$guard
参数。
Route::get('/admin', [ 'uses' => 'FooBarController@Foobar', 'middleware' => ['auth.timeout:custom-guard'] // Add the guard name as a parameter for the auth.timeout middleware. ]);
注意:此包仅适用于使用
session
驱动器的守卫。
AuthTimedOut
每次用户超时时,都会触发一个AuthTimedOut
。您可以在您的EventServiceProvider
中为该事件分配监听器。
protected $listen = [ \JulioMotol\AuthTimeout\Events\AuthTimedOut::class => [ // ... ], ];
AuthTimedOut
有两个属性,您可以在您的EventListener
中访问。
class FooEventListener { public function handle(AuthTimedOut $event) { $event->user; $event->guard; } }
重定向
要修改用户超时时重定向的行为,您可以在您的AppServiceProvider
中使用CheckAuthTimeout::setRedirectTo()
来设置重定向回调。
class AppServiceProvider extends ServiceProvider { public function boot() { CheckAuthTimeout::setRedirectTo(function ($request, $guard){ return match($guard){ 'custom-guard' => route('some.route'), default => route('auth.login') } }); } }
AuthTimeout Facade
此包还提供以下方法的facade:
AuthTimeout::init() // Initialize the timeout session when no has been set yet. AuthTimeout::check($guard) // Check if a user has timed out and logs them out if so. AuthTimeout::hit() // Reset the user's timeout session. AuthTimeout::lastActiveAt() // The last activity time of the user.
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全漏洞
请参阅我们的安全策略,了解如何报告安全漏洞。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。