Home > Backend Development > PHP Tutorial > PHP single sign-on SSO implementation method

PHP single sign-on SSO implementation method

小云云
Release: 2023-03-21 21:08:02
Original
12556 people have browsed it

SSO is single sign-on, which is a kind of access permission to control multiple related but independent systems. Users with this permission can use a single ID and password to access one or more systems to avoid using different Username or password, or some configuration to log in to each system seamlessly.

For large systems, using single sign-on can reduce a lot of trouble for users. Take Baidu as an example. There are many subsystems under Baidu - Baidu Experience, Baidu Knows, Baidu Library, etc. If we use these systems, each system requires us to enter a user name and password to log in once. I I believe the user experience will definitely plummet.

Two elements that interact with SSO: 1. User, 2. System, its characteristics are: One login, all access. SSO is a type of access control, which controls whether users can log in, that is, verifies the user's identity, and all other system authentication is performed here. Looking at SSO from the entire system level, its core is these three elements. : 1. User, 2. System, 3. Authentication center.

PHP single sign-on SSO implementation method

1. How to perform single sign-on for the same domain but different subdomains

Suppose our site is deployed according to the following domain name:
sub1.onmpw.com
sub2.onmpw.com

These two sites share the same domain onmpw.com.

By default, the browser will send the host corresponding to the domain to which the cookie belongs. In other words, the default domain for cookies from sub1.onmpw.com is .sub1.onmpw.com. Therefore, sub2.onmpw.com will not get any cookie information belonging to sub1.onmpw.com. Because they are on different hosts, and their subdomains are also different.

1.1 Set the cookie information of both under the same domain

  • Log in sub1.onmpw.com system

  • Log in After success, a unique identifier Token is generated (if you know the token, you will know which user is logged in). Set cookie information. It should be noted here that the Token is stored in the cookie, but when setting it, the domain to which this cookie belongs must be set to the top-level domain .onmpw.com. The setcookie function can be used here. The fourth parameter of this function is used to set the domain of the cookie.

setcookie(‘token’, ’xxx’, '/', ’.onmpw.com’);
Copy after login
  • When you visit the sub2.onmpw.com system, the browser will send the information token in the cookie to the sub2.onmpw.com system along with the request. . At this time, the system will first check whether the session is logged in. If not logged in, it will verify the token in the cookie to achieve automatic login.

  • sub2.onmpw.com Write session information after successful login. For future verification, just use your own session information to verify it.

1.2 Log out

There is a problem here is that after the sub1 system logs out, in addition to clearing its own session information and the cookie information of the domain .onmpw.com . It does not clear the session information of the sub2 system. That sub2 is still logged in. In other words, although this method can achieve single sign-on, it cannot achieve simultaneous logout. The reason is that although sub1 and sub2 can share cookies through the setting of the setcookie function, their sessionIds are different, and this sessionId is also stored in the form of a cookie in the browser, but the domain it belongs to is not .onmpw. com.

So how to solve this problem? We know that in this case, as long as the sessionId of the two systems is the same, this problem can be solved. That is to say, the domain to which the cookie storing sessionId belongs is also .onmpw.com. In PHP, sessionId is generated after session_start() is called. If you want sub1 and sub2 to have the same sessionId, you must set the domain to which the sessionId belongs before session_start():

ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '.onmpw.com');
ini_set('session.cookie_lifetime', '0');
Copy after login
Copy after login

1. After the above steps, you can achieve single sign-on for different second-level domain names. and exit.
2. However, it can be simplified further. For example, by ensuring that sessionId is the same, single sign-on and logout of different second-level domain names can be achieved.
Reference article: https://www.onmpw.com/tm/xwzj/network_145.html

2. How to implement single sign-on between different domains

Suppose we need to To implement single sign-on between the following stations

www.onmpw1.com
www.onmpw2.com
www.onmpw3.com

The above solution will not work.

There is currently an open source SSO solution on github, and the implementation principle is similar to that of mainstream SSO:
https://github.com/jasny/sso
wiki
demo

