【问题标题】:Can I store an access Cookie in a Laravel session?我可以在 Laravel 会话中存储访问 Cookie 吗?
【发布时间】:2016-03-24 15:57:42
【问题描述】:

我正在使用通常通过 JavaScript 直接访问的远程 API。在正常流程中,用户通过发送 Auth 标头进行身份验证,并作为回报被授予 cookie。

我想做的是从 laravel 应用程序发送 auth 标头,在应用程序控制器中进行身份验证,并通过 laravel 控制器功能提供 API 访问。

我希望这会像验证和发送我的后续 API 调用一样简单,希望提供给 PHP 服务器的 cookie 将继续授予验证。

这不起作用,但现在我想我需要将我的访问 cookie 存储在 Session 中,并将其发送到标头中以供将来的 API 调用。

这行得通/我该怎么做?我的主管不想在远程服务器上实现 OAuth 类型的令牌,对我来说这似乎是最好的路线,所以我有点卡住了。

【问题讨论】:

    标签: laravel session authentication cookies


    【解决方案1】:

    Cookie 不能在多个主机之间共享。 cookie(在客户端)仅对设置它的路径有效。

    【讨论】:

      【解决方案2】:

      编辑 - 添加附加身份验证详细信息

      在 Laravel 中设置记住我

      1. 迁移(创建)用户表时添加 $table->rememberToken() 在您的用户表中创建该列。
      2. 当用户注册您的服务时,添加一个复选框以允许他们 做出决定,或者如果您不提供,您可以将其设置为真 用户选择步骤 3 中描述的选项

      < input type="checkbox" name="remember" >

      1. 在您的控制器中添加以下代码:

        if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) { // The user is being remembered... }

      Users 表必须包含每 1. 的字符串 remember_token 列,现在假设您已将令牌列添加到 User 表中,您可以将布尔值作为第二个参数传递给尝试方法,这将使用户无限期地通过身份验证,或直到他们手动注销。即 Auth::attempt([$creditentials], true);

      旁注:the Illuminate\Contracts\Auth\UserProvider 合约,公共函数updateRememberToken(Authenticatable $user, $token) 使用存储在 User 表中的用户 UID 和令牌来存储会话身份验证。

      验证一次:

      Laravel 有一个方法可以将用户登录到应用程序以获取单个请求。没有会话或 cookie。与无状态 API 一起使用。

      if (Auth::once($credentials)) {
          //
      }
      

      其他说明

      当用户注销时,记住 cookie 不会自动取消设置。但是,使用我在下面的 cookie 示例中解释的 cookie,您可以在注销后返回重定向响应之前将其添加到控制器中的注销函数中。

      public function logout() {
      // your logout code e.g. notfications, DB updates, etc
      
          // Get remember_me cookie name
          $rememberCookie = Auth::getRecallerName();
          // Forget the cookie
          $forgetCookie = Cookie::forget($rememberCookie);
      
          // return response (in the case of json / JS) or redirect  below will work 
          return Redirect::to('/')->withCookie($forgetCookie);
      
          OR you could q$ueue it up for later if you are elsewhere and cannot return a response immediately
          Cookie::queue(forgetCookie);
      
      }
      

      可能对您有所帮助的基本通用 cookie 示例。使用 Laravel 服务提供者有更好的方法来做到这一点

      // cookie key
      private $myCookieKey = 'myAppCookie';
      // example of cookie value but can be any string
      private $cookieValue = 'myCompany';
      
      // inside of a controller or a protected abstract class in Controller, 
      // or setup in a service ... etc.
      protected function cookieExample(Request $request)
      {
          // return true if cookie key 
          if ($request->has($this->myCookieKey)) {
              $valueInsideOfCookie = Cookie::get($this->myCookieKey);
              // do something with $valueInsideOfCookie
      
          } else {
              // queue a cookie with the next response
              Cookie::queue($this->myCookieKey, $this->cookieValue);
          }
      }
      
      public function exampleControllerFunction(Request $request)
      {
          $this->cookieExample($request);
          // rest of function one code
      }
      
      public function secondControllerFunction(Request $request)
      {
          $this->cookieExample($request);
          // rest of function two code 
      }
      

      【讨论】:

      • Cookie::get() 是否仅在同一会话期间设置 cookie 时才返回值?即我可以使用Cookie::queue() 保存API 给我的user1 cookie,然后使用Cookie::get()user1 和其他人提出的未来请求重建所述cookie?
      • 另外,考虑到这与使用cookie会话驱动程序存储在Session中没有什么不同,这是否意味着可以将cookie存储在用户Session中?
      • @thedarklord47 好的,以确保我在回答之前理解您的问题。用户注册或验证后,您希望他们能够将 api 用于 1. 验证会话或 2. 无限期而无需再次验证?
      • @thedarklord47我添加了一个编辑部分来回答身份验证问题以及如何使用记住我的 cookie。如果您需要更多详细信息,请告诉我
      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 2013-01-06
      • 2015-06-21
      • 2011-06-12
      • 1970-01-01
      • 2010-09-21
      • 2011-08-09
      • 1970-01-01
      相关资源
      最近更新 更多