日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Security 学习之LDAP认证

發布時間:2025/3/21 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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认证的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。