在Cas Server的WEB-INF目錄下有1個(gè)deployerConfigContext.xml文件,該文件是基于Spring的配置文件,里面寄存的內(nèi)容常常是部署人員需要修改的內(nèi)容。其中認(rèn)證方式也是定義在這個(gè)文件中的,id為authenticationManager的bean的authenticationHandlers即定義了需要使用的AuthenticationHandler列表。默許使用了兩個(gè)AuthenticationHandler,第1個(gè)是用來確保當(dāng)前使用的是https協(xié)議的HttpBasedServiceCredentialsAuthenticationHandler,第2個(gè)是我們需要改的,其簡(jiǎn)單認(rèn)證用戶名與密碼相等的SimpleTestUsernamePasswordAuthenticationHandler。我們只需要更改這里的SimpleTestUsernamePasswordAuthenticationHandler就好了。Cas中已為我們提供了很多AuthenticationHandler的實(shí)現(xiàn),包括基于數(shù)據(jù)庫認(rèn)證的實(shí)現(xiàn),固然用戶也能夠?qū)崿F(xiàn)自己的AuthenticationHandler。下面將以使用數(shù)據(jù)庫進(jìn)行認(rèn)證為例講授如何更改認(rèn)證方式。
Cas的各個(gè)模塊都是基于Maven開發(fā)的,Cas Server也不例外。所以官方推薦我們使用Maven的War覆蓋機(jī)制來修改Cas Server的配置文件。Maven的War覆蓋機(jī)制是指當(dāng)1個(gè)package類型為war的Maven項(xiàng)目A中引入了1個(gè)package類型為war的項(xiàng)目B作為依賴時(shí),終究項(xiàng)目A打包的war包中不但會(huì)包括項(xiàng)目A的內(nèi)容,還會(huì)包括項(xiàng)目B的內(nèi)容,且相同位置的文件項(xiàng)目A中的會(huì)覆蓋項(xiàng)目B中的,即當(dāng)項(xiàng)目A和項(xiàng)目B在WEB-INF下都具有1個(gè)web.xml文件時(shí),終究生成的war包中將使用項(xiàng)目A在WEB-INF下的web.xml文件;而當(dāng)項(xiàng)目A在WEB-INF下沒有web.xml文件,而項(xiàng)目B在WEB-INF下具有web.xml文件時(shí)終究生成的war包中將使用項(xiàng)目B在WEB-INF下的web.xml文件。所以如果我們要修改Cas Server的配置文件,我們可以建立1個(gè)自己的基于Maven的Web項(xiàng)目,然后引入Cas Server作為依賴,并在自己的項(xiàng)目中建立對(duì)應(yīng)的deployerConfigContext.xml文件。下面來看詳細(xì)步驟。
1、建立基于Maven的Web項(xiàng)目,取名為myCasServer。
2、打開pom.xml文件,刪除默許的依賴項(xiàng),添加以下內(nèi)容。
<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項(xiàng)目中src/main/webapp下的index.jsp文件和src/main/webapp/WEB-INF下的web.xml文件,由于在cas-server-webapp中都存在對(duì)應(yīng)的文件,不刪除的話打包后的結(jié)果將是myCasServer中的覆蓋cas-server-webapp中的。如果這個(gè)時(shí)候使用Maven進(jìn)行打包的話你將得到1個(gè)和cas-server-webapp1模1樣的war包。
4、使用數(shù)據(jù)庫進(jìn)行認(rèn)證的話還需要添加對(duì)應(yīng)的依賴,打開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、基于數(shù)據(jù)庫的AuthenticationHandler有多種,這里以QueryDatabaseAuthenticationHandler為例。QueryDatabaseAuthenticationHandler需要配置兩個(gè)參數(shù),dataSource和sql。dataSource就是數(shù)據(jù)源,表示從哪一個(gè)數(shù)據(jù)源進(jìn)行查詢。sql是對(duì)應(yīng)的查詢語句,其會(huì)接收username作為參數(shù),然后查詢出對(duì)應(yīng)的密碼,以后QueryDatabaseAuthenticationHandler會(huì)將查詢出來的密碼與用戶提交的密碼進(jìn)行匹配。所以這里我們打開復(fù)制到myCasServer中的deployerConfigContext.xml文件,找到id為authenticationManager的bean的authenticationHandlers屬性定義,將最后1個(gè)AuthenticationHandler替換成我們想要的QueryDatabaseAuthenticationHandler。
替換前:
<property name="authenticationHandlers">
<list>
<beanclass="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>
<beanclass="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" />
<beanclass="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的定義及其需要使用到的依賴包我就不貼了,比較經(jīng)常使用。
打包以后生成的war包中使用的AuthenticationHandler就會(huì)是我們?cè)趍yCasServer的src/main/webapp/WEB-INF目錄下的deployerConfigContext.xml文件中定義的QueryDatabaseAuthenticationHandler了。以后需要修改Cas Server中的其它內(nèi)容也能夠依照此種方式進(jìn)行修改。
(注:本文是基于cas 3.5.2所寫)
(注:原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明出處。原文地址:http://haohaoxuexi.iteye.com/blog/2128869)