我最新最全的文章都在南瓜慢說 www.pkslow.com,歡迎大家來喝茶!
1 前言
Spring Cloud Data Flow
整合UAA
的文章已經寫了兩篇,之前的方案是把使用者資訊儲存在資料庫中;但在許多企業,是使用AD
來管理賬戶資訊,本文將講解如何整合Data Flow
和LDAP
。
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
。
這個對映關係的後半部分之前講解了,前半部分通過uaac
或Rest 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 Flow
與LDAP
的整合,至此,在Spring Cloud Data Flow
的鑑權方面,已經講述比較完整了。
程式碼請檢視:https://github.com/LarryDpk/pkslow-samples
參考文件:
歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...
多讀書,多分享;多寫作,多整理。