Core principle:
1, The client accesses different subsystems, and the SSO user service center corresponding to the subsystem uses the same SessionId.
2, The subsystem Broker and Server are authorized link binding through attach

同根域名不指定 domain 根域名,第一次授权需要每次都要跳转到授权服务,但是指定了domain, 同根域名只要有一个授权成功,都可以共用那个 cookie 了,下次就不用再授权了,直接就可以请求用户信息了。

第一次访问A:
PHP single sign-on SSO implementation method

第二次访问B:
PHP single sign-on SSO implementation method

2.1 登录状态判断

用户到认证中心登录后,用户和认证中心之间建立起了会话,我们把这个会话称为全局会话。当用户后续访问系统应用时,我们不可能每次应用请求都到认证中心去判定是否登录,这样效率非常低下,这也是单Web应用不需要考虑的。

我们可以在系统应用和用户浏览器之间建立起局部会话,局部会话保持了客户端与该系统应用的登录状态,局部会话依附于全局会话存在,全局会话消失,局部会话必须消失。

用户访问应用时,首先判断局部会话是否存在,如存在,即认为是登录状态,无需再到认证中心去判断。如不存在,就重定向到认证中心判断全局会话是否存在,如存在,通知该应用,该应用与客户端就建立起它们之间局部会话,下次请求该应用,就不去认证中心验证了。

2.2 退出

用户在一个系统退出了,访问其它子系统,也应该是退出状态。要想做到这一点,应用除结束本地局部会话外,还应该通知认证中心该用户退出。

认证中心接到退出通知,即可结束全局会话,用户访问其它应用时,都显示已登出状态。

需不需要立即通知所有已建立局部会话的子系统,将它们的局部会话销毁,可根据实际项目来。

SSO( Single Sign On ),即单点登录,是一种控制多个相关但彼此独立的系统的访问权限, 拥有这一权限的用户可以使用单一的ID和密码访问某个或多个系统从而避免使用不同的用户名或密码,或者通过某种配置无缝地登录每个系统 。

对于大型系统来说使用单点登录可以减少用户很多的麻烦。就拿百度来说吧,百度下面有很多的子系统——百度经验、百度知道、百度文库等等,如果我们使用这些系统的时候,每一个系统都需要我们输入用户名和密码登录一次的话,我相信用户体验肯定会直线下降。

与 SSO 交互的2个元素:1. 用户,2. 系统,它的特点是:一次登录,全部访问。SSO 是访问控制的一种,控制用户能否登录,即验证用户身份,而且是所有其它系统的身份验证都在它这里进行,从整个系统层面来看 SSO ,它的核心就是这3个元素了:1. 用户,2. 系统,3. 验证中心。

PHP single sign-on SSO implementation method

1、同一个域但不同的子域如何进行单点登录

假如我们的站点是按照下面的域名进行部署的:
sub1.onmpw.com
sub2.onmpw.com

这两个站点共享同一域 onmpw.com 。

默认情况下,浏览器会发送 cookie 所属的域对应的主机。也就是说,来自于 sub1.onmpw.com 的 cookie 默认所属的域是 .sub1.onmpw.com 。因此,sub2.onmpw.com 不会得到任何的属于 sub1.onmpw.com 的 cookie 信息。因为它们是在不同的主机上面,并且二者的子域也是不同的。

1.1 设置二者的 cookie 信息在同一个域下

  • 登录 sub1.onmpw.com 系统

  • 登录成功以后,生成唯一标识符Token(知道token,就知道哪个用户登录了)。设置 cookie 信息,这里需要注意,将Token存到 cookie 中,但是在设置的时候必须将这 cookie 的所属域设置为顶级域 .onmpw.com 。这里可以使用 setcookie 函数,该函数的第四个参数是用来设置 cookie 所述域的。

