我们知道,CAS SSO可以把username(uid)带入到客户端登录成功后的页面中去,可是一个uid在LDAP中还关联着许多其它有用的信息如:email,address。我们也想把这些信息同时带到客户端登录成功的画面中去.网上大多数是CAS3的一些方法,CAS4与CAS3相比在配置上有着很大的不同。我就把我 的整合在此分享下。
其实CAS验证成功到跳转客户端这个过程中,CAS SERVER 会返回登录的相关信息给客户端,客户端只要进行获取,就能知道登录的具体是哪个用户了。不过CAS 默认只返回用户名给客户端,那么怎么定义CAS SERVER返回的信息呢?
首先,先了解下几个相关的接口:
Credentials
Principal
IPersonAttributeDao
PrincipalResolver
Credentials (org.jasig.cas.authentication.Credentials)接口:这个接口是用来定义我们登录页上输入的认证信息的,比如用户名、密码、验证码等,可以理解为用户认证的相关凭据。
Principal (org.jasig.cas.authentication.principal.Principal) 接口:这个主要是用来保存用户认证后的用户信息,信息保存在一个Map中。
IPersonAttributeDao (org.jasig.services.persondir.IPersonAttributeDao) 接口,这个是用来定义我们需要返回给客户端相关信息的接口,CAS SERVER 默认有提供许多实现,比如
LdapPersonAttributeDao :通过查询 LDAP 目录 ,来返回信息
SingleRowJdbcPersonAttributeDao : 通过JDBC SQL查询,来返回信息等等,还有许多,大家可以参考源码中的实现,CAS SERVER 提供了各种功能的实现,有时候我们可以直接使用这个现成的就行了。
PrincipalResolver(org.jasig.cas.authentication.principal.PrincipalResolver) 接口,上面有说到 Credentials 是从登录页面上进行获取相关用户信息的。那么认证成功后,怎么把Credentials里面的信息转换到 Principal 中呢,这就是这个接口的作用了。由于认证本身是没有返回用户信息的,只是确定认证是通过还是没有通过。这时还要用到我们上面的IPersonAttributeDao 接口,在这接口中我们就可以定义我们需要返回的信息了。接口中有两个方法
resolve : 解析Credentials中的信息,返回 Principal 接口
supports : 判断Credentials 是否支持 Principal 协议。
ps: 在3.x版本中没有 PrincipalResolver接口,对应的是CredentialsToPrincipalResolver, PrincipalResolver这个是在4.0版本中加入的.
整合流程:
1、配置安全证书:生成证书->导出证书->导入证书到jdk 在cmd中敲以下3行代码:
keytool -genkey -alias tomcat -keyalg RSA -storepass changeit -keystore d:\keys\.keystore -validity 3600
keytool -export -trustcacerts -alias tomcat -file d:\keys\tomcat.cer -keystore d:\keys\.keystore -storepass changeit
keytool -import -trustcacerts -alias tomcat -file d:\keys\tomcat.cer -keystore "%JAVA_HOME%\jre\lib\security\cacerts" -storepass changeit
2、下载cas-server-4.0.0-release.zip
3、解压cas-server-4.0.0-release.zip,并把里面的modules文件夹中的cas-server-webapp-4.0.0.war改名成cas(仅仅是为了方便)拷出来放到tomcat中的webapp中
4、修改tomcat里面的8443端口内容,修改成如下:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="d:/keys/.keystore" <!--你的证书所放的位置-->
keystorePass="xxxx" /> <!--认证证书的密码-->
5、下载这些jar包,将这些jar包放入cas中的lib文件夹中
cas-server-support-ldap-4.0.0.jar
spring-ldap-core-2.0.2.RELEASE.jar
ldaptive-1.0.5.jar
6、CAS整合LDAP:
6.1 修改deployerConfigContext.xml文件
6.1.1首先需要的是修改认证入口:
<bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager"> <constructor-arg> <map> <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" /> <!--新增ldap认证的入口--> <entry key-ref="ldapAuthHandler" value-ref="primaryPrincipalResolver"/> </map> </constructor-arg> <span style="white-space:pre"> </span><property name="authenticationPolicy"> <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" /> </property> </bean>
<!--ldap认证的入口--> <bean id="ldapAuthHandler" class="org.jasig.cas.authentication.LdapAuthenticationHandler" p:principalIdAttribute="uid" c:authenticator-ref="authenticator"> <property name="principalAttributeMap"> <map> <!-- | This map provides a simple attribute resolution mechanism. | Keys are LDAP attribute names, values are CAS attribute names. | Use this facility instead of a PrincipalResolver if LDAP is | the only attribute source. --> <entry key="uid" value="uid" /> <!-- <entry key="ip_address" value="ip_address" /> <entry key="device_number" value="device_number" /> <entry key="expired_time" value="expired_time" /> --> <entry key="displayName" value="displayName" /> </map> </property> </bean> <bean id="authenticator" class="org.ldaptive.auth.Authenticator" c:resolver-ref="dnResolver" c:handler-ref="authHandler" /> <!-- | The following DN format works for many directories, but may need to be | customized. --> <bean id="dnResolver" class="org.ldaptive.auth.FormatDnResolver" c:format="uid=%s,ou=People,dc=sumscope,dc=com" /> <bean id="authHandler" class="org.ldaptive.auth.PooledBindAuthenticationHandler" p:connectionFactory-ref="pooledLdapConnectionFactory" /> <bean id="pooledLdapConnectionFactory" class="org.ldaptive.pool.PooledConnectionFactory" p:connectionPool-ref="connectionPool" /> <!--连接池--> <bean id="connectionPool" class="org.ldaptive.pool.BlockingConnectionPool" init-method="initialize" p:poolConfig-ref="ldapPoolConfig" p:blockWaitTime="3000" p:validator-ref="searchValidator" p:pruneStrategy-ref="pruneStrategy" p:connectionFactory-ref="connectionFactory" /> <bean id="ldapPoolConfig" class="org.ldaptive.pool.PoolConfig" p:minPoolSize="3" p:maxPoolSize="10" p:validateOnCheckOut="true" p:validatePeriodically="true" p:validatePeriod="300" /> <bean id="connectionFactory" class="org.ldaptive.DefaultConnectionFactory" p:connectionConfig-ref="connectionConfig" /> <bean id="connectionConfig" class="org.ldaptive.ConnectionConfig" p:ldapUrl="ldaps://iam.sumscope.com:637" p:connectTimeout="3000" p:useStartTLS="false" p:sslConfig-ref="sslConfig" /> <!--证书配置--> <bean id="sslConfig" class="org.ldaptive.ssl.SslConfig"> <property name="credentialConfig"> <bean class="org.ldaptive.ssl.X509CredentialConfig" p:trustCertificates="file:/D:/keys/iam.sumscope.com.crt" /> <!--证书地址--> </property> </bean> <bean id="pruneStrategy" class="org.ldaptive.pool.IdlePruneStrategy" p:prunePeriod="300" p:idleTime="600" /> <bean id="searchValidator" class="org.ldaptive.pool.SearchValidator" /> <!--ldap认证结束-->
<bean id="attributeRepository" class="org.jasig.cas.persondir.LdapPersonAttributeDao" p:connectionFactory-ref="pooledLdapConnectionFactory" p:baseDN="ou=People,dc=sumscope,dc=com" p:searchControls-ref="searchControls" p:searchFilter="uid={0}"> <property name="queryAttributeMapping"> <map> <entry key="username" value="uid" /> </map> </property> <property name="resultAttributeMapping"> <map> <entry key="uid" value="uid" /> <!-- <entry key="ip_address" value="ip_address" /> <entry key="device_number" value="device_number" /> <entry key="expired_time" value="expired_time" /> --> <entry key="displayName" value="displayName" /> </map> </property> </bean>
<util:list id="registeredServicesList"> <bean class="org.jasig.cas.services.RegexRegisteredService" p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP(S) and IMAP(S) protocols" p:serviceId="^(https?|imaps?)://.*" p:evaluationOrder="10000001" p:ignoreAttributes="true"/>
</util:list>
<%@ page session="false" contentType="application/xml; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> <cas:authenticationSuccess> <cas:user>${fn:escapeXml(assertion.primaryAuthentication.principal.id)}</cas:user> <!--添加属性Begin--> <c:if test="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes) > 0}"> <cas:attributes> <c:forEach var="attr" items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}"> <cas:${fn:escapeXml(attr.key)}>${fn:escapeXml(attr.value)}</cas:${fn:escapeXml(attr.key)}> </c:forEach> </cas:attributes> </c:if> <!--添加属性End--> <c:if test="${not empty pgtIou}"> <cas:proxyGrantingTicket>${pgtIou}</cas:proxyGrantingTicket> </c:if> <c:if test="${fn:length(assertion.chainedAuthentications) > 1}"> <cas:proxies> <c:forEach var="proxy" items="${assertion.chainedAuthentications}" varStatus="loopStatus" begin="0" end="${fn:length(assertion.chainedAuthentications)-2}" step="1"> <cas:proxy>${fn:escapeXml(proxy.principal.id)}</cas:proxy> </c:forEach> </cas:proxies> </c:if> </cas:authenticationSuccess> </cas:serviceResponse>
<% request.setCharacterEncoding("UTF-8"); AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); Map attributes = principal.getAttributes(); String uid=(String)attributes.get("uid"); String ip_address = (String)attributes.get("ip_address"); String device_number = (String)attributes.get("device_number"); String displayName=(String)attributes.get("displayName"); %> <div>返回值演示</div> <ul> <li>uid:<%= uid%></li> <li>ip_address:<%= ip_address%></li> <li>device_number:<%= device_number%></li> <li>displayName:<%= displayName%></li> </ul>