使用工具
maven-3.3.9
cas-4.1.4
Tomcat-7.0.57-win-x64
cas-sample-java-webapp
一、Hello cas
1、下載Tomcat,解壓;修改其server.xml,增加對SSL支持(具體百度):
1 <Connector SSLEnabled="true" acceptCount="100" clientAuth="false" 2 disableUploadTimeout="true" enableLookups="false" maxThreads="25" 3 port="8443" keystoreFile="G:/work/xxx/tomcat.keystore" keystorePass="YOUR PASSWORD HERE" 4 protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https" 5 secure="true" sslProtocol="TLS" />
1、從https://github.com/Jasig/cas/releases 下載cas-server對應的版本;
2、解壓,使用maven命令mvn package對cas進行編譯打包,這里可能會出現2個問題:
a.單元測試不通過==》嘗試加上-Dmaven.test.skip=true跳過;
b.編譯到一半編譯不過==》進入cas-server-core,執行命令mvn install -Dmaven.test.skip=true先將core編譯為jar包后,再進行。
3、將打包好的cas-server-webapp下的war包復制到tomcat下,運行tomcat先試試吧;
4、使用賬號casuser,密碼Mellon登錄,成功
5、從https://github.com/Jasig/java-cas-client 下載java-cas-client,如上步驟解壓編譯,編譯后不再操作,待用。
6、從https://github.com/UniconLabs/cas-sample-java-webapp 下載cas-sample-java-webapp,如上步驟解壓編譯,編譯后不再操作,待用。
二、server配置數據庫連接認證
上面我們采用的是默認配置,登錄賬號實際上是在cas/WEB-INF/deployerConfigContext.xml配置寫死的。當然不符合我們大多數的實際使用場景。現在我們將其改為連接Mysql數據庫驗證。
1、回到我們剛剛maven編譯的cas目錄下,將cas-server-support-jdbc/target/cas-server-support-jdbc-4.1.4.jar 復制到tomcat/webapp/cas/WEB-INF/lib下,再復制mysql-connector-java-5.0.8-bin.jar到tomcat/webapp/cas/WEB-INF/lib下。當然這些你可以在cas-server-webapp下的pom.xml進行依賴添加再進行打包編譯。
2、修改cas/WEB-INF/deployerConfigContext.xml,這里需要注意修改以下地方:
1 <bean id="primaryAuthenticationHandler" 2 class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler"> 3 <property name="users"> 4 <map> 5 <entry key="casuser" value="Mellon"/> 6 </map> 7 </property> 8 </bean>
注釋以上代碼,增加如下代碼
1 <bean id="dataSource" 2 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 3 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 4 <property name="url" 5 value="jdbc:mysql://10.3.64.87:3306/cas?characterEncoding=utf-8&autoReconnect=true" /> 6 <property name="username" value="root" /> 7 <property name="password" value="123456" /> 8 </bean> 9 10 <bean id="primaryAuthenticationHandler" 11 class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" 12 p:dataSource-ref="dataSource" 13 p:passwordEncoder-ref="MD5PasswordEncoder" 14 p:sql="select password from user_info where user_name=?" /> 15 16 <bean id="MD5PasswordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder"> 17 <constructor-arg index="0"> 18 <value>MD5</value> 19 </constructor-arg> 20 </bean>
這里我們增加了MySQL的數據連接dataSource,並編寫了SQL查詢用戶認證信息。
修改完成,我們來測試一下能否通過MySQL進行認證。啟動tomcat。
登錄成功,我們已經成功和MYSQL連接成功了。
三、添加客戶端
這里我們將用到第一步里面待用的cas-sample-java-webapp。
1、將編譯好的cas-sample-java-webapp/target/cas-sample-java-webapp.war部署到tomcat下;
2、找到tomcat/webapp/cas-sample-java-webapp/WEB-INF/web.xml,刪除以下代碼
1 <init-param> 2 <param-name>redirectAfterValidation</param-name> 3 <param-value>true</param-value> 4 </init-param> 5 <init-param> 6 <param-name>useSession</param-name> 7 <param-value>true</param-value> 8 </init-param> 9 <init-param> 10 <param-name>acceptAnyProxy</param-name> 11 <param-value>true</param-value> 12 </init-param> 13 <init-param> 14 <param-name>proxyReceptorUrl</param-name> 15 <param-value>/client/proxyUrl</param-value> 16 </init-param> 17 <init-param> 18 <param-name>proxyCallbackUrl</param-name> 19 <param-value>https://zhuajindian.com:8443/client/proxyUrl</param-value> 20 </init-param>
將其中的多個casServerUrlPrefix、serverName、casServerLoginUrl替換成你自己的URL。
3、修改deployerConfigContext.xml,找到下面的代碼
1 <bean id="serviceRegistryDao" class="org.jasig.cas.services.JsonServiceRegistryDao" 2 c:configDirectory="${service.registry.config.location:classpath:services}" />
替換為
1 <!-- 注冊服務 --> 2 <bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl" 3 p:registeredServices-ref="registeredServicesList" /> 4 5 <util:list id="registeredServicesList"> 6 <bean class="org.jasig.cas.services.RegexRegisteredService" 7 p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP(S) and IMAP(S) protocols" 8 p:serviceId="^(https?|http?|imaps?)://.*" p:evaluationOrder="10000001" /> 9 </util:list>
這里的serviceId就是你告訴cas服務器端,通過這個正則來判斷,符合這個正則表達式的,就是我們自己的客戶端。當然這里泛匹配了,你可以根據實際情況來。
4、重啟tomcat,訪問剛剛填寫在serverName處的URL試試
可以看到,我們已經能夠成功獲取到Authenticated UserId,也就是登錄的用戶名。這樣,我們的簡單demo就完成了。
四、自定義登錄后的可傳遞字段,方便客戶端讀取
在我們的應用場景中,客戶端需要的參數不僅僅是用戶名。還需要諸如userid等各類信息,那么,接下來我們就來配置獲取自定義字段。
1、找到cas/WEB-INF/deployerConfigContext.xml,注釋以下代碼:
1 <bean id="attributeRepository" class="org.jasig.services.persondir.support.NamedStubPersonAttributeDao" 2 p:backingMap-ref="attrRepoBackingMap" /> 3 4 <util:map id="attrRepoBackingMap"> 5 <entry key="uid" value="uid" /> 6 <entry key="eduPersonAffiliation" value="eduPersonAffiliation" /> 7 <entry key="groupMembership" value="groupMembership" /> 8 <entry> 9 <key><value>memberOf</value></key> 10 <list> 11 <value>faculty</value> 12 <value>staff</value> 13 <value>org</value> 14 </list> 15 </entry> 16 </util:map>
替換為:
1 <bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"> 2 <constructor-arg index="0" ref="dataSource" /> 3 <constructor-arg index="1" value="SELECT id,user_name,mobile,cid FROM user_info WHERE {0}" /> 4 <property name="queryAttributeMapping"> 5 <map> 6 <entry key="username" value="user_name" /> 7 </map> 8 </property> 9 <property name="resultAttributeMapping"> 10 <map> 11 <entry key="id" value="userId" /> 12 <entry key="user_name" value="username" /> 13 <entry key="mobile" value="mobile" /> 14 <entry key="cid" value="cid" /> 15 </map> 16 </property> 17 </bean>
其中的sql只需要寫前半部分,如示例,entry的key代表上面sql查詢的字段,value代表服務端傳給客戶端的參數名,客戶端可以通過value取出對應的值。
2、修改cas/WEB-INF/view/jsp/protocol/2.0/casServiceValidationSuccess.jsp,增加下面這段
1 <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> 2 <cas:authenticationSuccess> 3 <cas:user>${fn:escapeXml(principal.id)}</cas:user> 4 <!-- 這段 --> 5 <c:if test="${fn:length(assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes) > 0}"> 6 <cas:attributes> 7 <c:forEach var="attr" items="${assertion.chainedAuthentications[fn:length(assertion.chainedAuthentications)-1].principal.attributes}"> 8 <cas:${fn:escapeXml(attr.key)}>${fn:escapeXml(attr.value)}</cas:${fn:escapeXml(attr.key)}> 9 </c:forEach> 10 </cas:attributes> 11 </c:if> 12 <!-- 這段 end--> 13 <c:if test="${not empty pgtIou}"> 14 <cas:proxyGrantingTicket>${pgtIou}</cas:proxyGrantingTicket> 15 </c:if> 16 <c:if test="${fn:length(chainedAuthentications) > 0}"> 17 <cas:proxies> 18 <c:forEach var="proxy" items="${chainedAuthentications}" varStatus="loopStatus" begin="0" end="${fn:length(chainedAuthentications)}" step="1"> 19 <cas:proxy>${fn:escapeXml(proxy.principal.id)}</cas:proxy> 20 </c:forEach> 21 </cas:proxies> 22 </c:if> 23 </cas:authenticationSuccess> 24 </cas:serviceResponse>
3、重啟tomcat,登錄看效果:
上面配置的4個參數這里顯示了3個,原來mobile字段是因為沒有值,所以他默認就不傳遞了。