Laravel:在多个域上共享会话数据

时间:2014-11-08 20:19:01

标签: session authentication laravel cross-domain

我在Laravel中构建一个多域/多店电子商务应用程序,并希望用户在商店之间更改时保持登录状态。

但据我所知,Laravel的Auth服务会将登录用户保存在会话中,其他域无法访问会话。

有没有办法(可能是一个包)来实现这一点,而不会让我的应用程序容易出现安全问题?

提前致谢!

6 个答案:

答案 0 :(得分:6)

  1. 捕获域A中的会话ID Session::getId()
  2. 通过HTTP POST将捕获的会话ID发送到域B
  3. 访问域B $sessionid_from_domainA = $_POST['session_from_A']
  4. 中已发送的会话ID
  5. 在域B Session::setId($sessionid_from_domainA)
  6. 中设置会话
  7. 在域B Session::start()
  8. 中启动会话

答案 1 :(得分:5)

  1. 在域A上创建一个类似<img src="https://DOMAINB.com/setcookie?id={{ Session::getId() }}" style="display:none;" />

  2. 的图像
  3. 在域B上创建如此路由:

  4. Route::get('setcookie', function(){
      Session::setId($_GET['id']);
      Session::start();
      return 'Cookie created';
    });`
    
    1. 完成,现在您应该可以通过$user = Auth::User;
    2. 获取用户

答案 2 :(得分:2)

我正在开发类似于单一登录系统的东西,仍在努力寻找解决方案,但这是一个开始http://laravel.io/forum/03-14-2014-multiple-domains-how-to-share-login

在laravel上,您可以将/app/config/session.php驱动程序更改为cookie


编辑:

这就是我所做的。

您可以使用像素图像共享域中的Cookie。 例如,当您在domain1.com上登录时,您想在domain2.com上创建一个cookie,在登录domain1.com之后,你需要有类似的东西

<img src="http://www.domain2.com/create-cookie?param=hash">

在domain2.com上面的路线:

  1. 将首先检查用户是否已登录
    1. 如果未登录将读取哈希值(例如电子邮件地址),请检查是否有用户使用该电子邮件,并在那里登录,同时设置cookie

答案 3 :(得分:1)

You can manually set which domain the session cookies will be registered, and thus have them persist across different sites. Just edit config/session.php, in the following section:

<?php
/*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => null,
?>

You're still restricted to a single top-level domain, though. So you can have store1.shops.com and store2.shops.com sharing sessions, but not myshop.com and shopsmart.com.

If you need something in this format, you'll probably fare better by creating an authentication service, and using access tokens to validate the credentials. You might also give a look at services like OneLogin.

答案 4 :(得分:1)

在这种情况下,如果要在多个子域之间共享会话,则必须设置域名config / session.php已设置域名。

示例:如果您有new.example.com和test.example.com,则必须将域名设置为example.com

'domain'=> env('SESSION_DOMAIN_URL','。example.com')

那里的解决方案对我有用,特别是设置域,然后清除我的浏览器Cookie和缓存。

答案 5 :(得分:-1)

在 sessionDomains 表单 db 或仅字符串中动态指定您的域 只需替换 best4games.com

Order

内核.php

Truck

及以上在优先组中开始会话

class SessionDomains
{
    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if($request->getHost() === 'best4games.com'){
            config([
                'session.domain' => '.best4games.com'
            ]);
        }
        return $next($request);
    }
}
相关问题