sicaboy/laravel-security

此包可用于增强Laravel项目的用户安全性。

1.2 2023-04-28 12:37 UTC

This package is auto-updated.

Last update: 2024-09-28 15:54:50 UTC


README

Latest Stable Version License Total Downloads

简介

此包可用于增强Laravel项目的用户安全性。

安装

需求

要获取Laravel Security的最新版本,只需运行

composer require sicaboy/laravel-security

然后执行vendor publish

php artisan vendor:publish --provider="Sicaboy\LaravelSecurity\LaravelSecurityServiceProvider"

发布后,您可以在以下位置修改模板和配置

app/config/laravel-security.php
resources/views/vendor/laravel-security/
resources/lang/en/laravel-security.php

如果您使用的是Laravel < 5.5,则需要注册服务提供者。打开 config/app.php 并将以下内容添加到 providers 数组中

Siaboy\LaravelSecurity\LaravelSecurityServiceProvider::class,

功能

禁止用户使用常用密码或已使用过的密码

验证用户提供的密码不是由可敬的IT安全分析师分析的顶级10,000个最糟糕密码之一。 了解更多 这里这里(wired)这里(telegram)

可用的验证规则

// Add rule instance to the field validation rules list
public function rules()
{
    return [
        'password_field' => [
            'required',
            'confirmed',
            'min:8',
            'regex:/[a-z]/',      // must contain at least one lowercase letter
            'regex:/[A-Z]/',      // must contain at least one uppercase letter
            'regex:/[0-9]/',      // must contain at least one digit
            //...
            new \Sicaboy\LaravelSecurity\Rules\NotCommonPassword(),
            new \Sicaboy\LaravelSecurity\Rules\NotAUsedPassword($user),
        ],
    ];
}
// Also you need to call event, examples in the next section

注意:需要额外调用的事件

用户登录和注册事件已自动追踪。同时,您还需要添加一个额外的事件来显式调用。

// Call on user password change
event(new \Illuminate\Auth\Events\PasswordReset($user));

// If you are using custom login, register and reset password actions which are not the Laravel built-in ones, you will need to call event in your function accordingly.
event(new \Illuminate\Auth\Events\Login($user)); 
event(new \Illuminate\Auth\Events\Registered($user));
event(new \Illuminate\Auth\Events\PasswordReset($user)); 

用法

密码策略

  • 删除长时间未活动的账户
  • 锁定长时间未活动的账户
  • 每x天强制更改密码
  1. 要启用前两个策略,您需要在以下 config/laravel-security.php 中将 enabled 设置为 true
...
'password_policy' => [
    // Delete accounts with days of no activity
    'auto_delete_inactive_accounts' => [
        'enabled' => true,
        ...
    ],

    // Lock out accounts with days of no activity
    'auto_lockout_inactive_accounts' => [
        'enabled' => true,
        ...
    ],
]
...
  1. 要拒绝锁定账户并强制用户每x天更改密码,您需要使用此中间件
Route::middleware(['security'])->group(function () {
    ...
});

如果使用不同的用户对象

  • 如果您使用不同的 User 对象,例如传统的 App\User 和自定义管理员用户,您可以按以下方式编写中间件
Route::middleware(['security:admin'])->group(function () {
  ...
});
  • 在您的 config/laravel-security.php 中添加配置组
 return [
     'default' => [
         ...
     ],
     'group' 
         'admin' => [ // Example, when using middleware 'security:admin'. Attributes not mentioned will be inherit from `default` above
            ...
         ],
         'other_name' => [ // Middleware 'security:other_name'
             ...
         ]
     ],
  1. 要启用 每x天强制更改密码,您需要在 config/laravel-security.php 中将 enabled 设置为 true 并设置 change_password_url
...
'password_policy' => [
    ...
    // Force change password every x days
    'force_change_password' => [
        'enabled' => true,
        'days_after_last_change' => 90, // every 90 days
        'change_password_url' => '/user/change-password', // Change My Password page URL
    ],
    ...
]
...
  1. 将以下命令添加到您的应用程序的 app/Console/Kernel.php 中。 如果使用Web服务器集群,请实现一个实例
protected function schedule(Schedule $schedule)
{
    $schedule->command(\Sicaboy\LaravelSecurity\Console\Commands\DeleteInactiveAccounts::class)
             ->hourly();
    $schedule->command(\Sicaboy\LaravelSecurity\Console\Commands\LockoutInactiveAccounts::class)
             ->hourly();
    ...
}
  1. 确保您在crontab中添加了 Laravel 调度器 如果使用Web服务器集群,请实现一个实例
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

多因素认证

此功能已移至 sicaboy/laravel-mfa

待办事项

  • extended_security 表拆分为多个表,或采用其他方法以支持用户众多的网站。

  • 添加cron作业以删除过旧密码记录,避免表过重。

更新日志

请参阅 更新日志 了解最近更改的更多信息。

贡献

请随意分支此包,并通过提交拉取请求来增强功能进行贡献。

许可证

MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。