Single Sign-on, called Sso,sso, is not only used in enterprise-level development, but is also a big line in the Internet. Just to give a few examples, such as when we login Sina Weibo, and then visit Sina home page, we found that has been automatically logged in, such as we log in csdn, can write blog, visit the forum, download resources and so on. The former is a fully cross-domain single sign-on, which is described below, which is the single sign-on of the common parent domain (www.csdn.net, blog.csdn.net, Bbs.csdn.net, passport.csdn.net), which is the main content of this article.
Single Sign-on is actually the "identity authentication" of the integration, when we have multiple applications, we want to log in one of the applications, and then access other applications, will automatically log in, to avoid repetitive manual labor. Single Sign-on implementation principle is relatively simple, as shown, when the user first access to the application system through the browser 1 o'clock, because not yet logged in, will be directed to the authentication system to log in. The following start the single sign-on process: The authentication system according to the user entered in the browser login information, authentication, if the authentication passed, return to the browser a Proof ticket (ticket); When the user accesses other application system, it will take ticket; The application system receives ticket, It will be sent to the authentication system for legitimacy check, after the verification, the user does not need to enter the user name password again to log in, thus realizing the single sign-on function.
The process described above is actually web-sso. To achieve SSO, the first must have a unified authentication system, and then each application system through the authentication system to verify the user, so this requires two aspects of cooperation. Web-sso is a good implementation, especially in the case of a common parent domain, we can save ticket through a browser cookie. Today I use servlet technology to realize the main functions of SSO, where I can download the project.
Domain Preparation
Modify the Hosts file to map 3 domain names:
[HTML]View Plaincopy
- 127.0.0.1 web1.ghsau.com
- 127.0.0.1 web2.ghsau.com
- 127.0.0.1 passport.ghsau.com
3 domain names must have a common parent domain (. ghsau.com), Web1 and WEB2 are used to access the application system, and passport is used to access the authentication system.
Project deployment
The project contains two Eclipse project, which you may need to set up under the Java EE Class library after importing to Eclipse/myeclipse. Webssoauth for the authentication system, Webssodemo for the application system, if the mapped domain name and I set the same, do not need to set up, direct deployment can be. If not, you need to modify the Web. xml file for the next two items. The key configuration information is as follows:
Webssoauth/web-inf/web.xml:
[HTML]View Plaincopy
- <servlet>
- <servlet-name>ssoauth</servlet-name>
- <servlet-class>com.ghsau.servlet.ssoauth</servlet-class>
- <init-param>
- <!--ticket name --
- <param-name>cookiename</param-name>
- <param-value>ssoid</param-value>
- </init-param>
- <init-param>
- <!--ticket scope --
- <param-name>domainname</param-name>
- <param-value>.ghsau.com</param-value>
- </init-param>
- <init-param>
- <param-name>secure</param-name>
- <param-value>false</param-value>
- </init-param>
- <init-param>
- <!--ticket content encryption key, must be 24 characters, Chinese count 2 characters- -
- <param-name>secretkey</param-name>
- <param-value>111111112222222233333333</param-value>
- </init-param>
- <init-param>
- <!--the expiration date of the ticket in the server, in minutes and
- <param-name>tickettimeout</param-name>
- <param-value>10080</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>ssoauth</servlet-name>
- <url-pattern>/ssoauth</url-pattern>
- </servlet-mapping>
Webssodemo/web-inf/web.xml:
[HTML]View Plaincopy
- <filter>
- <filter-name>ssoauth</filter-name>
- <filter-class>com.ghsau.filter.ssoauth</filter-class>
- <init-param>
- <!--certification System Services --
- <param-name>ssoservice</param-name>
- <param-value>http://passport.ghsau.com:8080/webssoauth/ssoauth</param-value >
- </init-param>
- <init-param>
- <!--authentication System login Page --
- <param-name>ssologin</param-name>
- <param-value>http://passport.ghsau.com:8080/webssoauth/login.jsp</param-value >
- </init-param>
- <init-param>
- <!--certification System ticket name --
- <param-name>cookiename</param-name>
- <param-value>ssoid</param-value>
- </init-param>
- </Filter>
- <filter-mapping>
- <filter-name>ssoauth</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>ssoauth</filter-name>
- <url-pattern>/logout</url-pattern>
- </filter-mapping>
If the domain name or port number is inconsistent with my, the corresponding configuration item can be modified. Finally, deploy to the application server and start the server.
SSO uses
First enter the access address of the first application system, http://web1.ghsau.com:8080/WebSSODemo/index.jsp, if it is the first visit, will automatically jump to the login page, such as:
System built in 3 users, Zhang San, John Doe, Harry, the user name and password are all pinyin spelling, input Zhangsan/zhangsan login, will automatically jump to the page we just visited, the page shows the login user name and welcome information, such as:
At this point, we enter the second application system access address, http://web2.ghsau.com:8080/WebSSODemo/index.jsp, we found that no second login, the same page shows the login user name and welcome information, such as:
We then click Logout Logout User, the page jumps to the login page, then we go back to the first application of the page, found also jump to the login page. This gives the user the effect is that an application login, other applications will be automatically logged in, and an application is written off, other applications will automatically logout, good magic look.
Project provides the source code, the implementation of the idea is the above diagram, ticket saved in the cookie, using the characteristics of the cookie domain, to achieve the ticket in different applications can be obtained, Ticket verification process is the use of httpclient to send the authentication request, ticket encryption using 3DES, specifically can see Desutils.java, OK, here it is, if there is any problem, welcome to discuss.