Spring Cloud Data Flow整合UAA之使用LDAP進行賬號管理


我最新最全的文章都在 南瓜慢說 www.pkslow.com ,歡迎大家來喝茶!

1 前言

Spring Cloud Data Flow整合UAA的文章已經寫了兩篇,之前的方案是把用戶信息保存在數據庫中;但在許多企業,是使用AD來管理賬戶信息,本文將講解如何整合Data FlowLDAP

Spring Cloud Data Flow相關文章:

Spring Cloud Data Flow初體驗,以Local模式運行

把Spring Cloud Data Flow部署在Kubernetes上,再跑個任務試試

Spring Cloud Data Flow用Shell來操作,方便建立CICD

被Spring坑了一把,查看源碼終於解決了DataFlow部署K8s應用的問題

Spring Cloud Data Flow整合Cloudfoundry UAA服務做權限控制

Spring Cloud Data Flow整合UAA使用外置數據庫和API接口

2 啟動LDAP服務器

2.1 啟動服務器

我們使用Apache的開源框架來作為Ldap服務器,引入依賴如下:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.1.0.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-protocol-ldap</artifactId>
    <version>1.5.5</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
  </dependency>
</dependencies>

Springboot的啟動類如下:

@SpringBootApplication
public class LdapServer {
    public static void main(String[] args) throws Throwable {
        SpringApplication.run(LdapServer.class, args);
    }

    @Bean
    public ApacheDSContainer apacheDSContainer() throws Exception {
        final File temporaryFolder = Files.createTempDirectory("ldap_server").toFile();
        final String ldapFileName = "testUsers.ldif";

        ApacheDSContainer apacheDSContainer = new ApacheDSContainer("dc=springframework,dc=org",
                "classpath:" + ldapFileName);

        apacheDSContainer.setPort(40000);
        final File workingDir = new File(temporaryFolder, UUID.randomUUID().toString());
        apacheDSContainer.setWorkingDirectory(workingDir);
        return apacheDSContainer;
    }
}

啟動端口為40000,用戶配置信息ldif文件為testUsers.ldif,我們把測試使用到的AD賬戶和群組信息都配置在這個文件里。dc=springframework,dc=org是AD的根目錄,所有配置信息樹的起點。

testUsers.ldif比較大,請參考:https://github.com/LarryDpk/pkslow-samples/blob/master/spring-cloud/ldap-server/src/main/resources/testUsers.ldif

2.2 連接服務器

啟動了Ldap服務器后,我們可以通過Apache Directory Studio客戶端工具來進行查看和管理。如下圖所示:

3 UAA配置

UAA服務器需要配置相關信息以連接Ldap服務,配置在uaa.yml文件中,具體添加的配置如下:

spring_profiles: default,postgresql,ldap

ldap:
  profile:
    file: ldap/ldap-search-and-bind.xml
  base:
    url: 'ldap://localhost:40000/'
    userDn: 'uid=leah,ou=people,dc=springframework,dc=org'
    password: 'leahberlin'
    searchBase: 'ou=otherpeople,dc=springframework,dc=org'
    searchFilter: 'uid={0}'
    referral: follow
  groups:
    file: 'ldap/ldap-groups-map-to-scopes.xml'
    searchBase: 'ou=groups,dc=springframework,dc=org'
    searchSubtree: true
    groupSearchFilter: member={0}
    maxSearchDepth: 10
    autoAdd: true

profiles需要添加ldap來打開這個功能。

添加配置后,重啟UAA服務器即可生效。但我們現在可以通過用戶的登陸信息獲取他的AD群組,但這個群組與UAA的群組是不一樣的,需要為它們建立一個映射關系。即:

AD group --> UAA group --> Data Flow Role

這個映射關系的后半部分之前講解了,前半部分通過uaacRest API可以配置,如下:

uaac group map "cn=view,ou=groups,dc=springframework,dc=org" --name="dataflow.view" --origin=ldap
uaac group map "cn=create,ou=groups,dc=springframework,dc=org" --name="dataflow.create" --origin=ldap
uaac group map "cn=manage,ou=groups,dc=springframework,dc=org" --name="dataflow.manage" --origin=ldap

4 登陸測試

我們直接用ldif文件配置的用戶marlene/supersecret登陸如下:

實際上,我們依舊可以使用保存在數據庫中賬號(如larry/larry)登陸,它們是可以並存的,提供了很大的便利性。

5 總結

本文講解了Data FlowLDAP的整合,至此,在Spring Cloud Data Flow的鑒權方面,已經講述比較完整了。

代碼請查看:https://github.com/LarryDpk/pkslow-samples


參考文檔:

security-ldap-uaa-example

OpenLDAP 概念與工作原理介紹


歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...

多讀書,多分享;多寫作,多整理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM