cas介绍及与spring-security整合
一、单点登录CAS介绍
1.1 什么是单点登录CAS
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。
1.2 单点登录的应用场景
在系统的开发过程中,如果个系统包含多个子系统,而这些子系统又共用一个认证数据库(可以理解为多个子系统登录的用户信息都保存在一个数据库)。这样就会涉及到一个场景:用户登录过任何一个子系统后,再访问其他的子系统时,不再需要重复登录。这就涉及到单点登录的问题。
例如:有三个子系统A、B、C。登录时用的是同一套数据库,三个系统间可以互相跳转。目标是:三个系统中其中一个登录了,其他两个就无需登录验证。
二、单点登录服务器的完整配置
2.1原生的CAS服务器登录界面效果
原生的CAS服务器,其实就是一个war包。要运行CAS服务器,直接将war包部署到tomcat服务器中即可运行查看效果。原生的cas.war部署后,访问的界面效果如下图:登录默认的CAS服务器的用户名和密码是: casuser/Mellon
2.2 修改CAS在开发中常用配置(修改商品和https校验忽略)
2.2.1 修改访问端口
- 修改tomcat的端口号
打开tomcat主目录中,conf/server.xml文件
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
- 修改CAS配置文件端口号
修改cas.war主目录中 WEB-INF/cas.properties
下面的端口号设置为与 tomcat修改的端口一致。
server.name=http://localhost:9100
2.2.2 去除https的认证
CAS 默认使用的是 HTTPS 协议,如果使用 HTTPS 协议需要 SSL 安全证书(需向特定的机构申请和购买) 。如果对安全要求不高或是在开发测试阶段,可使用 HTTP 协议。我们这里讲解通过修改配置,让 CAS 使用 HTTP 协议。
-
修改cas的WEB-INF/deployerConfigContext.xml
这里需要增加参数 p:requireSecure=“false”,requireSecure 属性意思为是否需要安全验证,即HTTPS,false 为不采用<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpClient-ref="httpClient"/>
-
修改cas的WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
参数 p:cookieSecure=“true”,同理为 HTTPS 验证相关,TRUE 为采用 HTTPS 验证,FALSE 为不采用 https 验证。<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator" p:cookieSecure="true" p:cookieMaxAge="-1" p:cookieName="CASTGC" p:cookiePath="/cas" />
参数 p:cookieMaxAge="-1",是 COOKIE 的最大生命周期,-1 为无生命周期,即只在当前打开的窗口有效,关闭或重新打开其它窗口,仍会要求验证。可以根据需要修改为大于 0 的数字,比如 3600 等,意思是在 3600 秒内,打开任意窗口,都不需要验证。
这里将 cookieSecure 改为 false , cookieMaxAge 改为 3600 -
修改cas的WEB-INF/spring-configuration/warnCookieGenerator.xml
这里将 cookieSecure 改为 false , cookieMaxAge 改为 3600<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator" p:cookieSecure="true" p:cookieMaxAge="-1" p:cookieName="CASPRIVACY" p:cookiePath="/cas" />
2.2.3 设置“退出登录”时自动跳转的界面
- 修改 cas 系统的配置文件 cas-servlet.xml
<bean id="logoutAction" class="org.jasig.cas.web.flow.LogoutAction"
p:servicesManager-ref="servicesManager"
p:followServiceRedirects="${cas.logout.followServiceRedirects:false}"/>
将cas.logout.followServiceRedirects的值修改为“true"
2. 在系统中,退出系统的按钮中,发起请求时,超链接进行如下修改
<a href="http://localhost:9100/cas/logout?service=希望跳转的界面或是URL请求">退出登录</a>
2.2.4 修改CAS连接系统数据库动态校验密码
-
修改 cas 服务端中 web-inf 下 deployerConfigContext.xml中最后添加下面3项配置
<!--p:jdbcUrl:设置连接数据库的URL地址;p:user:登录数据库的用户名;p:password:登录数据库的密码 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://127.0.0.1:3306/pinyougoudb?characterEncoding=utf8" p:user="root" p:password="123456" />
<!-- 注意c:encodingAlgorithm:参数指定当前验证密码时的加密方式 --> <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" c:encodingAlgorithm="MD5" p:characterEncoding="UTF-8" />
<!--p:sql:指定连接数据库后,根据用户名查询密码的 sql语句 --> <bean id="dbAuthHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" p:dataSource-ref="dataSource" p:sql="select password from tb_user where username = ?" p:passwordEncoder-ref="passwordEncoder"/>
-
将配置的dbAuthHandler注入到认证管理器authenticationManager中
在当前配置文件中,查找如下配置信息的位置
<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">
将这个bean下的 property属性修改为第1步中配置的dbAuthHandler,具体操作如下
将
<entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />
修改为<entry key-ref="dbAuthHandler" value-ref="primaryPrincipalResolver"/>
-
将下面cas所依赖的3个jar包放置到cas.war目录中的lib文件夹中
c3p0-0.9.1.2.jar cas-server-support-jdbc-4.0.0.jar mysql-connector-java-5.1.32.jar
3.4 修改CAS默认的登录界面为与集成的工程配套的
- cas.war默认的登录界面在目录 WEB-INF\view\jsp\default\ui\casLoginView.jsp。实现对CAS登录界面的更换,只需将集成工程中的login界面替换casLoginView.jsp界面即可。具体操作步骤如下
-
将集成工程中的登录界面整体复制到casLoginView.jsp中
-
把下面的指令添加到更换后的casLoginView.jsp界面
<%@ page pageEncoding="UTF-8" %> <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
-
修改form表单的标签
<form:form method="post" id="fm1" commandName="${commandName}" htmlEscape="true" class="sui-form">
-
修改登录用户名和登录密码的input框
<form:input id="username" tabindex="1" accesskey="${userNameAccessKey}" path="username" autocomplete="off" htmlEscape="true" placeholder="邮箱/用户名/手机号" class="span2 input-xfat" />
<form:password id="password" tabindex="2" path="password" accesskey="${passwordAccessKey}" htmlEscape="true" autocomplete="off" placeholder="请输入密码" class="span2 input-xfat" />
-
修改登录按钮
<input type="hidden" name="lt" value="${loginTicket}" /> <input type="hidden" name="execution" value="${flowExecutionKey}" /> <input type="hidden" name="_eventId" value="submit" /> <input class="sui-btn btn-block btn-xlarge btn-danger" accesskey="l" value="登陆" type="submit" />
二、单点登录案例中web.xml配置介绍
2.1配置单点登出的信息
<!-- 配置 单点登出 -->
<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
<listener>
<listener-class>
org.jasig.cas.client.session.SingleSignOutHttpSessionListener
</listener-class>
</listener>
<!-- 该过滤器用于实现单点登出功能,可选配置。 -->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>
org.jasig.cas.client.session.SingleSignOutFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 访问“登出“的URL,固定是 /logout。
- 我们这里访问登出的URL:http://localhst:9100/cas/logout
2.2 配置单点登录的信息
<filter>
<filter-name>CASFilter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<!-- 配置如果当前工程未登录,则会跳转到 CAS的登录界面 -->
<param-name>casServerLoginUrl</param-name>
<param-value>http://localhost:9100/cas/login</param-value>
<!--这里的server是服务端的IP -->
</init-param>
<init-param>
<!-- 统一配置,访问CAS服务器根路径 -->
<param-name>serverName</param-name>
<param-value>http://localhost:9001</param-value>
</init-param>
</filter>
<filter-mapping>
<!-- 配置 过滤器CASFilter 将会拦截哪些 URL请求-->
<filter-name>CASFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.3 配置票据管理的信息
<!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>http://localhost:9100/cas</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://localhost:9001</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、springsecurity与CAS整合案例
重点要将Spring-security.xml配置文件理解
3.1 配置总的配置信息
<http use-expressions="false" entry-point-ref="casProcessingFilterEntryPoint">
<intercept-url pattern="/**" access="ROLE_USER"/>
<csrf disabled="true"/>
<!-- filter 都是 spring-security框架内置的 -->
<!-- custom-filter为过滤器, position 表示将过滤器放在指定的位置上,before表示放在指定位置之前 ,after表示放在指定的位置之后 -->
<!-- 登录的配置-->
<custom-filter ref="casAuthenticationFilter" position="CAS_FILTER" />
<!--登出的配置-->
<custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/>
<custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>
</http>
3.2 认证过滤器的开始部分
<beans:bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager"/>
</beans:bean>
<!-- 认证管理器 -->
<authentication-manager alias="authenticationManager">
<authentication-provider ref="casAuthenticationProvider">
</authentication-provider>
</authentication-manager>
<!-- 认证提供者 -->
<beans:bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
<beans:property name="authenticationUserDetailsService">
<beans:bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<!-- 配置加载Spring-security框架的自定义认证类-->
<beans:constructor-arg ref="userDetailsService" />
</beans:bean>
</beans:property>
<beans:property name="serviceProperties" ref="serviceProperties"/>
<!-- ticketValidator 为票据验证器。指定了获取票据信息的CAS的访问根URL -->
<beans:property name="ticketValidator">
<beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<beans:constructor-arg index="0" value="http://localhost:9100/cas"/>
</beans:bean>
</beans:property>
<beans:property name="key" value="an_id_for_this_auth_provider_only"/>
</beans:bean>
<!-- 认证类:配置spring-security中认证操作的自定义实现类。作用:加载当前登录用户的角色列表 -->
<beans:bean id="userDetailsService" class="cn.itcast.demo.service.UserDetailServiceImpl"/>
<!-- 配置整合spring-security和cas工程后的 本地登录访问的URL-->
<beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
<!--service 配置自身工程的根地址+/login/cas 如果登出,则会跳转到当前所配置的URL路径中 -->
<beans:property name="service" value="http://localhost:9003/login/cas"/>
</beans:bean>
3.3 配置登出的信息
- requestSingleLogoutFilter
<!-- 经过此配置,当用户在地址栏输入本地工程 /logout/cas -->
<beans:bean id="requestSingleLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<!--配置cas退出成功,将会跳转的界面-->
<beans:constructor-arg value="http://localhost:9100/cas/logout?service=http://localhost:9003/login.html"/>
<beans:constructor-arg>
<beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
</beans:constructor-arg>
<beans:property name="filterProcessesUrl" value="/logout/cas"/>
</beans:bean>
- 配置singleLogoutFilter登出过滤器的配置
<beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>