Laravel : 为父域设置 cookie
Laravel : Set cookie for parent domain
我正在尝试构建一个 SSO,它将在具有相同父域的不同应用程序之间共享。
假设我们有这些域:
sso.example.com
统一登录页面
app1.example.com
将重定向到登录页面的其他一些应用程序
当我在 SSO 项目的 .env
文件中设置 SESSION_DOMAIN=.example.com
时,cookie 确实设置在父域中并且可以从 APP1 访问。但是,该应用程序还会设置其他 cookie,例如 XSRF-TOKEN。我不想弄乱父域 cookie,因为还有另一个使用该域的独立应用程序。
我试图在每个请求的运行中更改 session.domain 属性 并在之后立即将其更改回来,但它不起作用。我猜 cookie 是在响应完成时设置的,但事实并非如此
Config::set('session.domain','.domain.com');
Cookie::queue('ssotoken', 'test', 60);
Config::set('session.domain','sso.domain.com');
那么有什么解决方法可以只为父域设置“ssotoken”吗?或者有没有办法从 Laravel 中的 app1.example.com
访问 sso.example.com
cookie?
您可以在第五个参数处为特定 cookie 设置 cookie 域,如下所示:
Cookie::queue(
name: 'ssotoken',
value: 'test',
domain: '.domain.com'
);
没有命名参数,您的方法调用如下所示:
Cookie::queue('ssotoken', 'test', 0, null, '.domain.com')
有点难看因为Cookie::queue
没有any documented parameters since they're using the array spread operator, to either accept an already invoked Cookie
class or allow to build a cookie without the setp of nesting two methods. The parameters supplied in the example correspond with those documented in the Cookie::make
方法:
Cookie::make(
string $name,
string $value,
int $minutes = 0,
string|null $path = null,
string|null $domain = null,
bool|null $secure = null,
bool $httpOnly = true,
bool $raw = false,
string|null $sameSite = null
)
我正在尝试构建一个 SSO,它将在具有相同父域的不同应用程序之间共享。 假设我们有这些域:
sso.example.com
统一登录页面
app1.example.com
将重定向到登录页面的其他一些应用程序
当我在 SSO 项目的 .env
文件中设置 SESSION_DOMAIN=.example.com
时,cookie 确实设置在父域中并且可以从 APP1 访问。但是,该应用程序还会设置其他 cookie,例如 XSRF-TOKEN。我不想弄乱父域 cookie,因为还有另一个使用该域的独立应用程序。
我试图在每个请求的运行中更改 session.domain 属性 并在之后立即将其更改回来,但它不起作用。我猜 cookie 是在响应完成时设置的,但事实并非如此
Config::set('session.domain','.domain.com');
Cookie::queue('ssotoken', 'test', 60);
Config::set('session.domain','sso.domain.com');
那么有什么解决方法可以只为父域设置“ssotoken”吗?或者有没有办法从 Laravel 中的 app1.example.com
访问 sso.example.com
cookie?
您可以在第五个参数处为特定 cookie 设置 cookie 域,如下所示:
Cookie::queue(
name: 'ssotoken',
value: 'test',
domain: '.domain.com'
);
没有命名参数,您的方法调用如下所示:
Cookie::queue('ssotoken', 'test', 0, null, '.domain.com')
有点难看因为Cookie::queue
没有any documented parameters since they're using the array spread operator, to either accept an already invoked Cookie
class or allow to build a cookie without the setp of nesting two methods. The parameters supplied in the example correspond with those documented in the Cookie::make
方法:
Cookie::make(
string $name,
string $value,
int $minutes = 0,
string|null $path = null,
string|null $domain = null,
bool|null $secure = null,
bool $httpOnly = true,
bool $raw = false,
string|null $sameSite = null
)