From c16bc0ca8e064b96cad6d3cc3670f5ef6b21a54a Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 27 Oct 2020 17:53:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(LDAP):=20=E6=94=AF=E6=8C=81LDAPS,=20?= =?UTF-8?q?=E5=8C=BA=E5=88=86ldap=E5=92=8Cldaps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/ldap/service/LdapService.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/io/metersphere/ldap/service/LdapService.java b/backend/src/main/java/io/metersphere/ldap/service/LdapService.java index 60a1ce0145..44aa6bc84a 100644 --- a/backend/src/main/java/io/metersphere/ldap/service/LdapService.java +++ b/backend/src/main/java/io/metersphere/ldap/service/LdapService.java @@ -18,6 +18,7 @@ import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextMapper; import org.springframework.ldap.core.support.DefaultDirObjectFactory; +import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.query.SearchScope; import org.springframework.stereotype.Service; @@ -145,17 +146,21 @@ public class LdapService { preConnect(url, dn, password); String credentials = EncryptUtils.aesDecrypt(password).toString(); - - SSLLdapContextSource sourceLdapCtx = new SSLLdapContextSource(); - sourceLdapCtx.setUrl(url); - sourceLdapCtx.setUserDn(dn); - sourceLdapCtx.setPassword(credentials); - sourceLdapCtx.setDirObjectFactory(DefaultDirObjectFactory.class); - // todo 这里加上strategy 会报错 + LdapContextSource sourceLdapCtx; + if (StringUtils.startsWith(url, "ldaps://")) { + sourceLdapCtx = new SSLLdapContextSource(); + // todo 这里加上strategy 会报错 // DefaultTlsDirContextAuthenticationStrategy strategy = new DefaultTlsDirContextAuthenticationStrategy(); // strategy.setShutdownTlsGracefully(true); // strategy.setHostnameVerifier((hostname, session) -> true); // sourceLdapCtx.setAuthenticationStrategy(strategy); + } else { + sourceLdapCtx = new LdapContextSource(); + } + sourceLdapCtx.setUrl(url); + sourceLdapCtx.setUserDn(dn); + sourceLdapCtx.setPassword(credentials); + sourceLdapCtx.setDirObjectFactory(DefaultDirObjectFactory.class); sourceLdapCtx.afterPropertiesSet(); LdapTemplate ldapTemplate = new LdapTemplate(sourceLdapCtx); ldapTemplate.setIgnorePartialResultException(true);