這篇文章主要為大家展示了“Spring Security LDAP的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Spring Security LDAP的示例分析”這篇文章吧。
1.概述
如何設置Spring Security LDAP。
在我們開始之前,了解一下LDAP是什么? - 它代表輕量級目錄訪問協議。它是一種開放的,與供應商無關的協議,用于通過網絡訪問目錄服務。
2. Maven Dependency
首先,讓我們看看我們需要的maven依賴項:
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-ldap</artifactId> </dependency> <dependency> <groupId>org.apache.directory.server</groupId> <artifactId>apacheds-server-jndi</artifactId> <version>1.5.5</version> </dependency>
注意:我們使用ApacheDS作為LDAP服務器,它是一個可擴展和可嵌入的目錄服務器。
3. Java Configuration
接下來,我們來討論我們的Spring Security Java配置:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userSearchBase("ou=people")
.userSearchFilter("(uid={0})")
.groupSearchBase("ou=groups")
.groupSearchFilter("member={0}")
.contextSource()
.root("dc=baeldung,dc=com")
.ldif("classpath:users.ldif");
}
}這當然只是配置的LDAP相關部分 - 可以在此處找到完整的Java配置。
4. XML Configuration
現在,我們來看看相應的XML配置:
<authentication-manager>
<ldap-authentication-provider
user-search-base="ou=people"
user-search-filter="(uid={0})"
group-search-base="ou=groups"
group-search-filter="(member={0})">
</ldap-authentication-provider>
</authentication-manager>
<ldap-server root="dc=baeldung,dc=com" ldif="users.ldif"/>同樣,這只是配置的一部分 - 與LDAP相關的部分;完整的XML配置可以在這里找到。
5. LDAP數據交換格式
LDAP數據可以使用LDAP數據交換格式(LDIF)表示 - 這是我們的用戶數據的示例:
dn: ou=groups,dc=baeldung,dc=com objectclass: top objectclass: organizationalUnit ou: groups dn: ou=people,dc=baeldung,dc=com objectclass: top objectclass: organizationalUnit ou: people dn: uid=baeldung,ou=people,dc=baeldung,dc=com objectclass: top objectclass: person objectclass: organizationalPerson objectclass: inetOrgPerson cn: Jim Beam sn: Beam uid: baeldung userPassword: password dn: cn=admin,ou=groups,dc=baeldung,dc=com objectclass: top objectclass: groupOfNames cn: admin member: uid=baeldung,ou=people,dc=baeldung,dc=com dn: cn=user,ou=groups,dc=baeldung,dc=com objectclass: top objectclass: groupOfNames cn: user member: uid=baeldung,ou=people,dc=baeldung,dc=com
6. The Application
最后,這是我們的簡單應用:
@Controller
public class MyController {
@RequestMapping("/secure")
public String secure(Map<String, Object> model, Principal principal) {
model.put("title", "SECURE AREA");
model.put("message", "Only Authorized Users Can See This Page");
return "home";
}
}7.總結
在這本使用LDAP的Spring Security快速指南中,我們學習了如何使用LDIF配置基本系統并在spring security配置LDAP。
可以在github項目中找到本教程的完整實現 - 這是一個基于Eclipse的項目,因此它應該很容易導入和運行。
以上是“Spring Security LDAP的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。