|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2016 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2021 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -16,6 +16,10 @@
|
|
|
|
|
|
package org.springframework.security.ldap;
|
|
package org.springframework.security.ldap;
|
|
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.URLDecoder;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Hashtable;
|
|
import java.util.Hashtable;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -74,7 +78,7 @@ public class DefaultSpringSecurityContextSource extends LdapContextSource {
|
|
rootDn = (rootDn != null) ? rootDn : urlRootDn;
|
|
rootDn = (rootDn != null) ? rootDn : urlRootDn;
|
|
}
|
|
}
|
|
setUrls(urls.toArray(new String[0]));
|
|
setUrls(urls.toArray(new String[0]));
|
|
- setBase(rootDn);
|
|
|
|
|
|
+ setBase((rootDn != null) ? decodeUrl(rootDn) : null);
|
|
setPooled(true);
|
|
setPooled(true);
|
|
setAuthenticationStrategy(new SimpleDirContextAuthenticationStrategy() {
|
|
setAuthenticationStrategy(new SimpleDirContextAuthenticationStrategy() {
|
|
|
|
|
|
@@ -136,7 +140,7 @@ public class DefaultSpringSecurityContextSource extends LdapContextSource {
|
|
private static String buildProviderUrl(List<String> urls, String baseDn) {
|
|
private static String buildProviderUrl(List<String> urls, String baseDn) {
|
|
Assert.notNull(baseDn, "The Base DN for the LDAP server must not be null.");
|
|
Assert.notNull(baseDn, "The Base DN for the LDAP server must not be null.");
|
|
Assert.notEmpty(urls, "At least one LDAP server URL must be provided.");
|
|
Assert.notEmpty(urls, "At least one LDAP server URL must be provided.");
|
|
- String trimmedBaseDn = baseDn.trim();
|
|
|
|
|
|
+ String encodedBaseDn = encodeUrl(baseDn.trim());
|
|
StringBuilder providerUrl = new StringBuilder();
|
|
StringBuilder providerUrl = new StringBuilder();
|
|
for (String serverUrl : urls) {
|
|
for (String serverUrl : urls) {
|
|
String trimmedUrl = serverUrl.trim();
|
|
String trimmedUrl = serverUrl.trim();
|
|
@@ -147,11 +151,29 @@ public class DefaultSpringSecurityContextSource extends LdapContextSource {
|
|
if (!trimmedUrl.endsWith("/")) {
|
|
if (!trimmedUrl.endsWith("/")) {
|
|
providerUrl.append("/");
|
|
providerUrl.append("/");
|
|
}
|
|
}
|
|
- providerUrl.append(trimmedBaseDn);
|
|
|
|
|
|
+ providerUrl.append(encodedBaseDn);
|
|
providerUrl.append(" ");
|
|
providerUrl.append(" ");
|
|
}
|
|
}
|
|
return providerUrl.toString();
|
|
return providerUrl.toString();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static String encodeUrl(String url) {
|
|
|
|
+ try {
|
|
|
|
+ return URLEncoder.encode(url, StandardCharsets.UTF_8.toString());
|
|
|
|
+ }
|
|
|
|
+ catch (UnsupportedEncodingException ex) {
|
|
|
|
+ throw new IllegalStateException(ex);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String decodeUrl(String url) {
|
|
|
|
+ try {
|
|
|
|
+ return URLDecoder.decode(url, StandardCharsets.UTF_8.toString());
|
|
|
|
+ }
|
|
|
|
+ catch (UnsupportedEncodingException ex) {
|
|
|
|
+ throw new IllegalStateException(ex);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|