配置cas服務認證的驗證方式(sql資料庫或ldap)
一.配置sql認證
關于使用傳統的關系型資料庫做為驗證使用者和密碼的資料源,在上一篇博文:中有提到過,關于關系型資料庫的配置這裡也再簡單的重複下。具體的可以參考上一篇博文:周記(搭建cas驗證服務)
1.導入cas資料庫支援jar和資料庫驅動jar。複制D:\cas-server-3.5.2.1\modules下面cas-server-support-jdbc-3.5.2.1.jar到D:\apache-tomcat-7.0.52\webapps\cas\WEB-INF\lib下。資料庫這裡使用的是Mysql,是以把Mysql資料庫驅動包(mysql-connector-java-5.1.26-bin.jar)也複制到\cas\WEB-INF\lib檔案夾下。
2.配置資料連接配接資訊。打開D:\apache-tomcat-7.0.52\webapps\cas\WEB-INF\deployerConfigContext.xml檔案,在<beanid="authenticationManager"節點上面添加資料源資訊:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>supre2015</value>
</property>
</bean>
3.配置驗證規則。還是在上述的cas\WEB-INF\deployerConfigContext.xml檔案内,找到<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>,先注釋掉該句配置,在該句平行位置下添加如下配置
<bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
<property name="dataSource" ref="dataSource"></property>
<property name="sql" value="select password from tb_user where user_name=?"></property>
<!-- 密碼自定義加密配置
<property name="passwordEncoder" ref="myPasswordEncoder" ></property> -->
</bean>
說明:a. name="dataSourec" 中的ref為上面我們配置資料庫連接配接資訊(<bean id="dataSource")
b.name="sql" 為驗證查詢語句,後面value中的查詢語句,根據具體資料庫中的資料表和結構而定
c.name="passwordEncoder"項在後面對密碼加密進行中要使用,這裡未使用,先注釋掉
4.重新開機Tomcat,通路http://localhost:8080/cas,根據連接配接資料庫中的資料,進行測試。
二.配置ldap認證
ldap百科:LDAP是輕量目錄通路協定,英文全稱是Lightweight Directory Access Protocol,一般都簡稱為LDAP。它是基于X.500标準的,但是簡單多了并且可以根據需要定制。與X.500不同,LDAP支援TCP/IP,這對通路Internet是必須的。LDAP的核心規範在RFC中都有定義,所有與LDAP相關的RFC都可以在LDAPman RFC網頁中找到。
關于ldap大家可以自行去深入了解和學習,這裡主要是介紹下cas服務配置ladp來進行驗證的步驟。
1.導入cas服務認證的ldap支援jar和spring連接配接ldapjar(spring-ldap,cas-server-support-ldap
), 複制D:\cas-server-3.5.2.1\modules下面cas-server-support-ldap-3.5.2.1.jar到D:\apache-tomcat-7.0.52\webapps\cas\WEB-INF\lib下。這裡使用的spring-ldap是spring-ldap-1.3.1.RELEASE-all.jar版本,該jar可以去spring官網下載下傳,或通過maven添加,把該jar也複制到\cas\WEB-INF\lib檔案夾下。如果有需要jar的,可以去下載下傳博文中的demo,demo位址将在後面補上。
2.配置ldap的連接配接資訊。打開D:\apache-tomcat-7.0.52\webapps\cas\WEB-INF\deployerConfigContext.xml檔案,在<beanid="authenticationManager"節點上面添加資料源資訊:
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="pooled" value="false"/>
<property name="url" value="ldap://192.168.1.102:389" />
<property name="userDn" value="cn=admin"/>
<property name="password" value="centos"/><!-- 自己openldap的密碼 -->
<property name="baseEnvironmentProperties">
<map>
<entry key="com.sun.jndi.ldap.connect.timeout" value="3000" />
<entry key="com.sun.jndi.ldap.read.timeout" value="3000" />
<entry key="java.naming.security.authentication" value="simple" />
</map>
</property>
</bean>
說明:url為該ldap服務的位址
userDn為該ldap服務的管理者使用者名
password為對應管理者的密碼
這三項為主要配置資訊,下面map中是對連接配接參數的一些配置
3.配置驗證規則。還是在上述的cas\WEB-INF\deployerConfigContext.xml檔案内,找到<bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler"/>,先注釋掉該句配置,在該句平行位置下添加如下配置
<bean class="org.jasig.cas.adaptors.ldap.FastBindLdapAuthenticationHandler">
<property name="filter" value="uid=%u,ou=People,dc=supresoft,dc=com" />
<property name="contextSource" ref="contextSource" />
</bean>
說明:其中contextSource中的ref為上面配置的連結資訊bean的id
filter中配置驗證的ldap查詢語句
4.重新開機Tomcat,通路http://localhost:8080/cas,根據連接配接資料庫中的資料,進行測試。
demo位址:後期補上或參考同系列博文