setcookie(‘token’, ’xxx’, '/', ’.onmpw.com’);
Copy after login
  • 访问 sub2.onmpw.com 系统,浏览器会将 cookie 中的信息 token 附带在请求中一块儿发送到 sub2.onmpw.com 系统。这时该系统会先检查 session 是否登录,如果没有登录则验证 cookie 中的 token 从而实现自动登录。

  • sub2.onmpw.com 登录成功以后再写 session 信息。以后的验证就用自己的 session 信息验证就可以了。

1.2 退出登录

这里存在一个问题就是 sub1 系统退出以后,除了可以清除自身的 session 信息和所属域为 .onmpw.com 的 cookie 的信息。它并不能清除 sub2 系统的 session 信息。那 sub2 仍然是登录状态。也就是说,这种方式虽说可以实现单点登录,但是不能实现同时退出。原因是,sub1 和 sub2 虽说通过 setcookie 函数的设置可以共享 cookie,但是二者的sessionId 是不同的,而且这个 sessionId 在浏览器中也是以 cookie 的形式存储的,不过它所属的域并不是 .onmpw.com 。

那如何解决这个问题呢?我们知道,对于这种情况,只要是两个系统的 sessionId 相同就可以解决这个问题了。也就是说存放 sessionId 的 cookie 所属的域也是 .onmpw.com 。在PHP中,sessionId 是在 session_start() 调用以后生成的。要想使sub1 和 sub2 有共同的 sessionId ,那必须在 session_start() 之前设置 sessionId 所属域:

ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '.onmpw.com');
ini_set('session.cookie_lifetime', '0');
Copy after login
Copy after login

1、经过上面的步骤就可以实现不同二级域名的单点登录与退出。
  2、不过还可以再简化,如确保 sessionId 相同就可以实现不同二级域名的单点登录与退出。
 参考文章: https://www.onmpw.com/tm/xwzj/network_145.html

2、不同域之间如何实现单点登录

假设我们需要在以下这些站之间实现单点登录

www.onmpw1.com
www.onmpw2.com
www.onmpw3.com

上面的方案就行不通了。

目前 github 上有开源的 SSO 解决方案,实现原理与主流 SSO 差不多:
https://github.com/jasny/sso
wiki
demo

核心原理:  
1、客户端访问不同的子系统,子系统对应的 SSO 用户服务中心使用相同的 SessionId
2、子系统 Broker 与 Server 间通过 attach 进行了授权链接绑定

同根域名不指定 domain 根域名,第一次授权需要每次都要跳转到授权服务,但是指定了domain, 同根域名只要有一个授权成功,都可以共用那个 cookie 了,下次就不用再授权了,直接就可以请求用户信息了。

第一次访问A:
PHP single sign-on SSO implementation method

第二次访问B:
PHP single sign-on SSO implementation method

2.1 登录状态判断

用户到认证中心登录后,用户和认证中心之间建立起了会话,我们把这个会话称为全局会话。当用户后续访问系统应用时,我们不可能每次应用请求都到认证中心去判定是否登录,这样效率非常低下,这也是单Web应用不需要考虑的。

我们可以在系统应用和用户浏览器之间建立起局部会话,局部会话保持了客户端与该系统应用的登录状态,局部会话依附于全局会话存在,全局会话消失,局部会话必须消失。

用户访问应用时,首先判断局部会话是否存在,如存在,即认为是登录状态,无需再到认证中心去判断。如不存在,就重定向到认证中心判断全局会话是否存在,如存在,通知该应用,该应用与客户端就建立起它们之间局部会话,下次请求该应用,就不去认证中心验证了。

2.2 退出

用户在一个系统退出了,访问其它子系统,也应该是退出状态。要想做到这一点,应用除结束本地局部会话外,还应该通知认证中心该用户退出。

认证中心接到退出通知,即可结束全局会话,用户访问其它应用时,都显示已登出状态。

需不需要立即通知所有已建立局部会话的子系统,将它们的局部会话销毁,可根据实际项目来。

相关推荐:

实例讲解SSO单点登录原理

单点登录原理和简单实现

php实现的SSO单点登录系统接入功能示例分析

The above is the detailed content of PHP single sign-on SSO implementation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template