zoukankan      html  css  js  c++  java
  • 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的项目,因此它应该很容易导入和运行。

  • 相关阅读:
    【移动开发】Android应用程序中实用的代码框架(二)
    使用ListView应该注意的地方
    使用ListView应该注意的地方
    web技术文章
    [LeetCode] 565. Array Nesting
    [LeetCode]495. Teemo Attacking
    south 命令学习
    关于access_token过期的解决办法
    决策树总结《一》.md
    crontab命令
  • 原文地址:https://www.cnblogs.com/xjknight/p/10905208.html
Copyright © 2011-2022 走看看