javascript
Spring Security 学习之LDAP认证
一、前言
LDAP:輕型目錄訪問協議,即Lightweight Directory Access Protocol (LDAP)是一個訪問在線目錄服務的協議,最典型例子的就是黃頁、電話簿等,主要用于讀極多并且寫極少的場景。
LDAP服務器:
-
OpenLDAP:官網--http://www.openldap.org/??, 有豐富LDAP相關文檔。
-
ADS:官網--https://directory.apache.org/,支持eclipse插件方式安裝,提供LDAP開發的各種工具。
二、Spring配置
ldap-server:
目標LDAP配置,即協議、IP、端口、用戶名、密碼等,如:
| 1 | <security:ldap-server?url="ldap://localhost:10389/o=apple"?manager-dn="uid=admin,ou=system"?manager-password="secret"?/> |
Spring支持內嵌的LDAP測試服務器配置,ADS有提供相關的jar,內嵌LDAP服務器只需要指定root和ldif文件即可,不需要協議IP端口等配置,ldif文件可以使用ADS開發工具編輯,如:
| 1 | <security:ldap-server?root="o=apple"?ldif="classpath:apple.ldif"/> |
ldap-authentication-provider:
用戶認證授權相關配置,主要是用戶配置用戶組合用戶過濾規則,方便快速從LDAP服務器定位到用戶信息,如:
| 1 2 3 4 5 6 7 8 9 10 | <security:authentication-manager> ????????????<security:ldap-authentication-provider? ????????????????????user-search-filter="(uid={0})" ????????????????????user-search-base="ou=users" ????????????????????group-search-filter="(uniqueMember={0})" ????????????????????group-search-base="ou=groups" ????????????????????group-role-attribute="cn" ????????????????????role-prefix="ROLE_"> ????????????</security:ldap-authentication-provider> ????</security:authentication-manager> |
user-dn-pattern: 用戶dn過濾規則,如user-dn-pattern="uid={0},ou=people", uid={0}表示過濾uid屬性
user-search-base: 指定從哪個分支開始查找用戶,如user-search-base="ou=people",默認從root開始
user-search-filter: 用戶過濾規則,如user-search-filter="(uid={0})" ,與user-dn-pattern類似,使用一個即可
group-search-base: 指定從哪個分支開始查找用戶用戶組,如group-search-base="ou=groups"
group-search-filter: 用戶組過濾規則,如group-search-filter="(uniqueMember={0})"表示使用uniqueMember屬性過濾
group-role-attribute: 用戶組中Role映射屬性,默認為cn,驗證時會自動增加role前綴
role-prefix?: role前綴, 默認為"ROLE_",即role-prefix="ROLE_"
通過以上基本的配置即可支持LDAP認證授權。
三、高級配置
Spring強大的功能之一就是在提供簡便配置的同時支持自己定制,而定制最基礎的就是bean配置,通過替換bean的實現可以替換掉spring原先提供的默認實現。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <bean?id="contextSource" ????????class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> ??<constructor-arg?value="ldap://monkeymachine:389/dc=springframework,dc=org"/> ??<property?name="userDn"?value="cn=manager,dc=springframework,dc=org"/> ??<property?name="password"?value="password"/> </bean> <bean?id="ldapAuthProvider" ????class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> ?<constructor-arg> ???<bean?class="org.springframework.security.ldap.authentication.BindAuthenticator"> ?????<constructor-arg?ref="contextSource"/> ?????<property?name="userDnPatterns"> ???????<list><value>uid={0},ou=people</value></list> ?????</property> ???</bean> ?</constructor-arg> ?<constructor-arg> ???<bean ?????class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator"> ?????<constructor-arg?ref="contextSource"/> ?????<constructor-arg?value="ou=groups"/> ?????<property?name="groupRoleAttribute"?value="ou"/> ???</bean> ?</constructor-arg> </bean> <bean?id="userSearch" ????class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> ??<constructor-arg?index="0"?value=""/> ??<constructor-arg?index="1"?value="(uid={0})"/> ??<constructor-arg?index="2"?ref="contextSource"?/> </bean> |
Spring已經提供對LDAP認證的強大支持,一般情況下不需要自己定制。
? ? ?本文轉自sarchitect 51CTO博客,原文鏈接:http://blog.51cto.com/stevex/1362706,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的Spring Security 学习之LDAP认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (六)构建Docker私有仓库、Gitl
- 下一篇: Windows 2000配置Web服务器