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 security LDAP获取用户信息

    很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...

  2. 1. Spring Security 框架简介

    官网:https://projects.spring.io/spring-security/Spring Security 是强大的,且容易定制的实现认证,与授权的基于 Spring 开发的框架.Sp ...

  3. Spring Security构建Rest服务-1200-SpringSecurity OAuth开发APP认证框架

    基于服务器Session的认证方式: 前边说的用户名密码登录.短信登录.第三方登录,都是普通的登录,是基于服务器Session保存用户信息的登录方式.登录信息都是存在服务器的session(服务器的一 ...

  4. Spring Security详解

    Spring Security 一. 简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文 ...

  5. Spring Security 快速上手

    Spring Security 框架简介 Spring Security 说明 Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案 关于安全方面的两 ...

  6. Spring Security探究之路之开始

    前言 在Spring Security介绍中,我们分析到了根据请求获取匹配的SecurityFilterChain,这个类中包含了一组Filter 接下来我们从这些Filter开始探究之旅 Sprin ...

  7. springboot集成spring security安全框架入门篇

    一. :spring security的简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下 ...

  8. Spring Security 简介

    本文引自:https://blog.csdn.net/xlecho/article/details/80026527 在 Web 应用开发中,安全一直是非常重要的一个方面.安全虽然属于应用的非功能性需 ...

  9. 转 Spring Security 简介

    https://blog.csdn.net/xlecho/article/details/80026527 Spring Security 简介 2018年04月21日 09:53:02 阅读数:13 ...

随机推荐

  1. 关于RHEL5中yum挂载iso源引起的问题(转)

    今天在虚机上通过yum挂载iso源来安装rpm包,但提示错误,内容见下面.之前也有过这样的操作, mount后,修改repo文件,然后就可以yum install  rpm包了:过程很简单啊.不知道这 ...

  2. mongodb存储二进制数据的二种方式——binary bson或gridfs

    python 版本为2.7 mongodb版本2.6.5 使用mongodb存储文件,可以使用两种方式,一种是像存储普通数据那样,将文件转化为二进制数据存入mongodb,另一种使用gridfs,咱们 ...

  3. zlog 纯C日志函数库的简单使用方法

    zlog简述: log是一个高性能.线程安全.灵活.概念清晰的纯C日志函数库. 事实上,在C的世界里面没有特别好的日志函数库(就像JAVA里面的的log4j,或者C++的log4cxx).C程序员都喜 ...

  4. pdf2swf 转换时报This file is too complex to render- SWF only supports 65536 shapes at once

    ERROR   ID Table overflow ERROR   This file is too complex to render- SWF only supports 65536 shapes ...

  5. Struts过滤器

    StrutsPrepareAndExecuteFilter过滤器其实是包含2部分的 StrutsPrepareFilter:做准备 StrutsExecuteFilter:进入Struts2的核心处理 ...

  6. python数据分析笔记中panda(2)

    1 将手机号码分开为运营商,地区和号码段 from pandas import read_csv; df = read_csv("H:\\pythonCode\\4.6\\data.csv& ...

  7. 剑指offer面试题:输入某二叉树的前序遍历和中序遍历,输出后序遍历

    二叉树的先序,中序,后序如何遍历,不在此多说了.直接看题目描述吧(题目摘自九度oj剑指offer面试题6): 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结 ...

  8. 关于`babel-loader`和`babel-core`版本兼容性问题

    1. 安装babel-loader和babel-core出现问题 1.1 安装babel的转换工具包: npm i babel-core babel-loader babel-plugin-trans ...

  9. WinMain和main

    WinMain的原型: int WINAPI WinMain(HINSTANCE hinstance,//程序本身的实例句柄                                  HINS ...

  10. 洛谷P3321 [SDOI2015]序列统计(NTT)

    传送门 题意:$a_i\in S$,求$\prod_{i=1}^na_i\equiv x\pmod{m}$的方案数 这题目太珂怕了……数学渣渣有点害怕……kelin大佬TQL 设$f[i][j]$表示 ...