本文簡單實作在spring架構下對apache shiro與jasig-cas 整合 實作單點登入多點登出統一權限管理平台,功能簡單,主要大緻講講入門配置。
一、環境:
1、背景架構:spring3.2+struts2+hibernate4+apache-shiro1.2.1+jasig-cas3.4.1(具體相關依賴包請自行上官網下載下傳);
2、資料庫:mysql5.x;
3、servlet容器:tomcat6;
二、首先CAS伺服器搭建
1、官網下載下傳cassever包,解壓後将modules檔案夾裡的 cas-server-webapp-*.war 複制到 tomcat webapps檔案夾下;
2、啟動tomcat,輸入相同的使用者名和密碼登入,将提示登入成功,這個cas預設的認證方式,重新整理頁面将回到登入頁面;
3、修改cas預設的認證方式: 做web開發,我們一般用資料庫使用者密碼方式認證:
a、修改web-inf下的deployerConfigContext.xml,注釋掉預設的驗證:
<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
b、添加擷取認證資訊的資料源:
<bean id="casdataSource" name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value><![CDATA[jdbc:mysql://localhost:3306/cas?useUnicode=true&characterEncoding=UTF-8]]></value>
</property>
<property name="username" >
<value>root</value>
</property>
<property name="password" >
<value>root</value>
</property>
</bean>
c、在注釋掉的預設驗證後增加bean,以下表示從該資料源的t_user表中擷取認證資訊,需要使用加密的自行google
<bean id="QueryDatabaseAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="casdataSource"></property>
<property name="sql" value="select USER_PWD from T_USER where USER_NAME=?"></property>
</bean>
4、cas協定,cas預設使用https協定,如果對安全要求不高,可使用http協定,我這裡使用http協定
a、修改deployerConfigContext.xml,
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient" p:requireSecure="false"/><!-- require 屬性為true 開啟ssl -->
b、修改spring-configuration下的ticketGrantingTicketCookieGenerator.xml
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
p:cookieSecure="false"
p:cookieMaxAge="-1"
p:cookieName="CASTGC"
p:cookiePath="/cas" />
<!-- 使用http協定: cookieSecure 為false -->
</beans>
以上,完成使用jdb資料源http協定的簡單cas server端配置,可以啟動tomcat 輸入位址檢視,這時候認證将需要與資料庫裡的使用者資訊比對還能認證成功!
三、用戶端client1配置
client1 相關其他配置就不詳講了,主要講述spring與shiro與cas的相關配置
1、首先引入用戶端所需jar包:spring相關jar包,shiro jar包,cas client jar包(下載下傳cas client modules 下會有cas-client-core-.jar)
2、web.xml 相關配置
加載相關配置檔案與監聽
<context-param>
<description>相關配置檔案加載</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring*.xml</param-value>
</context-param>
<!-- 相關監聽加載 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>com.areo.kongtan.listener.SessionListenerHandler</listener-class>
</listener>
shiro filter配置,shiro在1.2後開始支援的cas
<filter>
<description>
shrio權限過濾,DelegatingFilterProxy 作用是自動到 spring 容器查找名字為 shiroFilter(filter-name)的 bean并把所有 Filter 的操作委托給它
</description>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
以上完成client端的web配置 下一篇講client端spring整合相關配置