Cas(04)——更改認證方式

 

       Cas ServerWEB-INF目錄下有一個deployerConfigContext.xml文件,該文件是基於Spring的配置文件,裏面存放的內容常常是部署人員需要修改的內容。其中認證方式也是定義在這個文件中的,idauthenticationManagerbeanauthenticationHandlers即定義了需要使用的AuthenticationHandler列表。默認使用了兩個AuthenticationHandler,第一個是用來確保當前使用的是https協議的HttpBasedServiceCredentialsAuthenticationHandler,第二個是我們需要改的,其簡單認證用戶名與密碼相等的SimpleTestUsernamePasswordAuthenticationHandler。我們只需要更改這裏的SimpleTestUsernamePasswordAuthenticationHandler就行了。Cas中已經爲我們提供了不少AuthenticationHandler的實現,包括基於數據庫認證的實現,當然用戶也可以實現自己的AuthenticationHandler。下面將以使用數據庫進行認證爲例講解如何更改認證方式。

       Cas的各個模塊都是基於Maven開發的,Cas Server也不例外。所以官方推薦我們使用MavenWar覆蓋機制來修改Cas Server的配置文件。MavenWar覆蓋機制是指當一個package類型爲warMaven項目A中引入了一個package類型爲war的項目B作爲依賴時,最終項目A打包的war包中不僅會包含項目A的內容,還會包含項目B的內容,且相同位置的文件項目A中的會覆蓋項目B中的,即當項目A和項目BWEB-INF下都擁有一個web.xml文件時,最終生成的war包中將使用項目AWEB-INF下的web.xml文件;而當項目AWEB-INF下沒有web.xml文件,而項目BWEB-INF下擁有web.xml文件時最終生成的war包中將使用項目BWEB-INF下的web.xml文件。所以如果我們要修改Cas Server的配置文件,我們可以建立一個自己的基於MavenWeb項目,然後引入Cas Server作爲依賴,並在自己的項目中建立對應的deployerConfigContext.xml文件。下面來看詳細步驟。

 

       1、建立基於MavenWeb項目,取名爲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、使用數據庫進行認證的話還需要添加對應的依賴,打開myCasServerpom.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文件copymyCasServersrc/main/webapp/WEB-INF目錄下。

       6、基於數據庫的AuthenticationHandler有多種,這裏以QueryDatabaseAuthenticationHandler爲例。QueryDatabaseAuthenticationHandler需要配置兩個參數,dataSourcesqldataSource就是數據源,表示從哪個數據源進行查詢。sql是對應的查詢語句,其會接收username作爲參數,然後查詢出對應的密碼,之後QueryDatabaseAuthenticationHandler會將查詢出來的密碼與用戶提交的密碼進行匹配。所以這裏我們打開復制到myCasServer中的deployerConfigContext.xml文件,找到idauthenticationManagerbeanauthenticationHandlers屬性定義,將最後一個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就會是我們在myCasServersrc/main/webapp/WEB-INF目錄下的deployerConfigContext.xml文件中定義的QueryDatabaseAuthenticationHandler了。以後需要修改Cas Server中的其它內容也可以依照此種方式進行修改。

 

(注:本文是基於cas 3.5.2所寫)

(注:原創文章,轉載請註明出處。原文地址:http://haohaoxuexi.iteye.com/blog/2128869

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章