dongxun6458 2018-05-22 01:11
浏览 123
已采纳

在开发模式下使用laravel中的任何密码登录

We have auth() function in laravel and call it like this:

auth()->attempt(['email' => 'hello@gmail.com', 'password' => 123456]);

I want to set new provider for session guard and config it at config/auth.php too login user with any password in auth() function in development mode .

But i can't find a way to do it in my provider file!

  • 写回答

1条回答 默认 最新

  • dpa84373 2018-05-22 03:04
    关注

    This is a bad idea as you could inadvertently introduce a security risk into your production code. But in the interests of experimentation...

    One way would be to overwrite the validation in the default session guard, but I think this is too risky. So I would duplicate your existing production guard and and force it to die if not in development mode. Here's an example assuming you're using the session guard.


    In /app/Providers/AuthServiceProvider.php register a new guard:

        use App\Services\Auth\SillyGuard;
    

    ...

    public function boot()
    {
        $this->registerPolicies();
    
        Auth::extend('silly', function ($app, $name, array $config) {
            // Return an instance of Illuminate\Contracts\Auth\Guard...
    
            return new SillyGuard(Auth::createUserProvider($config['provider']));
        });
    }
    

    Add it to your /config/auth.php - you'll want to overwrite your existing web guard otherwise you'd have to redefine all your routes when you switch from development to production. You must switch this guard back to your original driver when you go to production mode

    'guards' => [
        //others
        'web' => [
            'driver' => 'silly',
            'provider' => 'users',
        ],
    ],
    

    Copy the file ...src/Illuminate/Auth/SessionGuard.php to app/Services/Auth/SillyGuard.php and make the following changes:

    //namespace Illuminate\Auth;
    namespace App\Services\Auth;
    

    ...

    public function __construct($name,
                                UserProvider $provider,
                                SessionInterface $session,
                                Request $request = null)
    {
         if (!\App::environment('local') || !config('app.debug')) {
             die("This guard only works in local debug mode");
         }
         $this->name...
    

    ...

     protected function hasValidCredentials($user, $credentials)
    {
        //return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
        return ! is_null($user);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