Laravel 通过密码限制站点

Laravel restrict site by password

我需要通过密码为我的 Laravel 5 站点提供身份验证(没有用户名,只有密码)。只有用户输入正确的密码才能访问站点。 它不能是基本的服务器授权,因为我必须实现一些设计。

我知道可以在 Laravel 中使用多重身份验证,但我不知道如何在我的应用程序中使用我的标准(用户和密码)身份验证。

请问有人可以帮忙吗?

构建了一个手动验证功能,仅检查密码。

    public function authenticate(Request $request)
    {
        $password=$request->get('password');
        if (Auth::attempt(['password' => $password]) )
        {     
            return redirect()->intended('/home');   
        }
        else 
        {
            return redirect('/login');
        }
     }

你可以这样做:

Route::get('/', function () {
    if (session('password_entered')) {
        return view('homepage');
    }

    return view('enter_password_page');
});

Route::post('/', function () {
    $password_to_website = 'RedMonkey';
    $password = request('password_field');

    if ($password && $password === $password_to_website) {
        session(['password_entered' => true]);
        return view('homepage');
    }

    return back()->withInput()->withErrors(['password' => 'Password is incorect']);
});

我没试过这个,但想法是存在的。你可以用它制作中间件,或者直接在 route/controller.

中使用它

如果我没有正确理解你的问题,你想在你的站点上只使用密码验证而不需要用户名。为此,我将执行以下操作:

  • 使用电子邮件 'passwordlogin@yoursite.com' 和您的 所需密码

  • 将您的登录表单更新为仅需要密码

    <form method="POST" action="/login">
    {!! csrf_field() !!}
    
    <input type="password" id="fieldPassword" name="password" required>
    <label for="fieldPassword" class="label">Password</label>
    
  • 更改您的 POST '/login' 路由以触发 Auth\AuthController class

    中名为 'passwordLogin' 的方法
    <?php
          Route::post('/login', 'Auth\AuthController@passwordLogin');
    
  • 在 Auth\AuthController class 中创建一个名为 'passwordLogin' 的方法,该方法使用 Auth::attempt() 来验证密码。将电子邮件地址硬编码为您在步骤 1 中设置的同一地址

    public function passwordLogin(Request $request){
    
        if($request->has('password')){
            if(Auth::attempt(['email' => 'passwordlogin@yoursite.com', 'password' => $request->input('password')])) {
                // User has the correct password
                return redirect()->('/home');
            }
        }
    
        // Login failed
        return redirect()->intended('/login');
    }