[FIXED HUDSON-1445] Supported the ldaps:// protocol.

Given that LDAPS is deprecated in favor of startTLS in LDAP v3, I felt it doesn't deserve the real estate of having a separate checkbox, so the approach is somewhat clumsy.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11664 71c3de6d-444a-0410-be80-ed276b4c234a

Originally-Committed-As: be7f3ab5f64bf087d634a8d25d9dc3f47d367775
This commit is contained in:
kohsuke 2008-08-22 01:36:16 +00:00
parent 384de4b6ba
commit 7d54470c20

View file

@ -10,7 +10,6 @@ import hudson.model.User;
import hudson.util.FormFieldValidator;
import hudson.util.Scrambler;
import hudson.util.spring.BeanBuilder;
import net.sf.json.JSONObject;
import org.acegisecurity.AuthenticationManager;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UserDetails;
@ -128,6 +127,10 @@ public class LDAPSecurityRealm extends SecurityRealm {
this.managerPassword = Scrambler.scramble(Util.fixEmpty(managerPassword));
}
public String getServerUrl() {
return addPrefix(server);
}
/**
* Infer the root DN.
*
@ -140,7 +143,7 @@ public class LDAPSecurityRealm extends SecurityRealm {
props.put(Context.SECURITY_PRINCIPAL,managerDN);
props.put(Context.SECURITY_CREDENTIALS,getManagerPassword());
}
DirContext ctx = LdapCtxFactory.getLdapCtxInstance("ldap://"+server+'/', props);
DirContext ctx = LdapCtxFactory.getLdapCtxInstance(getServerUrl()+'/', props);
Attributes atts = ctx.getAttributes("");
Attribute a = atts.get("defaultNamingContext");
if(a!=null) // this entry is available on Active Directory. See http://msdn2.microsoft.com/en-us/library/ms684291(VS.85).aspx
@ -163,7 +166,7 @@ public class LDAPSecurityRealm extends SecurityRealm {
}
public String getLDAPURL() {
return "ldap://"+server+'/'+Util.fixNull(rootDN);
return getServerUrl()+'/'+Util.fixNull(rootDN);
}
public SecurityComponents createSecurityComponents() {
@ -255,7 +258,7 @@ public class LDAPSecurityRealm extends SecurityRealm {
if(managerPassword!=null && managerPassword.trim().length() > 0 && !"undefined".equals(managerPassword)) {
props.put(Context.SECURITY_CREDENTIALS,managerPassword);
}
DirContext ctx = LdapCtxFactory.getLdapCtxInstance("ldap://"+server+'/', props);
DirContext ctx = LdapCtxFactory.getLdapCtxInstance(addPrefix(server)+'/', props);
ctx.getAttributes("");
ok(); // connected
} catch (NamingException e) {
@ -293,6 +296,15 @@ public class LDAPSecurityRealm extends SecurityRealm {
}
}
/**
* If the given "server name" is just a host name (plus optional host name), add ldap:// prefix.
* Otherwise assume it already contains the scheme, and leave it intact.
*/
private static String addPrefix(String server) {
if(server.contains("://")) return server;
else return "ldap://"+server;
}
static {
LIST.add(DESCRIPTOR);
}