天天看點

cas-ad支援ldap的配置方法

将CAS-Ad.war進行解包,增加兩個jar檔案到web-inf/lib目錄下,

1.   cas-server-support-ldap-3.4.2.1.jar

2.   spring-ldap-core-1.3.2.RELEASE.jar

注:因為cas-server中采用了Spring的架構,是以需要加入spring-ldap的jar檔案,另外cas-server-support-ldap在cas-server/modules下面

修改cas-ad/ WEB-INF/ deployerConfigContext.xml

1.   找到authenticationManager

2.   找到authenticationHandlers

3.   将<bean

class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> 注釋掉

4.   在注釋的配置下面增加

<bean class="org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler"

                   p:filter="sAMAccountName=%u"

                   p:searchBase="ou=根路徑,dc=域名,dc=com"

                   p:contextSource-ref="contextSource"

                   p:ignorePartialResultException="true" />

   請根據應用的環境替換ou和dc的資訊

5.   在authenticationManager下面增加新的bean

<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">

  <!-- DO NOT enable JNDI pooling for context sources that perform LDAP bind operations. -->

  <property name="pooled" value="false"/>

  <!--

    Although multiple URLs may defined, it's strongly recommended to avoid this configuration

    since the implementation attempts hosts in sequence and requires a connection timeout

    prior to attempting the next host, which incurs unacceptable latency on node failure.

    A proper HA setup for LDAP directories should use a single virtual host that maps to multiple

    real hosts using a hardware load balancer.

  -->

  <property name="url" value="LDAP://192.168.0.1:389" />

  <!--  請根據環境資訊更換url的位址以及下面的連結帳号資訊

    Manager credentials are only required if your directory does not support anonymous searches.

    Never provide these credentials for FastBindLdapAuthenticationHandler since the user's

    credentials are used for the bind operation.

  -->

  <property name="userDn" value="test\test_admin"/>

  <property name="password" value="123"/>

  <!-- Place JNDI environment properties here. -->

  <property name="baseEnvironmentProperties">

    <map>

      <!-- Three seconds is an eternity to users. -->

      <entry key="com.sun.jndi.ldap.connect.timeout" value="3000" />

      <entry key="com.sun.jndi.ldap.read.timeout" value="3000" />

      <!-- Explained at http://download.oracle.com/javase/1.3/docs/api/javax/naming/Context.html#SECURITY_AUTHENTICATION -->

      <entry key="java.naming.security.authentication" value="simple" />

    </map>

  </property>

  </bean>

完成以上修改後,将改好的cas-ad檔案夾進行打包,打包示例:

jar cvf cas-ad.war -C cas-ad /.

-C是指的目前需要編譯的目錄  /.是包含所有的内容

重新部署後,可通路LDAP域資訊了

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

補充下關于LDAP的知識  轉載于

常用的LDAP伺服器有以下幾種

     1:Apache directory server

     2:Sun directory server 

     3:openDS 一個開源的,基于LDAP和DSML标準的Directory service。Directory service不僅包括Directory server,還有其它與directory相關的基本service:directory  proxy、virtual dirctory、namespace distribution和資料同步Directory server是一個可以通過網絡通路,資訊分級存儲的資料庫。OpenDS隻能用在linux作業系統。該項目的位址為:http://www.opends.org/ 

    4: Netscape Directory Server

    5: Window AD

    6: IBM Tivoli Diectory Server

LDAP認證配置有兩種:

[第一種]、FastBindLdapAuthenticationHandler

這種認證處理器一般用于DN是由使用者名直接組成的,比如:uid=%u,ou=dev,dc=micmiu.com,dc=com ,其中 %u 就是CAS登

錄的使用者名。

修改web的配置檔案 WEB-INF\deployerConfigContext.xml:

首先在<beans>根節點下添加bean:ContextSource 的配置:

 <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">

   <property name="pooled" value="false"/>

   <property name="url" value="ldap://ldapServerIp:389" />

   <property name="userDn" value="cn=root"/>

   <property name="password" value="your_password"/>

   <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>

ContextSource 的配置說明:

(1)如果有多個LDAP伺服器,可以通過參數urls 配置多個。

(2)FastBindLdapAuthenticationHandler配置時,這裡的userDn 可以配置成 “cn=root,ou=dev,dc=micmiu,dc=com”

或 “cn=root,ou=dev”  或 “cn=root” 或 “root” 這四個都可以。其中的cn=root為LDAP伺服器執行個體的管理者使用者,password為對應的密碼。

(3)如果LDAP伺服器有SSL,注意url配置的字首是ldaps:”ldaps://ldapServerIp:636″。

在<bean id=”authenticationManager” />下找到SimpleTestUsernamePasswordAuthenticationHandler的配置,修改

成如下:

 <bean class="org.jasig.cas.adaptors.ldap.FastBindLdapAuthenticationHandler">

     <property name="filter" value="uid=%u,ou=dev,dc=micmiu,dc=com" />

     <property name="contextSource" ref="contextSource" />

 </bean>

[第二種]、BindLdapAuthenticationHandler

這種認證處理器一般用于需要驗證的使用者名是DN的其他的屬性比如email,而不是上面第一種處理器中的uid(當然uid屬性同

樣适用,下面我們配置的示例就還是用uid)。

修改web的配置檔案 WEB-INF\deployerConfigContext.xml:

同樣在<beans>根節點下添加bean:ContextSource 的配置:

 <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">

     <property name="anonymousReadOnly" value="false" />

     <property name="password" value="your_password" />

     <property name="pooled" value="false" />

     <property name="urls">

         <list>

             <value>ldap://ldapServerIp:389</value>

         </list>

     </property>

     <property name="userDn" value="cn=root,dc=micmiu,dc=com" />

     <property name="baseEnvironmentProperties">

         <map>

             <!-- LDAP SSL通路配置

             <entry key="java.naming.security.protocol" value="ssl" />

                 -->

             <entry key="java.naming.security.authentication" value="simple" />

         </map>

     </property>

 </bean>

在<bean id=”authenticationManager” />修改認證bean的配置,修改成如下:

 <bean class="org.jasig.cas.adaptors.ldap.BindLdapAuthenticationHandler">

     <property name="filter" value="uid=%u" />

     <property name="searchBase" value="dc=micmiu,dc=com" />

     <property name="contextSource" ref="contextSource" />

     <!-- 允許多個賬号-->

     <property name="allowMultipleAccounts" value="true" />

 </bean>