spring mvc配置cas客戶端
1:在WEB-INF/lib中匯入cas jar包
2、匯入證書
(1)將證書server.cer檔案拷貝至業務系統所在伺服器上的某路徑下(如c盤根目錄),在cmd命令介面切換至證書所在路徑,執行以下命令(jdk路徑自行修改),並信任該認證(輸入Y,回車):
keytool -import -trustcacerts -alias casserver -file server.cer -keystore "C:\Program Files\Java\jdk1.6.0_21\jre\lib\security\cacerts" -storepass changeit |
(2)如果安裝了jre,務必再執行一遍以下命令:
keytool -import -trustcacerts -alias casserver -file server.cer -keystore "C:\Program Files\Java\jre6\lib\security\cacerts" -storepass changeit
匯入證書介面如下圖:
3:配置web.xml
<!-- CAS退出url --> <context-param> <param-name>casServerLogoutUrl</param-name> <param-value>http://casserver.sdcloud.net:8090/cas/logout</param-value> </context-param> <!--單點退出配置,一定要放在其他filter之前--> <listener> <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class> </listener> <filter> <filter-name>CAS Single Sign Out Filter</filter-name> <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class> </filter> <filter-mapping> <filter-name>CAS Single Sign Out Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- CAS 客戶端配置 這個filter負責對請求進行登入驗證攔截,--> <filter> <filter-name>CAS Authentication Filter</filter-name> <filter-class> org.jasig.cas.client.authentication.AuthenticationFilter </filter-class> <!-- CAS驗證伺服器地址,有域名填寫域名 --> <init-param> <param-name>casServerLoginUrl</param-name> <param-value> https://casserver.sdcloud.net:8443/cas/login </param-value> </init-param> <init-param> <param-name>renew</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>gateway</param-name> <param-value>false</param-value> </init-param> <!-- 客戶端應用伺服器地址,utl_pattern用來控制需要過濾的url--> <!-- 這裡將對http://localhost:8080/xxx/logon/xx.jsp的所有路徑進行過濾--> <init-param> <param-name>serverName</param-name> <param-value>http://localhost:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Authentication Filter</filter-name> <url-pattern>/logon/*</url-pattern> </filter-mapping> <!--負責Ticket校驗 這個filter負責對請求引數ticket進行驗證(ticket引數是負責子系統與CAS進行驗證互動的憑證)--> <filter> <filter-name>CAS Validation Filter</filter-name> <filter-class> org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter </filter-class> <init-param> <param-name>casServerUrlPrefix</param-name> <param-value>https://casserver.sdcloud.net:8443/cas/</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://localhost:8080</param-value> </init-param> <init-param> <param-name>useSession</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>redirectAfterValidation</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Validation Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 這個是HttpServletRequet的包裹類,讓他支援getUserPrincipal,getRemoteUser方法來取得使用者資訊 --> <filter> <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> <filter-class> org.jasig.cas.client.util.HttpServletRequestWrapperFilter </filter-class> </filter> <filter-mapping> <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 這個類把Assertion資訊放在ThreadLocal變數中,這樣應用程式不在web層也能夠獲取到當前登入資訊 --> <filter> <filter-name>CAS Assertion Thread Local Filter</filter-name> <filter-class> org.jasig.cas.client.util.AssertionThreadLocalFilter </filter-class> </filter> <filter-mapping> <filter-name>CAS Assertion Thread Local Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4:修改源程式
假設原業務系統中(參看test測試應用),系統登入頁面為test/login.jsp,使用者驗證頁面為test/checkUser.jsp,驗證成功後的頁面為test/index.jsp,退出後的頁面為test/logout.jsp。
原業務流程為:使用者在login.jsp中輸入使用者名稱、密碼,通過checkUser.jsp進行相關業務邏輯的驗證,然後登入到index.jsp,退出系統到logout.jsp;如果未登入訪問index.jsp,則自動轉到login.jsp。
使用CAS單點登入伺服器,需要做以下修改(參看castest測試應用)。
(1) 修改原來的使用者登入驗證頁面checkUser.jsp
//獲取在CAS登入視窗輸入的使用者名稱密碼
AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();
String username = principal.getName();
String password = principal.getProxyTicketFor(username);
(2) 修改index.jsp頁面中,原來判斷如果未登入的轉向頁面,原來是轉向登入頁面,修改為使用者登入驗證頁面。(參考castest/index.jsp)或者改相應的配置檔案,web.xml.
(3) 修改退出處理頁面logout.jsp。在使session失效後,增加CAS退出的語句。
response.sendRedirect("http://casserver.sdcloud.net:8090/cas/logout?service=http://localhost:8080/test/logoutseccess.jsp"); 標黃色為退出後要轉到的頁面,可自行設定。
5:釋出系統