Cas的各個模塊都是基於Maven開發的,Cas Server也不例外。所以官方推薦我們使用Maven的War覆蓋機制來修改Cas Server的配置文件。Maven的War覆蓋機制是指當一個package類型爲war的Maven項目A中引入了一個package類型爲war的項目B作爲依賴時,最終項目A打包的war包中不僅會包含項目A的內容,還會包含項目B的內容,且相同位置的文件項目A中的會覆蓋項目B中的,即當項目A和項目B在WEB-INF下都擁有一個web.xml文件時,最終生成的war包中將使用項目A在WEB-INF下的web.xml文件;而當項目A在WEB-INF下沒有web.xml文件,而項目B在WEB-INF下擁有web.xml文件時最終生成的war包中將使用項目B在WEB-INF下的web.xml文件。所以如果我們要修改Cas Server的配置文件,我們可以建立一個自己的基於Maven的Web項目,然後引入Cas Server作爲依賴,並在自己的項目中建立對應的deployerConfigContext.xml文件。下面來看詳細步驟。
1、建立基於Maven的Web項目,取名爲myCasServer。
2、打開pom.xml文件,刪除默認的依賴項,添加如下內容。
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>cas</warName>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<version>${cas.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
<cas.version>3.5.2</cas.version>
</properties>
<repositories>
<repository>
<id>ja-sig</id>
<url>http://oss.sonatype.org/content/repositories/releases/ </url>
</repository>
</repositories>
3、刪除myCasServer項目中src/main/webapp下的index.jsp文件和src/main/webapp/WEB-INF下的web.xml文件,因爲在cas-server-webapp中都存在對應的文件,不刪除的話打包後的結果將是myCasServer中的覆蓋cas-server-webapp中的。如果這個時候使用Maven進行打包的話你將得到一個和cas-server-webapp一模一樣的war包。
4、使用數據庫進行認證的話還需要添加對應的依賴,打開myCasServer的pom.xml文件,添加如下依賴。
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
<scope>runtime</scope>
</dependency>
5、將cas-server-webapp/WEB-INF下的deployerConfigContext.xml文件copy到myCasServer的src/main/webapp/WEB-INF目錄下。
6、基於數據庫的AuthenticationHandler有多種,這裏以QueryDatabaseAuthenticationHandler爲例。QueryDatabaseAuthenticationHandler需要配置兩個參數,dataSource和sql。dataSource就是數據源,表示從哪個數據源進行查詢。sql是對應的查詢語句,其會接收username作爲參數,然後查詢出對應的密碼,之後QueryDatabaseAuthenticationHandler會將查詢出來的密碼與用戶提交的密碼進行匹配。所以這裏我們打開復制到myCasServer中的deployerConfigContext.xml文件,找到id爲authenticationManager的bean的authenticationHandlers屬性定義,將最後一個AuthenticationHandler替換成我們想要的QueryDatabaseAuthenticationHandler。
替換前:
<property name="authenticationHandlers">
<list>
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" />
<bean
class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
</list>
</property>
替換後:
<property name="authenticationHandlers">
<list>
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" />
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="select password from t_user where username = ?"/>
</bean>
</list>
</property>
像dataSource的定義及其需要使用到的依賴包我就不貼了,比較常用。
打包以後生成的war包中使用的AuthenticationHandler就會是我們在myCasServer的src/main/webapp/WEB-INF目錄下的deployerConfigContext.xml文件中定義的QueryDatabaseAuthenticationHandler了。以後需要修改Cas Server中的其它內容也可以依照此種方式進行修改。
(注:本文是基於cas 3.5.2所寫,原文地址:http://haohaoxuexi.iteye.com/blog/2128869)