Laravel 系统版本查看及artisan管理员密码找回方法针对各个版本通用方法及原理-优雅草卓伊凡

Laravel 系统版本查看及artisan管理员密码找回方法针对各个版本通用方法及原理-优雅草卓伊凡

一、查看 Laravel 版本的方法

优雅草蜻蜓T会议系统专业版 最近又有客户要了,但是发现 密码不对 管理员账户密码不对,卓伊凡必须处理下,这里顺便讲解密码原理

1. 通过命令行查看(最准确)

复制代码
php artisan --version
# 示例输出:Laravel Framework 10.10.0

我输出 版本 又遇到 问题了,不过已经解决

Laravel Framework 8.83.27

我们的版本是8

其实确实算比较新了,

2. 通过 composer.json 文件查看

复制代码
cat composer.json | grep laravel/framework
# 示例输出:"laravel/framework": "^10.0"

看了看composer.json

没问题 首任主程 写的很好

没问题 除了版本没写 其他依赖都有 能看到 也利于维护

3. 通过 PHP 代码查看

复制代码
// 在路由或控制器中添加
Route::get('/version', function() {
    return app()->version();
});
// 访问 /version 即可看到

二、不同 Laravel 版本找回 admin 密码的方法

通用方法(适用于所有版本)

方法1:使用 Tinker 重置密码
复制代码
php artisan tinker

# 在 tinker 中执行:
$user = App\Models\User::where('email', 'admin@example.com')->first();
$user->password = Hash::make('new_password');
$user->save();

不过这里要改改 我们字段是username 然后是admin

$user = App\Models\User::where('username', 'admin')->first();

$user->password = Hash::make('密码');

$user->save();

因此应该这样执行,这里脱敏我写的密码

提示我是未知字段,失败了,

方法2:数据库直接更新
复制代码
UPDATE users SET password = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi' WHERE email = 'admin@example.com';
# 上面密码对应明文是 "password"

这个方法 应该可行性高一些,

版本特定方法

Laravel 5.x - 7.x
复制代码
php artisan make:command ResetAdminPassword

然后在生成的命令文件中:

复制代码
public function handle()
{
    $user = User::where('email', 'admin@example.com')->first();
    $user->password = bcrypt('new_password');
    $user->save();
    $this->info('Password reset successfully!');
}
Laravel 8.x 及以上
复制代码
php artisan make:command ResetAdminPassword

命令文件内容:

复制代码
public function handle()
{
    $user = \App\Models\User::where('email', 'admin@example.com')->first();
    $user->forceFill([
        'password' => \Illuminate\Support\Facades\Hash::make('new_password')
    ])->save();
    $this->info('Password reset successfully!');
}

刚刚我们已经看过我们的版本是8了 所以我们用这个方法应该才是最可靠的

根据情况 我们创建应该是

在 Laravel 项目中,使用 php artisan make:command ResetAdminPassword 生成的命令文件默认会存放在以下路径:

标准路径

复制代码
app/Console/Commands/ResetAdminPassword.php

详细说明

  1. 文件完整路径结构

    your-project/
    ├── app/
    │ ├── Console/
    │ │ ├── Commands/
    │ │ │ └── ResetAdminPassword.php ← 这就是生成的文件
    │ │ └── Kernel.php

  2. 文件内容示例
    生成的 ResetAdminPassword.php 会包含类似这样的基础结构:

    <?php

    namespace App\Console\Commands;

    use Illuminate\Console\Command;

    class ResetAdminPassword extends Command
    {
    protected signature = 'command:name'; protected description = 'Command description';

    复制代码
     public function __construct()
     {
         parent::__construct();
     }
    
     public function handle()
     {
         // 命令逻辑代码
     }

    }

  3. 如何验证文件是否存在

    • 命令行方式:

      ls app/Console/Commands/ResetAdminPassword.php

    • 或使用PHP检查:

      php -r "echo file_exists('app/Console/Commands/ResetAdminPassword.php') ? '存在' : '不存在';"

  1. 如果找不到文件
    • 检查是否在项目根目录执行命令
    • 运行 composer dump-autoload 重新生成自动加载
    • 检查 app/Console/Commands/ 目录是否存在
  1. 注册命令
    生成后需要在 app/Console/Kernel.php$commands 数组中添加:

    protected $commands = [
    Commands\ResetAdminPassword::class,
    ];

  2. 使用命令
    注册后可以通过以下方式执行:

    php artisan list # 查看可用命令
    php artisan your:command-name # 执行你的命令

提示:如果你自定义了 Laravel 的目录结构,文件可能会出现在你配置的对应路径中,可以通过检查 composer.jsonautoload.psr-4 配置确认实际路径。

我们打开查看到原始内容

修改成我们的内容

public function handle()

{

$user = \App\Models\User::where('username', 'admin')->first();

$user->forceFill([

'password' => \Illuminate\Support\Facades\Hash::make('123456')

])->save();

$this->info('Password reset successfully!');

}

默认改123456

验证命令签名

打开 ResetAdminPassword.php 文件,检查 $signature

protected $signature = 'admin:reset-password';

注册命令

在 app/Console/Kernel.php 中添加:

那么我执行命令为

php artisan admin:reset-password

这样就可以重置密码

完整重置密码,nice!卓伊凡!

使用 Laravel Breeze/Jetstream 的情况

Laravel 8+ 使用 Jetstream
复制代码
php artisan tinker
$user = \App\Models\User::where('email', 'admin@example.com')->first();
$user->forceFill(['password' => \Illuminate\Support\Facades\Hash::make('new_password')])->save();
Laravel 8+ 使用 Sanctum
复制代码
php artisan tinker
$user = \App\Models\User::find(1);
$user->password = \Illuminate\Support\Facades\Hash::make('new_password');
$user->save();

三、预防措施

  1. 创建密码重置命令

    php artisan make:command ResetPasswordCommand

  2. 添加管理员种子用户

    // 在 DatabaseSeeder.php
    User::create([
    'name' => 'Admin',
    'email' => 'admin@example.com',
    'password' => Hash::make('temp_password'),
    'is_admin' => true
    ]);

  3. 设置密码过期策略(Laravel 8+):

    // 在 AppServiceProvider.php
    \Illuminate\Auth\Passwords\PasswordBrokerManager::macro('setDefaultPasswordTimeout', function(timeout) { config(['auth.passwords.users.expire' => timeout]);
    });

四、安全建议

  1. 重置后立即修改临时密码

  2. 使用强密码生成器:

    use Illuminate\Support\Str;
    $password = Str::password(12); // Laravel 9+

  3. 考虑使用双重认证

  4. 定期轮换管理员密码

以上方法覆盖了从 Laravel 5.x 到最新版本的管理员密码找回需求

整体来说 laravel 真的 很安全的了

相关推荐
BingoGo2 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack2 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo3 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack3 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack4 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo4 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
BingoGo5 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·laravel
JaguarJack5 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082855 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php