Example of PHP single sign-on implementation solution
Abstract:
This article mainly introduces the analysis and design of a universal single sign-on system using webservice, session, and cookie technologies. The specific implementation language is PHP. Single sign-on, also known as Single Sign On in English, or SSO for short, is an important part of the comprehensive processing of users in current enterprises and network businesses. The definition of SSO is that in multiple application systems, users only need to log in once to access all mutually trusted application systems.
Motivation:
Friends who have used ucenter’s full-site login method should know that this is a typical observer pattern solution. The user center is a subject, and the registration and deletion of its observers are unified in the backend of ucenter. Each sub-application site corresponds to an observer. Every login action in the user center will trigger a js script to call back the w3c standard subsite login interface (api/uc.php).
The shortcomings of this method, I think, are mainly two points: 1. When there are too many sub-sites, the callback interface will increase accordingly. This is limited by the number of distributed sub-sites. How to control it so that the login efficiency will not be affected? Too low and difficult to grasp; 2. When a problem occurs with the callback interface of a certain substation, the default login process will be stuck (the execution time of the login program can be limited, but if there is a corresponding problem, the callback interface of the substation behind the substation will be called No more.
Based on the above issues, during the actual development process, I designed another single sign-on system
1. Login principle explanation
Technical implementation mechanism of single sign-on: When the user accesses application system 1 for the first time, because he has not logged in yet, he will be directed to the authentication system to log in; based on the login information provided by the user, the authentication system performs identity verification , if it passes the validation, an authentication credential - ticket should be returned to the user; when the user accesses other applications, he will bring this ticket as his own authentication credential. After the application system receives the request, it will send the ticket Go to the authentication system for verification and check the validity of the ticket. If it passes the verification, the user can access application system 2 and application system 3 without logging in again.
It can be seen that to implement SSO, The following main functions are required:
a) All application systems share an identity authentication system;
b) All application systems can identify and extract ticket information;
c) Application The system can identify users who have logged in, and can automatically determine whether the current user has logged in, thereby completing the single sign-on function
Based on the above basic principles, I designed a set of single sign-on system programs in PHP language , has now been put into official production server operation. This system program uses the ticket information with the unique session id of the entire system as a medium to obtain the current online user's entire site information (login status information and other user-wide site information that needs to be processed).
2. Process description:
Login process:
1. Log in to a site for the first time:
a) The user enters the username + password and sends a login request to the user verification center
b) When currently logging into the site, through a webservice request, the user verification center verifies the legitimacy of the username and password. If the verification is passed, a ticket is generated to identify the user of the current session, and the site identifier of the currently logged in sub-site is recorded in the user center. Finally,
c) return the obtained user data and ticket to the sub-site. stand. If the verification fails, the corresponding error status code is returned.
d) According to the result returned by the webservice request in the previous step, the current sub-site logs in the user: if the status code indicates success, the current site saves the ticket through the cookie of this site, and the site records the user's Login status. If the status code indicates failure, the user will be given a corresponding login failure prompt.
2. In the logged-in state, the user goes to another page:
a) Verify the user's login status through the site's cookie or session: If the verification is passed, enter the normal site processing program; Otherwise, the user center verifies the user's login status (sends a ticket to the user verification center). If the verification is passed, local login processing is performed on the returned user information. Otherwise, it indicates that the user is not logged in.
Logout process
a) The current logout site clears the user’s login status of the site and the locally saved user’s unique random ID
b) Through the webservice interface, clear the unique random ID recorded in the entire site. The webservice interface will return, log out the javascript code of other logged-in sub-sites, and this site will output this code.
c) js code accesses the W3C standard logout script of the corresponding site
3. Code description:
The relevant code involved in this article has been Package and upload. If you are interested, you can click to download at the download link at the end of this article.
1. Login process:
Starting from opening the browser, the first subsite logged in must call the UClientSSO::loginSSO() method. This method returns a random ID that is unique to the entire site and is used to identify the user. This random ID has been saved through the cookie of this website in UClientSSO::loginSSO(), that is, the sub-site retains the stub of the user's login ID on this website.
a) UClientSSO::loginSSO() method is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
2. After successful login to this site, localized user login processing is performed, and subsequent verification of whether the user is logged in is only performed locally. (To access information about logged-in user status locally, please set it to exit after closing the browser)
3. When detecting user login status, please call the local verification process first. If the local verification fails, call again The UClientSSO::checkUserLogin() method goes to the user center to detect the user's login status.
a) UClientSSO::checkUserLogin() method is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
4. When single-point logout, call UClientSSO::logoutSSO( )method. After the call is successful, if you want other logged-in sites to log out immediately, please call the UClientSSO::getSynloginScript() method to obtain the W3C standard script and output it on the page.
a) UClientSSO::logoutSSO() method is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
4. Code deployment:
1. User Authentication Center Settings
a) The webservice service interface file provided by the User Authentication Center to the sub-site, namely UserSvc.php, is deployed in hostname/webapps/port/ UserSvc.php. To view wsdl content, please visit http://www.php.cn/ UserSvc.php?wsdl
b) The user center user single-point service class file is UCenterSSO.class.php, and the file path is in hostname/ webapps/include/UCenterSSO.class.php. This file is the server class for user single sign-in processing and is called by hostname/webapps/port/UserSvc.php. Used to obtain the user's login information, status information about whether to log in single-point, single-logout processing, etc.
c) The User Authentication Center passes W3C standards and uses cookies to record and delete the unique random ID of users across the site. The script file is hostname/webapps/port/cookie_mgr.php.
2 Sub -site settings
A) For sub -sites, please, uclientso.class.php is deployed in the service client directory of the user center. After deployment, please modify the last line of UClientSSO::setSite('1'); The parameter value is the identification id uniformly assigned to each site by the user verification center.
b) Serve the client in the deployed user center In the api directory under the package, please transfer the logout_sso.php script here and write a processing script for logging out of this site.
c) In the code section for verifying user login status on the subsite, additional single sign-on verification processing in the user center is added.
That is, first verify the user's login status through this site. If the verification fails, go to the user center for verification. The verification operation requires calling the UClientSSO::checkUserLogin(); interface. Please see the code comments for the meaning of the interface.
d) In the logout processing script of the branch station, use UClientSSO::getSynlogoutScript(); to obtain the script string output.
5. Extended functions:
1. Record and track all online users
Because all user logins must go through the user verification center, all users The tickets are generated in the verification center, and a mapping table can be established between the user and the ticket (session id) in the memory table. Get a record list of all online users.
If it is necessary to track the user status in the future to implement other functions, just track this mapping table. Other functions can be: obtaining a list of online users, determining the user's online status, obtaining the number of online users, etc.
2. Special statistical processing
Because the entire system login and logout must go through the user verification center, special statistics of users can be processed. Such as the number of user logins per day, login time, login status expiration time, the trend of the number of online users in each period, etc.
6. Other matters:
1.
#1. The state is lost when the browser is closed. Each branch station is required to handle sessions or cookies as follows:
a) Sites that record user login status in Session mode
1 2 3 4 5 6 |
|
b) Sites that use cookies to record user login status
Please set the cookie validity time to null when setting the cookie for user login status.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
