Spring Security LDAP的示例分析

发布时间:2021-08-23 12:26:25 作者:小新
来源:亿速云 阅读:124

这篇文章主要为大家展示了“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的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 【总结】基于Spring LDAP和Spring Security的用户认证和权限控制Web实现
  2. Spring Security认证流程的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring security ldap

上一篇:Angular4依赖注入的示例分析

下一篇:Intellij IDEA插件开发的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》