Skip to content

Commit c6a4880

Browse files
committed
Use LdapName instead of DistinguishedName
Closes gh-17301 Signed-off-by: Andrey Litvitski <[email protected]>
1 parent 08cbdb4 commit c6a4880

12 files changed

+39
-91
lines changed

ldap/src/main/java/org/springframework/security/ldap/DefaultLdapUsernameToDnMapper.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import javax.naming.ldap.LdapName;
2020

21-
import org.springframework.ldap.core.DistinguishedName;
2221
import org.springframework.ldap.support.LdapNameBuilder;
2322

2423
/**
@@ -44,18 +43,6 @@ public DefaultLdapUsernameToDnMapper(String userDnBase, String usernameAttribute
4443
this.usernameAttribute = usernameAttribute;
4544
}
4645

47-
/**
48-
* Assembles the Distinguished Name that should be used the given username.
49-
* @deprecated Use {@link #buildLdapName(String)} instead
50-
*/
51-
@Override
52-
@Deprecated
53-
public DistinguishedName buildDn(String username) {
54-
DistinguishedName dn = new DistinguishedName(this.userDnBase);
55-
dn.add(this.usernameAttribute, username);
56-
return dn;
57-
}
58-
5946
@Override
6047
public LdapName buildLdapName(String username) {
6148
return LdapNameBuilder.newInstance(this.userDnBase).add(this.usernameAttribute, username).build();
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,23 +18,13 @@
1818

1919
import javax.naming.ldap.LdapName;
2020

21-
import org.springframework.ldap.core.DistinguishedName;
22-
2321
/**
2422
* Constructs an Ldap Distinguished Name from a username.
2523
*
2624
* @author Luke Taylor
2725
*/
2826
public interface LdapUsernameToDnMapper {
2927

30-
/**
31-
* @deprecated Use {@link #buildLdapName(String)} instead
32-
*/
33-
@Deprecated
34-
DistinguishedName buildDn(String username);
35-
36-
default LdapName buildLdapName(String username) {
37-
return org.springframework.ldap.support.LdapUtils.newLdapName(buildDn(username));
38-
}
28+
LdapName buildLdapName(String username);
3929

4030
}

ldap/src/main/java/org/springframework/security/ldap/LdapUtils.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.apache.commons.logging.LogFactory;
2929

3030
import org.springframework.ldap.core.DirContextAdapter;
31-
import org.springframework.ldap.core.DistinguishedName;
3231
import org.springframework.ldap.support.LdapNameBuilder;
3332
import org.springframework.security.crypto.codec.Utf8;
3433
import org.springframework.util.Assert;
@@ -101,18 +100,7 @@ public static String getRelativeName(String fullDn, Context baseCtx) throws Nami
101100
/**
102101
* Gets the full dn of a name by prepending the name of the context it is relative to.
103102
* If the name already contains the base name, it is returned unaltered.
104-
* @deprecated Use {@link #getFullDn(LdapName, Context)}
105103
*/
106-
@Deprecated
107-
public static DistinguishedName getFullDn(DistinguishedName dn, Context baseCtx) throws NamingException {
108-
DistinguishedName baseDn = new DistinguishedName(baseCtx.getNameInNamespace());
109-
if (dn.contains(baseDn)) {
110-
return dn;
111-
}
112-
baseDn.append(dn);
113-
return baseDn;
114-
}
115-
116104
public static LdapName getFullDn(LdapName dn, Context baseCtx) throws NamingException {
117105
LdapName baseDn = LdapNameBuilder.newInstance(baseCtx.getNameInNamespace()).build();
118106
if (dn.startsWith(baseDn)) {

ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManager.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,6 @@
4848
import org.springframework.ldap.core.AttributesMapperCallbackHandler;
4949
import org.springframework.ldap.core.ContextSource;
5050
import org.springframework.ldap.core.DirContextAdapter;
51-
import org.springframework.ldap.core.DistinguishedName;
5251
import org.springframework.ldap.core.LdapTemplate;
5352
import org.springframework.ldap.core.SearchExecutor;
5453
import org.springframework.ldap.support.LdapNameBuilder;
@@ -289,39 +288,23 @@ public boolean userExists(String username) {
289288
* Creates a DN from a group name.
290289
* @param group the name of the group
291290
* @return the DN of the corresponding group, including the groupSearchBase
292-
* @deprecated
293291
*/
294-
@Deprecated
295-
protected DistinguishedName buildGroupDn(String group) {
296-
DistinguishedName dn = new DistinguishedName(this.groupSearchBase);
297-
dn.add(this.groupRoleAttributeName, group.toLowerCase(Locale.ROOT));
298-
return dn;
299-
}
300-
301-
protected LdapName buildGroupName(String group) {
302-
return LdapNameBuilder.newInstance(buildGroupDn(group)).build();
292+
protected LdapName buildGroupDn(String group) {
293+
return LdapNameBuilder.newInstance(this.groupSearchBase)
294+
.add(this.groupRoleAttributeName, group.toLowerCase(Locale.ROOT))
295+
.build();
303296
}
304297

305298
protected void copyToContext(UserDetails user, DirContextAdapter ctx) {
306299
this.userDetailsMapper.mapUserToContext(user, ctx);
307300
}
308301

309-
@Deprecated
310-
protected void addAuthorities(DistinguishedName userDn, Collection<? extends GrantedAuthority> authorities) {
311-
modifyAuthorities(LdapNameBuilder.newInstance(userDn).build(), authorities, DirContext.ADD_ATTRIBUTE);
312-
}
313-
314302
protected void addAuthorities(LdapName userDn, Collection<? extends GrantedAuthority> authorities) {
315-
addAuthorities(new DistinguishedName(userDn), authorities);
316-
}
317-
318-
@Deprecated
319-
protected void removeAuthorities(DistinguishedName userDn, Collection<? extends GrantedAuthority> authorities) {
320-
modifyAuthorities(LdapNameBuilder.newInstance(userDn).build(), authorities, DirContext.REMOVE_ATTRIBUTE);
303+
modifyAuthorities(LdapNameBuilder.newInstance(userDn).build(), authorities, DirContext.ADD_ATTRIBUTE);
321304
}
322305

323306
protected void removeAuthorities(LdapName userDn, Collection<? extends GrantedAuthority> authorities) {
324-
removeAuthorities(new DistinguishedName(userDn), authorities);
307+
modifyAuthorities(LdapNameBuilder.newInstance(userDn).build(), authorities, DirContext.REMOVE_ATTRIBUTE);
325308
}
326309

327310
private void modifyAuthorities(final LdapName userDn, final Collection<? extends GrantedAuthority> authorities,
@@ -332,7 +315,7 @@ private void modifyAuthorities(final LdapName userDn, final Collection<? extends
332315
LdapName fullDn = LdapUtils.getFullDn(userDn, ctx);
333316
ModificationItem addGroup = new ModificationItem(modType,
334317
new BasicAttribute(this.groupMemberAttributeName, fullDn.toString()));
335-
ctx.modifyAttributes(buildGroupName(group), new ModificationItem[] { addGroup });
318+
ctx.modifyAttributes(buildGroupDn(group), new ModificationItem[] { addGroup });
336319
}
337320
return null;
338321
});

ldap/src/test/java/org/springframework/security/ldap/SpringSecurityAuthenticationSourceTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.ldap.core.AuthenticationSource;
24-
import org.springframework.ldap.core.DistinguishedName;
24+
import org.springframework.ldap.support.LdapNameBuilder;
2525
import org.springframework.security.authentication.AnonymousAuthenticationToken;
2626
import org.springframework.security.authentication.TestingAuthenticationToken;
2727
import org.springframework.security.core.authority.AuthorityUtils;
@@ -82,7 +82,7 @@ public void expectedCredentialsAreReturned() {
8282
public void expectedPrincipalIsReturned() {
8383
LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
8484
user.setUsername("joe");
85-
user.setDn(new DistinguishedName("uid=joe,ou=users"));
85+
user.setDn(LdapNameBuilder.newInstance("uid=joe,ou=users").build());
8686
AuthenticationSource source = new SpringSecurityAuthenticationSource();
8787
SecurityContextHolder.getContext()
8888
.setAuthentication(new TestingAuthenticationToken(user.createUserDetails(), null));
@@ -93,7 +93,7 @@ public void expectedPrincipalIsReturned() {
9393
public void getPrincipalWhenCustomSecurityContextHolderStrategyThenExpectedPrincipalIsReturned() {
9494
LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
9595
user.setUsername("joe");
96-
user.setDn(new DistinguishedName("uid=joe,ou=users"));
96+
user.setDn(LdapNameBuilder.newInstance("uid=joe,ou=users").build());
9797
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
9898
given(strategy.getContext())
9999
.willReturn(new SecurityContextImpl(new TestingAuthenticationToken(user.createUserDetails(), null)));

ldap/src/test/java/org/springframework/security/ldap/authentication/LdapAuthenticationProviderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.ldap.CommunicationException;
2424
import org.springframework.ldap.core.DirContextAdapter;
2525
import org.springframework.ldap.core.DirContextOperations;
26-
import org.springframework.ldap.core.DistinguishedName;
26+
import org.springframework.ldap.support.LdapNameBuilder;
2727
import org.springframework.security.authentication.BadCredentialsException;
2828
import org.springframework.security.authentication.InternalAuthenticationServiceException;
2929
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -165,12 +165,12 @@ public DirContextOperations authenticate(Authentication authentication) {
165165
String username = authentication.getName();
166166
String password = (String) authentication.getCredentials();
167167
if (username.equals("ben") && password.equals("benspassword")) {
168-
ctx.setDn(new DistinguishedName("cn=ben,ou=people,dc=springframework,dc=org"));
168+
ctx.setDn(LdapNameBuilder.newInstance("cn=jen,ou=people,dc=springframework,dc=org").build());
169169
ctx.setAttributeValue("userPassword", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
170170
return ctx;
171171
}
172172
else if (username.equals("jen") && password.equals("")) {
173-
ctx.setDn(new DistinguishedName("cn=jen,ou=people,dc=springframework,dc=org"));
173+
ctx.setDn(LdapNameBuilder.newInstance("cn=jen,ou=people,dc=springframework,dc=org").build());
174174
return ctx;
175175
}
176176
throw new BadCredentialsException("Authentication failed.");

ldap/src/test/java/org/springframework/security/ldap/jackson2/InetOrgPersonMixinTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import org.skyscreamer.jsonassert.JSONAssert;
2525

2626
import org.springframework.ldap.core.DirContextAdapter;
27-
import org.springframework.ldap.core.DistinguishedName;
27+
import org.springframework.ldap.support.LdapNameBuilder;
2828
import org.springframework.security.core.authority.AuthorityUtils;
2929
import org.springframework.security.jackson2.SecurityJackson2Modules;
3030
import org.springframework.security.ldap.userdetails.InetOrgPerson;
@@ -165,7 +165,7 @@ public void deserializeWhenMixinRegisteredThenDeserializes() throws Exception {
165165

166166
private DirContextAdapter createUserContext() {
167167
DirContextAdapter ctx = new DirContextAdapter();
168-
ctx.setDn(new DistinguishedName("ignored=ignored"));
168+
ctx.setDn(LdapNameBuilder.newInstance("ignored=ignored").build());
169169
ctx.setAttributeValue("uid", "ghengis");
170170
ctx.setAttributeValue("userPassword", USER_PASSWORD);
171171
ctx.setAttributeValue("carLicense", "HORS1");

ldap/src/test/java/org/springframework/security/ldap/jackson2/LdapUserDetailsImplMixinTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import org.skyscreamer.jsonassert.JSONAssert;
2525

2626
import org.springframework.ldap.core.DirContextAdapter;
27-
import org.springframework.ldap.core.DistinguishedName;
27+
import org.springframework.ldap.support.LdapNameBuilder;
2828
import org.springframework.security.core.authority.AuthorityUtils;
2929
import org.springframework.security.jackson2.SecurityJackson2Modules;
3030
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
@@ -118,7 +118,7 @@ public void deserializeWhenMixinRegisteredThenDeserializes() throws Exception {
118118

119119
private DirContextAdapter createUserContext() {
120120
DirContextAdapter ctx = new DirContextAdapter();
121-
ctx.setDn(new DistinguishedName("ignored=ignored"));
121+
ctx.setDn(LdapNameBuilder.newInstance("ignored=ignored").build());
122122
ctx.setAttributeValue("userPassword", USER_PASSWORD);
123123
return ctx;
124124
}

ldap/src/test/java/org/springframework/security/ldap/jackson2/PersonMixinTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import org.skyscreamer.jsonassert.JSONAssert;
2525

2626
import org.springframework.ldap.core.DirContextAdapter;
27-
import org.springframework.ldap.core.DistinguishedName;
27+
import org.springframework.ldap.support.LdapNameBuilder;
2828
import org.springframework.security.core.authority.AuthorityUtils;
2929
import org.springframework.security.jackson2.SecurityJackson2Modules;
3030
import org.springframework.security.ldap.userdetails.Person;
@@ -125,7 +125,7 @@ public void deserializeWhenMixinRegisteredThenDeserializes() throws Exception {
125125

126126
private DirContextAdapter createUserContext() {
127127
DirContextAdapter ctx = new DirContextAdapter();
128-
ctx.setDn(new DistinguishedName("ignored=ignored"));
128+
ctx.setDn(LdapNameBuilder.newInstance("ignored=ignored").build());
129129
ctx.setAttributeValue("userPassword", USER_PASSWORD);
130130
ctx.setAttributeValue("cn", "Ghengis Khan");
131131
ctx.setAttributeValue("description", "Scary");

ldap/src/test/java/org/springframework/security/ldap/userdetails/InetOrgPersonTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.ldap.core.DirContextAdapter;
25-
import org.springframework.ldap.core.DistinguishedName;
25+
import org.springframework.ldap.support.LdapNameBuilder;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828

@@ -95,7 +95,7 @@ public void mappingBackToContextMatchesOriginalData() {
9595
DirContextAdapter ctx2 = new DirContextAdapter();
9696
ctx1.setAttributeValues("objectclass",
9797
new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
98-
ctx2.setDn(new DistinguishedName("ignored=ignored"));
98+
ctx2.setDn(LdapNameBuilder.newInstance("ignored=ignored").build());
9999
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
100100
p.populateContext(ctx2);
101101
assertThat(ctx2).isEqualTo(ctx1);
@@ -105,7 +105,7 @@ public void mappingBackToContextMatchesOriginalData() {
105105
public void copyMatchesOriginalData() {
106106
DirContextAdapter ctx1 = createUserContext();
107107
DirContextAdapter ctx2 = new DirContextAdapter();
108-
ctx2.setDn(new DistinguishedName("ignored=ignored"));
108+
ctx2.setDn(LdapNameBuilder.newInstance("ignored=ignored").build());
109109
ctx1.setAttributeValues("objectclass",
110110
new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
111111
InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)).createUserDetails();
@@ -116,7 +116,7 @@ public void copyMatchesOriginalData() {
116116

117117
private DirContextAdapter createUserContext() {
118118
DirContextAdapter ctx = new DirContextAdapter();
119-
ctx.setDn(new DistinguishedName("ignored=ignored"));
119+
ctx.setDn(LdapNameBuilder.newInstance("ignored=ignored").build());
120120
ctx.setAttributeValue("uid", "ghengis");
121121
ctx.setAttributeValue("userPassword", "pillage");
122122
ctx.setAttributeValue("carLicense", "HORS1");

ldap/src/test/java/org/springframework/security/ldap/userdetails/LdapUserDetailsMapperTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.ldap.core.DirContextAdapter;
25-
import org.springframework.ldap.core.DistinguishedName;
25+
import org.springframework.ldap.support.LdapNameBuilder;
2626
import org.springframework.security.core.authority.AuthorityUtils;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
@@ -58,7 +58,7 @@ public void testNonRetrievedRoleAttributeIsIgnored() {
5858
mapper.setRoleAttributes(new String[] { "userRole", "nonRetrievedAttribute" });
5959
BasicAttributes attrs = new BasicAttributes();
6060
attrs.put(new BasicAttribute("userRole", "x"));
61-
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
61+
DirContextAdapter ctx = new DirContextAdapter(attrs, LdapNameBuilder.newInstance("cn=someName").build());
6262
ctx.setAttributeValue("uid", "ani");
6363
LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
6464
AuthorityUtils.NO_AUTHORITIES);
@@ -72,7 +72,7 @@ public void testPasswordAttributeIsMappedCorrectly() {
7272
mapper.setPasswordAttributeName("myappsPassword");
7373
BasicAttributes attrs = new BasicAttributes();
7474
attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
75-
DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
75+
DirContextAdapter ctx = new DirContextAdapter(attrs, LdapNameBuilder.newInstance("cn=someName").build());
7676
ctx.setAttributeValue("uid", "ani");
7777
LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani",
7878
AuthorityUtils.NO_AUTHORITIES);

ldap/src/test/java/org/springframework/security/ldap/userdetails/LdapUserDetailsServiceTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323

2424
import org.springframework.ldap.core.DirContextAdapter;
2525
import org.springframework.ldap.core.DirContextOperations;
26-
import org.springframework.ldap.core.DistinguishedName;
26+
import org.springframework.ldap.support.LdapNameBuilder;
2727
import org.springframework.security.core.GrantedAuthority;
2828
import org.springframework.security.core.authority.AuthorityUtils;
2929
import org.springframework.security.core.userdetails.UserDetails;
@@ -53,7 +53,7 @@ public void rejectsNullAuthoritiesPopulator() {
5353

5454
@Test
5555
public void correctAuthoritiesAreReturned() {
56-
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
56+
DirContextAdapter userData = new DirContextAdapter(LdapNameBuilder.newInstance("uid=joe").build());
5757
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData),
5858
new MockAuthoritiesPopulator());
5959
service.setUserDetailsMapper(new LdapUserDetailsMapper());
@@ -65,7 +65,7 @@ public void correctAuthoritiesAreReturned() {
6565

6666
@Test
6767
public void nullPopulatorConstructorReturnsEmptyAuthoritiesList() {
68-
DirContextAdapter userData = new DirContextAdapter(new DistinguishedName("uid=joe"));
68+
DirContextAdapter userData = new DirContextAdapter(LdapNameBuilder.newInstance("uid=joe").build());
6969
LdapUserDetailsService service = new LdapUserDetailsService(new MockUserSearch(userData));
7070
UserDetails user = service.loadUserByUsername("doesntmatterwegetjoeanyway");
7171
assertThat(user.getAuthorities()).isEmpty();

0 commit comments

Comments
 (0)