env = new Hashtable<>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
String bindPrincipal = createBindPrincipal(username);
env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
- env.put(Context.PROVIDER_URL, bindUrl);
+ env.put(Context.PROVIDER_URL, this.url);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.OBJECT_FACTORIES, DefaultDirObjectFactory.class.getName());
diff --git a/ldap/src/main/java/org/springframework/security/ldap/jackson2/InetOrgPersonMixin.java b/ldap/src/main/java/org/springframework/security/ldap/jackson2/InetOrgPersonMixin.java
index fca449114e..21ff071976 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/jackson2/InetOrgPersonMixin.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/jackson2/InetOrgPersonMixin.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-2021 the original author or authors.
+ * Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ import org.springframework.security.ldap.userdetails.InetOrgPerson;
* @see LdapJackson2Module
* @see SecurityJackson2Modules
*/
-@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
+@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
diff --git a/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapAuthorityMixin.java b/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapAuthorityMixin.java
index 85fe16f5fd..7494687aba 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapAuthorityMixin.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapAuthorityMixin.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-2021 the original author or authors.
+ * Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ import org.springframework.security.ldap.userdetails.LdapAuthority;
* @see LdapJackson2Module
* @see SecurityJackson2Modules
*/
-@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
+@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
abstract class LdapAuthorityMixin {
diff --git a/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapJackson2Module.java b/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapJackson2Module.java
index 62cb17a11a..f84e8df620 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapJackson2Module.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapJackson2Module.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-2021 the original author or authors.
+ * Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.security.ldap.userdetails.Person;
* {@link LdapAuthorityMixin}, {@link LdapUserDetailsImplMixin}, {@link PersonMixin},
* {@link InetOrgPersonMixin}.
*
+ *
* If not already enabled, default typing will be automatically enabled as type info is
* required to properly serialize/deserialize objects. In order to use this module just
* add it to your {@code ObjectMapper} configuration.
diff --git a/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapUserDetailsImplMixin.java b/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapUserDetailsImplMixin.java
index a441102e6b..ef6e470b60 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapUserDetailsImplMixin.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/jackson2/LdapUserDetailsImplMixin.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-2021 the original author or authors.
+ * Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
* @see LdapJackson2Module
* @see SecurityJackson2Modules
*/
-@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
+@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
diff --git a/ldap/src/main/java/org/springframework/security/ldap/jackson2/PersonMixin.java b/ldap/src/main/java/org/springframework/security/ldap/jackson2/PersonMixin.java
index a3a0ddebc5..2063186658 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/jackson2/PersonMixin.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/jackson2/PersonMixin.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015-2021 the original author or authors.
+ * Copyright 2015-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ import org.springframework.security.ldap.userdetails.Person;
* @see LdapJackson2Module
* @see SecurityJackson2Modules
*/
-@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
+@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
diff --git a/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyAwareContextSource.java b/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyAwareContextSource.java
index 9c4ee19ecf..043b7cc1ec 100755
--- a/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyAwareContextSource.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyAwareContextSource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2016 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -77,7 +77,7 @@ public class PasswordPolicyAwareContextSource extends DefaultSpringSecurityConte
@Override
@SuppressWarnings("unchecked")
protected Hashtable getAuthenticatedEnv(String principal, String credentials) {
- Hashtable env = super.getAuthenticatedEnv(principal, credentials);
+ Hashtable env = super.getAuthenticatedEnv(principal, credentials);
env.put(LdapContext.CONTROL_FACTORIES, PasswordPolicyControlFactory.class.getName());
return env;
}
diff --git a/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyResponseControl.java b/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyResponseControl.java
index f0b89483d7..2aa2b330e0 100755
--- a/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyResponseControl.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/ppolicy/PasswordPolicyResponseControl.java
@@ -220,7 +220,7 @@ public class PasswordPolicyResponseControl extends PasswordPolicyControl {
}
}
- class SpecificTagDecoder extends BERTagDecoder {
+ static class SpecificTagDecoder extends BERTagDecoder {
/** Allows us to remember which of the two options we're decoding */
private Boolean inChoice = null;
diff --git a/ldap/src/main/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearch.java b/ldap/src/main/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearch.java
index d3f92397c9..326b5c5fa1 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearch.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearch.java
@@ -52,7 +52,7 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
/**
* Context name to search in, relative to the base of the configured ContextSource.
*/
- private String searchBase = "";
+ private final String searchBase;
/**
* The filter expression used in the user search. This is an LDAP search filter (as
@@ -78,9 +78,9 @@ public class FilterBasedLdapUserSearch implements LdapUserSearch {
this.contextSource = contextSource;
this.searchBase = searchBase;
setSearchSubtree(true);
- if (searchBase.length() == 0) {
+ if (searchBase.isEmpty()) {
logger.info(LogMessage.format("Searches will be performed from the root %s since SearchBase not set",
- contextSource.getBaseLdapPath()));
+ contextSource.getBaseLdapName()));
}
}
diff --git a/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java b/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java
index c93fa15b8c..ee9f6254f9 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2022 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.Lifecycle;
import org.springframework.core.io.Resource;
+import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
/**
@@ -43,7 +44,7 @@ public class UnboundIdContainer
private InMemoryDirectoryServer directoryServer;
- private String defaultPartitionSuffix;
+ private final String defaultPartitionSuffix;
private int port = 53389;
@@ -51,7 +52,7 @@ public class UnboundIdContainer
private boolean running;
- private String ldif;
+ private final String ldif;
public UnboundIdContainer(String defaultPartitionSuffix, String ldif) {
this.defaultPartitionSuffix = defaultPartitionSuffix;
@@ -79,7 +80,7 @@ public class UnboundIdContainer
}
@Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+ public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
diff --git a/ldap/src/main/java/org/springframework/security/ldap/userdetails/DefaultLdapAuthoritiesPopulator.java b/ldap/src/main/java/org/springframework/security/ldap/userdetails/DefaultLdapAuthoritiesPopulator.java
index b69985d2b4..0a1bc4035d 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/userdetails/DefaultLdapAuthoritiesPopulator.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/userdetails/DefaultLdapAuthoritiesPopulator.java
@@ -129,7 +129,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
/**
* The base DN from which the search for group membership should be performed
*/
- private String groupSearchBase;
+ private final String groupSearchBase;
/**
* The pattern to be used for the user search. {0} is the user's DN
@@ -166,7 +166,7 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
if (groupSearchBase == null) {
logger.info("Will not perform group search since groupSearchBase is null.");
}
- else if (groupSearchBase.length() == 0) {
+ else if (groupSearchBase.isEmpty()) {
logger.info("Will perform group search from the context source base since groupSearchBase is empty.");
}
this.authorityMapper = (record) -> {
@@ -365,16 +365,6 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
return this.convertToUpperCase;
}
- /**
- * Returns the default role Method available so that classes extending this can
- * override
- * @return the default role used
- * @see #setDefaultRole(String)
- */
- private GrantedAuthority getDefaultRole() {
- return this.defaultRole;
- }
-
/**
* Returns the search controls Method available so that classes extending this can
* override the search controls used
diff --git a/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapAuthority.java b/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapAuthority.java
index e08678b2a4..424fe11cc4 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapAuthority.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapAuthority.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2023 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,16 +31,16 @@ import org.springframework.util.Assert;
*/
public class LdapAuthority implements GrantedAuthority {
- private String dn;
+ private final String dn;
- private String role;
+ private final String role;
- private Map> attributes;
+ private final Map> attributes;
/**
* Constructs an LdapAuthority that has a role and a DN but no other attributes
- * @param role
- * @param dn
+ * @param role the principal's role
+ * @param dn the distinguished name
*/
public LdapAuthority(String role, String dn) {
this(role, dn, null);
@@ -48,9 +48,9 @@ public class LdapAuthority implements GrantedAuthority {
/**
* Constructs an LdapAuthority with the given role, DN and other LDAP attributes
- * @param role
- * @param dn
- * @param attributes
+ * @param role the principal's role
+ * @param dn the distinguished name
+ * @param attributes additional LDAP attributes
*/
public LdapAuthority(String role, String dn, Map> attributes) {
Assert.notNull(role, "role can not be null");
@@ -70,7 +70,7 @@ public class LdapAuthority implements GrantedAuthority {
/**
* Returns the DN for this LDAP authority
- * @return
+ * @return the distinguished name
*/
public String getDn() {
return this.dn;
@@ -91,7 +91,7 @@ public class LdapAuthority implements GrantedAuthority {
/**
* Returns the first attribute value for a specified attribute
- * @param name
+ * @param name the attribute name
* @return the first attribute value for a specified attribute, may be null
*/
public String getFirstAttributeValue(String name) {
diff --git a/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManager.java b/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManager.java
index 3ace03ebe8..8db6ec99da 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManager.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManager.java
@@ -44,7 +44,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.AttributesMapperCallbackHandler;
-import org.springframework.ldap.core.ContextExecutor;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DistinguishedName;
@@ -121,7 +120,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
private final LdapTemplate template;
/** Default context mapper used to create a set of roles from a list of attributes */
- private AttributesMapper roleMapper = (attributes) -> {
+ private AttributesMapper roleMapper = (attributes) -> {
Attribute roleAttr = attributes.get(this.groupRoleAttributeName);
NamingEnumeration> ne = roleAttr.getAll();
Object group = ne.next();
@@ -147,7 +146,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
}
private DirContextAdapter loadUserAsContext(final LdapName dn, final String username) {
- return (DirContextAdapter) this.template.executeReadOnly((ContextExecutor) (ctx) -> {
+ return this.template.executeReadOnly((ctx) -> {
try {
Attributes attrs = ctx.getAttributes(dn, this.attributesToRetrieve);
return new DirContextAdapter(attrs, LdapUtils.getFullDn(dn, ctx));
@@ -162,6 +161,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
* Changes the password for the current user. The username is obtained from the
* security context.
*
+ *
* There are two supported strategies for modifying the user's password depending on
* the capabilities of the corresponding LDAP server.
*
@@ -170,6 +170,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
* LDAP Password Modify
* Extended Operation .
*
+ *
* See {@link LdapUserDetailsManager#setUsePasswordModifyExtensionOperation(boolean)}
* for details.
*
@@ -205,7 +206,6 @@ public class LdapUserDetailsManager implements UserDetailsManager {
* @param username the user whose roles are required.
* @return the granted authorities returned by the group search
*/
- @SuppressWarnings("unchecked")
List getUserAuthorities(final LdapName dn, final String username) {
SearchExecutor se = (ctx) -> {
LdapName fullDn = LdapUtils.getFullDn(dn, ctx);
@@ -214,7 +214,8 @@ public class LdapUserDetailsManager implements UserDetailsManager {
return ctx.search(this.groupSearchBase, this.groupSearchFilter,
new String[] { fullDn.toString(), username }, ctrls);
};
- AttributesMapperCallbackHandler roleCollector = new AttributesMapperCallbackHandler(this.roleMapper);
+ AttributesMapperCallbackHandler roleCollector = new AttributesMapperCallbackHandler<>(
+ this.roleMapper);
this.template.search(se, roleCollector);
return roleCollector.getList();
}
@@ -229,7 +230,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
// Check for any existing authorities which might be set for this
// DN and remove them
List authorities = getUserAuthorities(dn, user.getUsername());
- if (authorities.size() > 0) {
+ if (!authorities.isEmpty()) {
removeAuthorities(dn, authorities);
}
addAuthorities(dn, user.getAuthorities());
@@ -322,7 +323,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
private void modifyAuthorities(final LdapName userDn, final Collection extends GrantedAuthority> authorities,
final int modType) {
- this.template.executeReadWrite((ContextExecutor) (ctx) -> {
+ this.template.executeReadWrite((ctx) -> {
for (GrantedAuthority authority : authorities) {
String group = convertAuthorityToGroup(authority);
LdapName fullDn = LdapUtils.getFullDn(userDn, ctx);
@@ -389,20 +390,26 @@ public class LdapUserDetailsManager implements UserDetailsManager {
/**
* Sets the method by which a user's password gets modified.
*
+ *
* If set to {@code true}, then {@link LdapUserDetailsManager#changePassword} will
* modify the user's password by way of the
* Password Modify
* Extension Operation.
*
+ *
* If set to {@code false}, then {@link LdapUserDetailsManager#changePassword} will
* modify the user's password by directly modifying attributes on the corresponding
* entry.
*
+ *
* Before using this setting, ensure that the corresponding LDAP server supports this
* extended operation.
*
+ *
* By default, {@code usePasswordModifyExtensionOperation} is false.
- * @param usePasswordModifyExtensionOperation
+ * @param usePasswordModifyExtensionOperation whether to use the
+ * Password Modify
+ * Extension Operation to modify the password
* @since 4.2.9
*/
public void setUsePasswordModifyExtensionOperation(boolean usePasswordModifyExtensionOperation) {
@@ -473,6 +480,7 @@ public class LdapUserDetailsManager implements UserDetailsManager {
* LDAP Password Modify
* Extended Operation client request.
*
+ *
* Can be directed at any LDAP server that supports the Password Modify Extended
* Operation.
*
diff --git a/ldap/src/main/java/org/springframework/security/ldap/userdetails/NestedLdapAuthoritiesPopulator.java b/ldap/src/main/java/org/springframework/security/ldap/userdetails/NestedLdapAuthoritiesPopulator.java
index b61068ec8f..a957209a7d 100644
--- a/ldap/src/main/java/org/springframework/security/ldap/userdetails/NestedLdapAuthoritiesPopulator.java
+++ b/ldap/src/main/java/org/springframework/security/ldap/userdetails/NestedLdapAuthoritiesPopulator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -176,7 +176,7 @@ public class NestedLdapAuthoritiesPopulator extends DefaultLdapAuthoritiesPopula
if (getAttributeNames() == null) {
setAttributeNames(new HashSet<>());
}
- if (StringUtils.hasText(getGroupRoleAttribute()) && !getAttributeNames().contains(getGroupRoleAttribute())) {
+ if (StringUtils.hasText(getGroupRoleAttribute())) {
getAttributeNames().add(getGroupRoleAttribute());
}
Set