search
HomeBackend DevelopmentPHP Tutorial如何实现SSO单点登陆?

如何实现SSO单点登陆?

Jun 06, 2016 pm 08:14 PM
sso

可以使用跨域Cookie实现单点登录。步骤:1、未登录用户访问子站a.com进行登录,自动跳转到统一登录页;2、用户在统一登录页进行登录,成功后显示登录跳转页,然后自动跳转回a.com,单点登录完成;4、用户在访问b.com时无需再次登录。

如何实现SSO单点登陆?

单点登录

单点登录(SSO - Single Sign On):对于同一个客户端(例如 Chrome 浏览器),只要登录了一个子站(例如 a.com),则所有子站(b.com、c.com)都认为已经登录。

比如用户在登录淘宝后,跳转到天猫时就已经登录了。

如何实现SSO单点登陆?

方法:使用跨域Cookie实现单点登录

用例步骤

● 未登录用户访问子站 a.com 进行登录,自动跳转到账户中心的统一登录页 account.com/login

● 用户在统一登录页进行登录,登录成功后显示登录跳转页

● 显示登录跳转页后自动跳转回 a.com,单点登录完成

● 用户在访问 b.com 时无需再次登录

实现原理

登录

● 统一登录页登录请求完成后响应为登录跳转页

● 登录跳转页中通知各子站进行登录

<script src=&#39;b.com/login?uid=xxxx&token=xxxxx&#39;></script>
<script src=&#39;c.com/login?uid=xxxx&token=xxxxx&#39;></script>

● 子站收到登录请求后验证 token 是否有效,有效的话在响应中设置 cookie(user_token=xxxx)

token 验证

● 账户中心使用私钥加密 user id,生成 token

● 子站使用公钥解密 token,将得到的 user id 和参数 uid 对比,如果一样就是校验通过

登出

● 用户在某个子站主动登出时跳转到账户中心统一登出页 account.com/logout?uid=xxxx&token=xxxx

● 账户中心验证 token 后进行登出,在登出跳转页中通知各子站进行登出(设置 cookie),类似登录通知

● 子站收到登出请求后验证 token 是否有效,有效的话在响应中设置 cookie(删除 user_token)

关键点

● 浏览器渲染登录跳转页时将执行上面用 3f1c4e4b6b16bbbd69b2ee476dc4f83a 发送的登录通知请求,执行完后(或者超时)才跳转回前面登录的子站

● 登录通知请求是跨域的(当前域是账户中心 account.com),所以在响应中设置 cookie 时 IE 某些版本需要设置 P3P 头

● 在验证 token 时可以考虑使用账户中心提供高性能的验证接口,子站进行调用

更多相关知识,请访问 PHP中文网!!

Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool