1. 程式人生 > >CAS+LDAP實現單點登入認證

CAS+LDAP實現單點登入認證

<groupId>org.jasig.cas</groupId> <artifactId>cas-server-support-ldap</artifactId> <version>${cas.version}</version> </dependency> <!-- 僅僅在使用到連線池時新增該依賴 <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>${apache.commons.pool.version}</version> </dependency> --> 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>