這個CAS( Central Authentication Service )是耶魯大學的開源項目,旨在實現企業應用單點登錄,還是很不錯的,牛b的大學確實不一樣,不是我們這些2b學校式的教育。其官網http://www.jasig.org/cas,很不幸,應該翻了墻才能進去,自己去搗鼓翻墻吧,建議就GoA
這個CAS(Central Authentication Service)是耶魯大學的開源項目,旨在實現企業應用單點登錄,還是很不錯的,牛b的大學確實不一樣,不是我們這些2b學校式的教育。其官網http://www.jasig.org/cas,很不幸,應該翻了墻才能進去,自己去搗鼓翻墻吧,建議就GoAgent足已。
1 |
1.jdk7_45 |
2 |
2.tomcat7_45 |
3 |
3.三臺虛擬機: |
4 |
1.tomcat1(部署cas驗證服務器) |
5 |
2.tomcat2(其中部署了兩個web應用cas-web-client,
cas-web-client2) |
6 |
3.mysql
server一臺(直接到數據庫種驗證) |
http://downloads.jasig.org/
解壓之,jar會再modules目錄下,其他都為源代碼項目
已經打包的server:
client:
------------------------------------------------------
開始搭建:
1.在tomcat1所在機器生成證書:
這時會在用戶主目錄下生成.keystore文件,這個文件也可在上述命令種指定,其中生成過程會填入一些信息,注意輸入第一個時(名字與姓氏)就輸入你本機器的域名(不能時IP), 這里我的是www.tomcat1.com:
2.導出證書(后面將其導入客戶端的jre環境中):
于是在用戶主目錄下就有了ssotest.crt證書文件(保留著,待會客戶端配置要用),
3.配置Tomcat SSL: ${TOMCAT_HOME}/conf/server.xml中83-93行修改為:
01 |
|
02 |
This
connector uses the JSSE configuration, when using APR, the |
03 |
connector
should be using the OpenSSL style configuration |
04 |
described
in the APR documentation --> |
05 |
|
06 |
< Connector port = "8443" protocol = "HTTP/1.1" SSLEnabled = "true" |
07 |
maxThreads = "150" scheme = "https" secure = "true" |
08 |
clientAuth = "false" sslProtocol = "TLS" |
09 |
keystoreFile = "/home/tomcatadmin/.keystore" |
10 |
keystorePass = "ssotest" /> |
這時輸入用戶名及密碼(保持一樣就可以,默認CAS驗證方式只要用戶名密碼一樣就行), 可見其默認驗證實現類SimpleTestUsernamePasswordAuthenticationHandler:
01 |
public final class SimpleTestUsernamePasswordAuthenticationHandler extends |
02 |
AbstractUsernamePasswordAuthenticationHandler
{ |
03 |
04 |
public boolean authenticateUsernamePasswordInternal( final UsernamePasswordCredentials
credentials) { |
05 |
final String
username = credentials.getUsername(); |
06 |
final String
password = credentials.getPassword(); |
07 |
08 |
if (StringUtils.hasText(username)
&& StringUtils.hasText(password) |
09 |
&&
username.equals(getPasswordEncoder().encode(password))) { //用戶名及密碼相同就成功 |
10 |
return true ; |
11 |
} |
12 |
return false ; |
13 |
} |
14 |
} |
這樣CAS服務端就默認配置完畢了,待會兒我們還要配置數據庫驗證。
1.生成.keystore(同上);
2.將CAS服務端的導出ssotest.crt證書拷貝到某目錄,并且導入到客戶端機器的jre運行環境中:
1 |
keytool
- import -keystore
$JAVA_HOME/jre/lib/security/cacerts - file ~/ssotest.crt
- alias ssotest |
2 |
#
NOTE: 有可能會有異常:java.io.IOException: Keystore was tampered with, or password was incorrect. 那就先刪除本機上述的cacerts文件。 |
3.配置客戶端Tomcat SSL(同上);
4.新建WEB項目cas-web-client, 并加入依賴包:cas-client-core-3.2.1.jar,commons-logging-1.1.jar
5.配置web.xml:
01 |
xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5" > |
03 |
< display-name >cas-web-client2 display-name > |
04 |
< listener > |
05 |
< listener-class > |
06 |
org.jasig.cas.client.session.SingleSignOutHttpSessionListener |
07 |
listener-class > |
08 |
listener > |
09 |
< filter > |
10 |
< filter-name >CasSingleSignOutFilter filter-name > |
11 |
< filter-class >org.jasig.cas.client.session.SingleSignOutFilter filter-class > |
12 |
filter > |
13 |
< filter-mapping > |
14 |
< filter-name >CasSingleSignOutFilter filter-name > |
15 |
< url-pattern >/* url-pattern > |
16 |
filter-mapping > |
17 |
< filter > |
18 |
< filter-name >CASFilter filter-name > |
19 |
< filter-class >org.jasig.cas.client.authentication.AuthenticationFilter filter-class > |
20 |
< init-param > |
21 |
< param-name >casServerLoginUrl param-name >
|
22 |
< param-value >https://www.tomcat1.com:8443/cas/login param-value > |
23 |
init-param > |
24 |
< init-param >
|
25 |
< param-name >serverName param-name > |
26 |
< param-value >http://www.tomcat2.com:8080 param-value > |
27 |
init-param > |
28 |
filter > |
29 |
< filter-mapping > |
30 |
< filter-name >CASFilter filter-name > |
31 |
< url-pattern >/* url-pattern > |
32 |
filter-mapping > |
33 |
< filter > |
34 |
< filter-name >CasTicketFilter filter-name > |
35 |
< filter-class > |
36 |
org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter filter-class > |
37 |
< init-param > |
38 |
< param-name >casServerUrlPrefix param-name > |
39 |
< param-value >https://www.tomcat1.com:8443/cas param-value > |
40 |
init-param > |
41 |
< init-param > |
42 |
< param-name >serverName param-name > |
43 |
< param-value >http://www.tomcat2.com:8080 param-value > |
44 |
init-param > |
45 |
filter > |
46 |
< filter-mapping > |
47 |
< filter-name >CasTicketFilter filter-name > |
48 |
< url-pattern >/* url-pattern > |
49 |
filter-mapping > |
50 |
< filter > |
51 |
< filter-name >CasRequestWrapFilter filter-name > |
52 |
< filter-class > |
53 |
org.jasig.cas.client.util.HttpServletRequestWrapperFilter filter-class > |
54 |
filter > |
55 |
< filter-mapping > |
56 |
< filter-name >CasRequestWrapFilter filter-name > |
57 |
< url-pattern >/* url-pattern > |
58 |
filter-mapping > |
59 |
< filter > |
60 |
< filter-name >AssertionThreadLocalFilter filter-name > |
61 |
< filter-class >org.jasig.cas.client.util.AssertionThreadLocalFilter filter-class > |
62 |
filter > |
63 |
< filter-mapping > |
64 |
< filter-name >AssertionThreadLocalFilter filter-name > |
65 |
< url-pattern >/* url-pattern > |
66 |
filter-mapping > |
67 |
< welcome-file-list > |
68 |
< welcome-file >index.jsp welcome-file > |
69 |
welcome-file-list > |
70 |
web-app > |
這樣cas-web-client就完成了,拷貝一份為另一個web客戶端cas-web-client2:
啟動Cas Server所在tomcat(Tomcat1),再啟動web客戶端所在tomcat(Tomcat2),此時我們訪問兩個web客戶端:
http://www.tomcat2.com:8080/cas-web-client/index.jsp
http://www.tomcat2.com:8080/cas-web-client2/index.jsp
都會被重定想到CAS Server登錄界面,登錄成功其中之一(將看到對應的index.jsp),再刷新另一個(也能看到對應的index.jsp了,因為已經登錄過了), 這就算服務端和客戶端都配置OK了。
------------------------------------------------------
1.新建WEB項目cas-auth-server,你需要將cas-server-webapp-3.5.2.war中的資源對應拷貝到項目中,再把下載包中cas-server-webapp的源碼考到項目中,由于基于數據庫驗證,還得將cas-server-support-jdbc-3.5.2.jar和mysql-connector驅動包拷貝到WEB-INF/lib目錄下,這樣就構成了純凈的cas server項目,當然你也可以通過maven去構建,官網也有說明,如圖:
2.我們要做的是修改一些配置,就是WEB-INF/deployerConfigContext.xml文件,先配置mysql數據源:
1 |
|
2 |
< bean id = "mysqlDataSource" class = "org.apache.commons.dbcp.BasicDataSource" > |
3 |
< property name = "driverClassName" value = "com.mysql.jdbc.Driver" /> |
4 |
< property name = "url" value = "jdbc:mysql://192.168.141.129:3306/blog?useUnicode=true&characterEncoding=utf-8" /> |
5 |
< property name = "username" value = "mysqladmin" /> |
6 |
< property name = "password" value = "mysqladmin" /> |
7 |
bean > |
01 |
< property name = "authenticationHandlers" > |
02 |
< list > |
03 |
< bean class = "org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpClient-ref = "httpClient" /> |
04 |
|
05 |
class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"
/>--> |
06 |
|
07 |
< bean class = "org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler" > |
08 |
< property name = "dataSource" ref = "mysqlDataSource" /> |
09 |
< property name = "sql" |
10 |
value = "select
password from t_user where username = ?" /> |
11 |
< property name = "passwordEncoder" ref = "myPasswordEncoder" > property > |
12 |
bean > |
13 |
list > |
14 |
property > |
1 |
|
2 |
< bean id = "myPasswordEncoder" class = "org.jasig.cas.custom.encoder.MyPasswordEncoder" /> |
這樣就配置完成了,將項目導出war包,并部署到tomcat1所在機器上運行起來,效果會之前一樣。
收工。
http://my.oschina.net/indestiny/blog/200768
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com