一、简介
单点登录(Single Sign On ,简称SSO)是目前比较流行的服务于企业业务整合的解决方案之一,SSO 使得在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
SSO的实现机制不尽相同,大体分为Cookie机制和Session机制两大类。
(A):Session是一种服务器端机制,当客户端访问服务器时,服务器为客户端创建一个惟一的SessionID,以使在整个交互过程中始终保持状态,而交互的信息则可由应用自行指定,因此用Session方式实现SSO,不能在多个浏览器之间实现单点登录,但却可以跨域。
(B):Cookie是一种客户端机制,它存储的内容主要包括:名称、值、过期时间、路径和域,路径与域合在一起就构成了Cookie的作用范围,因此用Cookie方式可实现SSO,但域名必须相同。
目前大部分SSO产品采用的是Cookie机制,最好的开源单点登录产品CAS也是采用Cookie机制。
二、CAS 协议
2.1 基础协议执行步骤如下:
访问服务:用户发送请求访问应用系统(可称为CAS客户端)提供的受保护的服务资源。
重定向认证:CAS客户端分析HTTP请求中没有Service Ticket(即ST)或SessionID,说明用户还没有进行身份认证。于是,重定向用户请求到CAS服务器进行身份认证,并把用户此次访问CAS客户端的URL作为参数(service)传递给CAS服务器。
用户认证:CAS服务器接收到身份认证请求后转向登录页面,用户提供认证信息后进行身份认证。身份认证成功后,CAS服务器以SSL方式给浏览器返回一个TGC(用户身份信息凭证,用于以后获取ST)。
发放票据:CAS服务器会产生一个随机的ST,然后重定向到CAS客户端。
验证票据:CAS客户端收到ST后,向CAS服务器验证票据ST的合法性,验证通过后,允许用户访问客户端服务。
-
传输用户信息:CAS服务器验证票据ST通过后,传输用户认证结果信息给客户端。
2.2 CAS基础协议流程图
TIM截图20171030134208.png
三、使用步骤
第一步 部署CAS系统服务端
1、从官网http://www.jasig.org下载CAS Server将cas-server-webapp-3.4.12.war解压, 可以看到是一个标准的java的web应用, 可以直接部署到tomcat的webapps目录下的,我这里假设部署的路径是{tomcat}/webapps/cas。
2、CAS默认需要tomcat配置SSL协议,使用https协议通信的。 由于项目是一般企业系统,不需要高的安全级别, 可以简化操作,不走SSL协议。修改下配置文件\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml,如下, 将默认的true改成false即可。
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
3、配置登录的验证逻辑, 修改配置文件
cas\WEB-INF\deployerConfigContext.xml。在authenticationHandlers中配置验证方式,我这里配置数据库查询语句来实现用户名和密码的验证。
<property name="authenticationHandlers">
<list>
<!--
| This is the authentication handler that authenticates services by means of callback via SSL, thereby validating
| a server side SSL certificate.
+-->
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" p:requireSecure="false" />
<!--
| This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS
| into production. The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials
| where the username equals the password. You will need to replace this with an AuthenticationHandler that implements your
| local authentication strategy. You might accomplish this by coding a new such handler and declaring
| edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.
+-->
<!--
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />-->
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource" />
<property name="passwordEncoder" ref="MD5PasswordEncoder" />
<property name="sql">
<value>select pwd from user_tab where user_account = ?</value>
</property>
</bean>
<!--<bean class="org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler" p:filter="uid=%u" p:searchBase="dc=ebeitech,dc=com" p:contextSource-ref="contextSource" />-->
</list>
</property>
这边的三个bean只可以放开其中一个,我这边放开的bean是采用数据库验证。
注释掉第一个bean是用户名和密码输入一样即可登陆(默认使用该方式)
注释掉第三个bean放开的话ldap验证。
密码加密方法我这里使用MD5, 配置passwordEncoder的bean
<bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
<constructor-arg index="0" value="MD5" />
</bean>
在配置一个名称为dataSource的数据源
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/zt</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
数据源的配置根据自己的实际情况来配置, 需要的jar如果lib下面没有,自己复制进去, 不然数据源连不上报错。
第一步完成后启动cas服务
第二步 客户端web应用程序集成CAS
1、配置web.xml文件,主要是添加过滤器拦截通信
<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置-->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>autoLoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>autoLoginFilter</filter-name>
<filter-class>com.ebei.common.servlet.AutoLoginFilter</filter-class>
</filter>
2、添加applicationContext-security-cas.xml文件,在web.xml中加入该文件(只需要更改下面红色框地址即可使用)
3、过滤器AutoLoginFilter的实现是需要在具体的应用中实现的,他的目的是, CAS服务端登录验证成功后,会将登录用户的用户名携带回来, 这时客户端web应用程序需要根据用户名从数据库用户表中查询到用户的Id等信息, 并填充到Session中。
下面是AutoLoginFilter的一个简单实现。
/**
* 该过滤器用户从CAS认证服务器中获取登录用户用户名,并填充必要的
*/
public class AutoLoginFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
//如果session中没有用户信息,则填充用户信息
if (session.getAttribute("j_userId") == null) {
//从Cas服务器获取登录账户的用户名
Assertion assertion = AssertionHolder.getAssertion();
String userName = assertion.getPrincipal().getName();
try {
//根据单点登录的账户的用户名,从数据库用户表查找用户信息, 填充到session中
User user = UserDao.getUserByName(userName);
session.setAttribute("username", userName);
session.setAttribute("userId", user.getId());
} catch (Exception e) {
e.printStackTrace(); } }
chain.doFilter(request, response);
}
当你访问具体应用的网址, 如http://具体应用IP: 8080/ ,就会跳转到CAS服务器的登陆页面。http://CAS服务器IP: 8080/ 进行登录验证, 验证通过后, 又会跳转回应用的网址。
`