简体   繁体   English

Laravel通过密码限制网站

[英]Laravel restrict site by password

I need to provide authentication my Laravel 5 site by password (no username, just only password). 我需要通过密码(没有用户名,只有密码)提供对Laravel 5网站的身份验证。 Access to site will possible only if user type correct password. 仅当用户键入正确的密码时,才可以访问网站。 It can't be basic server authorization, because I have to implement some design. 它不能是基本的服务器授权,因为我必须实现一些设计。

I know it is possible to do it with multiple authentication in Laravel, but I have no idea how to make it working with my standard (user and password) authentication in my application. 我知道可以在Laravel中使用多重身份验证来做到这一点,但我不知道如何使其与我的应用程序中的标准(用户和密码)身份验证一起使用。

Can someone help how to do it, please? 有人可以帮忙吗?

built a manual authentication function to check for only password. 内置了手动身份验证功能,仅检查密码。

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

You could do something like: 您可以执行以下操作:

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']);
});

I have not tried this but the idea is there. 我没有尝试过,但是这个想法就在那里。 You can make middleware out of this or just use it straight up in route/controller. 您可以使用此中间件,也可以直接在路由/控制器中直接使用它。

If I've understood your question correctly, you want to have just password authentication on your site without the need for a username. 如果我正确理解了您的问题,那么您只希望在站点上进行密码身份验证而无需用户名。 To accomplish this I would do the following: 为此,我将执行以下操作:

  • Create a user with the email 'passwordlogin@yoursite.com' and your desired password 使用电子邮件“ passwordlogin@yoursite.com”和所需密码创建一个用户

  • Update your login form to only require a password 更新您的登录表单以仅要求输入密码

     <form method="POST" action="/login"> {!! csrf_field() !!} <input type="password" id="fieldPassword" name="password" required> <label for="fieldPassword" class="label">Password</label> 
  • Change your POST '/login' route to trigger a method called 'passwordLogin' in the Auth\\AuthController class 更改POST“ / login”路由以触发Auth \\ AuthController类中名为“ passwordLogin”的方法

     <?php Route::post('/login', 'Auth\\AuthController@passwordLogin'); 
  • Create a method called 'passwordLogin' in the Auth\\AuthController class that uses Auth::attempt() to verify a password. 在Auth \\ AuthController类中创建一个名为“ passwordLogin”的方法,该方法使用Auth :: attempt()来验证密码。 Hardcode the email address to the same one you set in step 1 将电子邮件地址硬编码为您在步骤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'); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM