Php 如何使用Laravel对页面进行密码保护?

Php 如何使用Laravel对页面进行密码保护?,php,ajax,laravel,Php,Ajax,Laravel,我正在尝试在Laravel中设置一些需要密码才能查看的页面 页面是一个模型,称为页面 每个页面都有一个关联的密码,存储在Pages数据库表中 模式 Schema::create('pages', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('client_id'); $table->stri

我正在尝试在Laravel中设置一些需要密码才能查看的页面

页面是一个模型,称为页面

每个页面都有一个关联的密码,存储在Pages数据库表中

模式

Schema::create('pages', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->string('client_id');
    $table->string('url');
    $table->text('content');
    $table->string('password');
    $table->timestamps();
});
我有一个路由,例如,
route::get('/page/{url}','PageController@index“)->中间件(“门”)将显示“页面”,它只是一个刀片/vue文件,带有注入模板的特定信息。
这些页面允许用户通过AJAX上传文件

目前,我创建了一些中间件来实现实际的身份验证部分

中间件

public function handle($request, Closure $next)
{
    if(Cookie::get($request->route('url'))){
        return $next($request);
    }
    session(['page' => $request->route('url')]);
    return redirect()->route('gate',['page'=> $request->route('url')]);
}
PublicController.php

public function gate_check(Request $request)
{
  //this part just ensures the correct page is loaded after being authed
  $past = parse_url($request->session()->get('_previous')['url']);

  if(array_key_exists('query', $past)){
    $page = str_replace('page=', '',parse_url($request->session()->get('_previous')['url'])['query']);

    $stored_password = Page::where('url', $page)->first()->password;
    $password = $request->password;  

    if($password === $stored_password){
      //if password matches, then redirect to that page
      return redirect()->route('page', session('page'));
    } else {
      //else redirect them back to the login page to try again
      return back();
    }
  } else {
    //if there is no page information that use should go to, just send them to google instead
    return redirect('https://google.com');
  }

}
中间件/auth方法的思想是,如果用户未经身份验证,则将其重定向到登录页面此登录页面仅包含您需要输入的密码。

一旦他们输入,我就设置了一个cookie,这样他们就可以避免再次登录

我现在意识到这是不安全的,或者至少看起来是这样的,因为cookie的过期时间可以由客户端/用户操纵,从而使他们能够永远保持登录状态。

我只想重复上面描述的方法是有效的,但它是不安全的。我还应该重申,这些“页面”允许用户通过ajax上传文件。只有在用户位于特定页面(如CSRF)时,才允许用户上传

我需要一种安全的密码保护页面的方法,可以自定义“会话”的到期时间。我还需要一种方法来“刷新”或“扩展”活动会话,而不需要使用AJAX进行页面刷新,这样用户就可以停留在上传页面上(以防上传需要很长时间)

带有用户名/电子邮件和密码的标准用户帐户不适用。仅限密码。

Wordpress内置了这一功能——为什么对于看起来如此琐碎的事情,要用Laravel做起来如此困难


您将如何处理此问题?

编写您自己的身份验证解决方案很难。是否有可能重新考虑保护页面的策略

如果您能够遵循正常的基于用户的身份验证策略,那么您可以利用Laravel提供的身份验证服务,它还可以通过消除您可能遇到的边缘案例提供其他优势

根据需要验证的页数,可以将每个页面的布尔列添加到用户表中,也可以添加sting类型的“pages”列,其中包含该用户有权访问的页面列表

然后,可以使用Laravel授权策略()强制对页面的访问,在刀片服务器中,您可以使用该策略构建用户可用的页面动态列表()

其他好处: -避免为每个页面共享公共密码。您可能不知道密码是否已在授权用户组之外共享,更改密码将影响页面的所有用户

  • 如果用户忘记密码,则可以使用标准密码重置过程

  • 如果用户可以访问多个页面,并且当一个域有多个密码时,密码管理器会遇到困难,则用户不需要跟踪多个密码

  • 了解用户可以访问哪些页面可以通过提供一个列出特定用户可用的所有页面的主页来提高可用性


    • 编写自己的身份验证解决方案很难。是否有可能重新考虑保护页面的策略

      如果您能够遵循正常的基于用户的身份验证策略,那么您可以利用Laravel提供的身份验证服务,它还可以通过消除您可能遇到的边缘案例提供其他优势

      根据需要验证的页数,可以将每个页面的布尔列添加到用户表中,也可以添加sting类型的“pages”列,其中包含该用户有权访问的页面列表

      然后,可以使用Laravel授权策略()强制对页面的访问,在刀片服务器中,您可以使用该策略构建用户可用的页面动态列表()

      其他好处: -避免为每个页面共享公共密码。您可能不知道密码是否已在授权用户组之外共享,更改密码将影响页面的所有用户

      • 如果用户忘记密码,则可以使用标准密码重置过程

      • 如果用户可以访问多个页面,并且当一个域有多个密码时,密码管理器会遇到困难,则用户不需要跟踪多个密码

      • 了解用户可以访问哪些页面可以通过提供一个列出特定用户可用的所有页面的主页来提高可用性


        • 使用会话而不是cookie。中间件的更改

          public function handle($request, Closure $next)
          {   
          
              if(in_array($request->route('url'), session('pages',[]))){
                 return $next($request);
              }
          
              session(["current-page"=>$request->route('url')]);
              return redirect()->route('gate');
          }
          
          public function gate_check(Request $request)
          {
              if(session()->has("current-page")){
          
                  $stored_password = Page::where('url', session("current-page"))
                     ->first()->password;
          
                  if($request->password === $stored_password){
                      $pages = session("pages",[]);
                      array_push($pages, session("current-page"));
                      session(["pages"=> $pages]);
                      //if password matches, then redirect to that page
                      return redirect()->route('page', ["url" => session("current-page")]);
                  } else {
                      //else redirect them back to the login page to try again
                      return back();
                  }
              } else {
                  //if there is no page information that use should go to, just send them 
                  // to google instead
                  return redirect('https://google.com');
              }
          }
          
          控制器的更改:

          public function handle($request, Closure $next)
          {   
          
              if(in_array($request->route('url'), session('pages',[]))){
                 return $next($request);
              }
          
              session(["current-page"=>$request->route('url')]);
              return redirect()->route('gate');
          }
          
          public function gate_check(Request $request)
          {
              if(session()->has("current-page")){
          
                  $stored_password = Page::where('url', session("current-page"))
                     ->first()->password;
          
                  if($request->password === $stored_password){
                      $pages = session("pages",[]);
                      array_push($pages, session("current-page"));
                      session(["pages"=> $pages]);
                      //if password matches, then redirect to that page
                      return redirect()->route('page', ["url" => session("current-page")]);
                  } else {
                      //else redirect them back to the login page to try again
                      return back();
                  }
              } else {
                  //if there is no page information that use should go to, just send them 
                  // to google instead
                  return redirect('https://google.com');
              }
          }
          
          您可以使用哈希密码,使用MD5、SHA256或SHA512算法保存在数据库中。 有关会话的更多信息,请学习Laravel会话文档

          您可以配置session.php,位置:根文件夹/config/session.php。例如,当用户关闭浏览器然后销毁会话时

          'expire_on_close' => true 
          

          使用会话而不是cookie。中间件的更改

          public function handle($request, Closure $next)
          {   
          
              if(in_array($request->route('url'), session('pages',[]))){
                 return $next($request);
              }
          
              session(["current-page"=>$request->route('url')]);
              return redirect()->route('gate');
          }
          
          public function gate_check(Request $request)
          {
              if(session()->has("current-page")){
          
                  $stored_password = Page::where('url', session("current-page"))
                     ->first()->password;
          
                  if($request->password === $stored_password){
                      $pages = session("pages",[]);
                      array_push($pages, session("current-page"));
                      session(["pages"=> $pages]);
                      //if password matches, then redirect to that page
                      return redirect()->route('page', ["url" => session("current-page")]);
                  } else {
                      //else redirect them back to the login page to try again
                      return back();
                  }
              } else {
                  //if there is no page information that use should go to, just send them 
                  // to google instead
                  return redirect('https://google.com');
              }
          }
          
          控制器的更改:

          public function handle($request, Closure $next)
          {   
          
              if(in_array($request->route('url'), session('pages',[]))){
                 return $next($request);
              }
          
              session(["current-page"=>$request->route('url')]);
              return redirect()->route('gate');
          }
          
          public function gate_check(Request $request)
          {
              if(session()->has("current-page")){
          
                  $stored_password = Page::where('url', session("current-page"))
                     ->first()->password;
          
                  if($request->password === $stored_password){
                      $pages = session("pages",[]);
                      array_push($pages, session("current-page"));
                      session(["pages"=> $pages]);
                      //if password matches, then redirect to that page
                      return redirect()->route('page', ["url" => session("current-page")]);
                  } else {
                      //else redirect them back to the login page to try again
                      return back();
                  }
              } else {
                  //if there is no page information that use should go to, just send them 
                  // to google instead
                  return redirect('https://google.com');
              }
          }
          
          您可以使用哈希密码,使用MD5、SHA256或SHA512算法保存在数据库中。 有关会话的更多信息,请学习Laravel会话文档

          您可以配置session.php,位置:根文件夹/config/session.php。例如,当用户关闭浏览器然后销毁会话时

          'expire_on_close' => true 
          

          我会这样做:

          不支持使用用户名/电子邮件和密码的标准用户帐户 可应用的仅限密码