瀏覽代碼

LDAP: add admin auth and search filter (#1403)

Co-authored-by: Roman Zabaluev <rzabaluev@provectus.com>
aizerin 3 年之前
父節點
當前提交
2c6a197cb3

+ 8 - 0
docker/auth-ldap.yaml

@@ -29,6 +29,14 @@ services:
       AUTH_TYPE: "LDAP"
       AUTH_TYPE: "LDAP"
       SPRING_LDAP_URLS: "ldap://ldap:10389"
       SPRING_LDAP_URLS: "ldap://ldap:10389"
       SPRING_LDAP_DN_PATTERN: "cn={0},ou=people,dc=planetexpress,dc=com"
       SPRING_LDAP_DN_PATTERN: "cn={0},ou=people,dc=planetexpress,dc=com"
+#     USER SEARCH FILTER INSTEAD OF DN
+#     SPRING_LDAP_USERFILTER_SEARCHBASE: "dc=planetexpress,dc=com"
+#     SPRING_LDAP_USERFILTER_SEARCHFILTER: "(&(uid={0})(objectClass=inetOrgPerson))"
+#     LDAP ADMIN USER
+#     SPRING_LDAP_ADMINUSER: "cn=admin,dc=planetexpress,dc=com"
+#     SPRING_LDAP_ADMINPASSWORD: "GoodNewsEveryone"
+
+
 
 
   ldap:
   ldap:
     image: rroemhild/test-openldap:latest
     image: rroemhild/test-openldap:latest

+ 21 - 2
kafka-ui-api/src/main/java/com/provectus/kafka/ui/config/auth/LdapSecurityConfig.java

@@ -16,6 +16,8 @@ import org.springframework.security.config.annotation.web.reactive.EnableWebFlux
 import org.springframework.security.config.web.server.ServerHttpSecurity;
 import org.springframework.security.config.web.server.ServerHttpSecurity;
 import org.springframework.security.ldap.authentication.BindAuthenticator;
 import org.springframework.security.ldap.authentication.BindAuthenticator;
 import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
 import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
+import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
+import org.springframework.security.ldap.search.LdapUserSearch;
 import org.springframework.security.web.server.SecurityWebFilterChain;
 import org.springframework.security.web.server.SecurityWebFilterChain;
 
 
 @Configuration
 @Configuration
@@ -26,13 +28,28 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
 
 
   @Value("${spring.ldap.urls}")
   @Value("${spring.ldap.urls}")
   private String ldapUrls;
   private String ldapUrls;
-  @Value("${spring.ldap.dn.pattern}")
+  @Value("${spring.ldap.dn.pattern:#{null}}")
   private String ldapUserDnPattern;
   private String ldapUserDnPattern;
+  @Value("${spring.ldap.adminUser:#{null}}")
+  private String adminUser;
+  @Value("${spring.ldap.adminPassword:#{null}}")
+  private String adminPassword;
+  @Value("${spring.ldap.userFilter.searchBase:#{null}}")
+  private String userFilterSearchBase;
+  @Value("${spring.ldap.userFilter.searchFilter:#{null}}")
+  private String userFilterSearchFilter;
 
 
   @Bean
   @Bean
   public ReactiveAuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
   public ReactiveAuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
     BindAuthenticator ba = new BindAuthenticator(contextSource);
     BindAuthenticator ba = new BindAuthenticator(contextSource);
-    ba.setUserDnPatterns(new String[]{ldapUserDnPattern});
+    if (ldapUserDnPattern != null) {
+      ba.setUserDnPatterns(new String[]{ldapUserDnPattern});
+    }
+    if (userFilterSearchFilter != null) {
+      LdapUserSearch userSearch =
+              new FilterBasedLdapUserSearch(userFilterSearchBase, userFilterSearchFilter, contextSource);
+      ba.setUserSearch(userSearch);
+    }
 
 
     LdapAuthenticationProvider lap = new LdapAuthenticationProvider(ba);
     LdapAuthenticationProvider lap = new LdapAuthenticationProvider(ba);
 
 
@@ -45,6 +62,8 @@ public class LdapSecurityConfig extends AbstractAuthSecurityConfig {
   public BaseLdapPathContextSource contextSource() {
   public BaseLdapPathContextSource contextSource() {
     LdapContextSource ctx = new LdapContextSource();
     LdapContextSource ctx = new LdapContextSource();
     ctx.setUrl(ldapUrls);
     ctx.setUrl(ldapUrls);
+    ctx.setUserDn(adminUser);
+    ctx.setPassword(adminPassword);
     ctx.afterPropertiesSet();
     ctx.afterPropertiesSet();
     return ctx;
     return ctx;
   }
   }