在Cas Server的WEB-INF目錄下有一個deployerConfigContext.xml檔案,該檔案是基于Spring的配置檔案,裡面存放的内容常常是部署人員需要修改的内容。其中認證方式也是定義在這個檔案中的,id為authenticationManager的bean的authenticationHandlers即定義了需要使用的AuthenticationHandler清單。預設使用了兩個AuthenticationHandler,第一個是用來確定目前使用的是https協定的HttpBasedServiceCredentialsAuthenticationHandler,第二個是我們需要改的,其簡單認證使用者名與密碼相等的SimpleTestUsernamePasswordAuthenticationHandler。我們隻需要更改這裡的SimpleTestUsernamePasswordAuthenticationHandler就行了。Cas中已經為我們提供了不少AuthenticationHandler的實作,包括基于資料庫認證的實作,當然使用者也可以實作自己的AuthenticationHandler。下面将以使用資料庫進行認證為例講解如何更改認證方式。
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檔案,添加如下依賴。
<artifactId>cas-server-support-jdbc</artifactId>
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>
替換後:
<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>
像dataSource的定義及其需要使用到的依賴包我就不貼了,比較常用。
打包以後生成的war包中使用的AuthenticationHandler就會是我們在myCasServer的src/main/webapp/WEB-INF目錄下的deployerConfigContext.xml檔案中定義的QueryDatabaseAuthenticationHandler了。以後需要修改Cas Server中的其它内容也可以依照此種方式進行修改。
(注:本文是基于cas 3.5.2所寫)