Skip to content

Commit 6e82138

Browse files
committed
Use SecurityContextHolderStrategy for Ldap
Issue gh-11060
1 parent 652c35d commit 6e82138

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

ldap/src/integration-test/java/org/springframework/security/ldap/userdetails/LdapUserDetailsManagerTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.security.core.GrantedAuthority;
3232
import org.springframework.security.core.authority.AuthorityUtils;
3333
import org.springframework.security.core.context.SecurityContextHolder;
34+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
35+
import org.springframework.security.core.context.SecurityContextImpl;
3436
import org.springframework.security.core.userdetails.UsernameNotFoundException;
3537
import org.springframework.security.ldap.ApacheDsContainerConfig;
3638
import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
@@ -40,6 +42,9 @@
4042

4143
import static org.assertj.core.api.Assertions.assertThat;
4244
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
45+
import static org.mockito.BDDMockito.given;
46+
import static org.mockito.Mockito.mock;
47+
import static org.mockito.Mockito.verify;
4348

4449
/**
4550
* @author Luke Taylor
@@ -201,6 +206,30 @@ public void testPasswordChangeWithCorrectOldPasswordSucceeds() {
201206
.isTrue();
202207
}
203208

209+
@Test
210+
public void testPasswordChangeUsesCustomSecurityContextHolderStrategy() {
211+
InetOrgPerson.Essence p = new InetOrgPerson.Essence();
212+
p.setDn("whocares");
213+
p.setCn(new String[] { "John Yossarian" });
214+
p.setSn("Yossarian");
215+
p.setUid("johnyossarian");
216+
p.setPassword("yossarianspassword");
217+
p.setAuthorities(TEST_AUTHORITIES);
218+
219+
this.mgr.createUser(p.createUserDetails());
220+
221+
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
222+
given(strategy.getContext()).willReturn(new SecurityContextImpl(UsernamePasswordAuthenticationToken
223+
.authenticated("johnyossarian", "yossarianspassword", TEST_AUTHORITIES)));
224+
this.mgr.setSecurityContextHolderStrategy(strategy);
225+
226+
this.mgr.changePassword("yossarianspassword", "yossariansnewpassword");
227+
228+
assertThat(this.template.compare("uid=johnyossarian,ou=test people", "userPassword", "yossariansnewpassword"))
229+
.isTrue();
230+
verify(strategy).getContext();
231+
}
232+
204233
@Test
205234
public void testPasswordChangeWithWrongOldPasswordFails() {
206235
InetOrgPerson.Essence p = new InetOrgPerson.Essence();

ldap/src/main/java/org/springframework/security/ldap/authentication/SpringSecurityAuthenticationSource.java

Lines changed: 19 additions & 3 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-2022 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,9 @@
2323
import org.springframework.security.authentication.AnonymousAuthenticationToken;
2424
import org.springframework.security.core.Authentication;
2525
import org.springframework.security.core.context.SecurityContextHolder;
26+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
2627
import org.springframework.security.ldap.userdetails.LdapUserDetails;
28+
import org.springframework.util.Assert;
2729

2830
/**
2931
* An AuthenticationSource to retrieve authentication information stored in Spring
@@ -40,13 +42,16 @@ public class SpringSecurityAuthenticationSource implements AuthenticationSource
4042

4143
private static final Log log = LogFactory.getLog(SpringSecurityAuthenticationSource.class);
4244

45+
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
46+
.getContextHolderStrategy();
47+
4348
/**
4449
* Get the principals of the logged in user, in this case the distinguished name.
4550
* @return the distinguished name of the logged in user.
4651
*/
4752
@Override
4853
public String getPrincipal() {
49-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
54+
Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
5055
if (authentication == null) {
5156
log.debug("Returning empty String as Principal since authentication is null");
5257
return "";
@@ -69,12 +74,23 @@ public String getPrincipal() {
6974
*/
7075
@Override
7176
public String getCredentials() {
72-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
77+
Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
7378
if (authentication == null) {
7479
log.debug("Returning empty String as Credentials since authentication is null");
7580
return "";
7681
}
7782
return (String) authentication.getCredentials();
7883
}
7984

85+
/**
86+
* Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
87+
* the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
88+
*
89+
* @since 5.8
90+
*/
91+
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
92+
Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
93+
this.securityContextHolderStrategy = securityContextHolderStrategy;
94+
}
95+
8096
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -54,6 +54,7 @@
5454
import org.springframework.security.core.GrantedAuthority;
5555
import org.springframework.security.core.authority.SimpleGrantedAuthority;
5656
import org.springframework.security.core.context.SecurityContextHolder;
57+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
5758
import org.springframework.security.core.userdetails.UserDetails;
5859
import org.springframework.security.core.userdetails.UsernameNotFoundException;
5960
import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
@@ -82,6 +83,9 @@ public class LdapUserDetailsManager implements UserDetailsManager {
8283

8384
private final Log logger = LogFactory.getLog(LdapUserDetailsManager.class);
8485

86+
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
87+
.getContextHolderStrategy();
88+
8589
/**
8690
* The strategy for mapping usernames to LDAP distinguished names. This will be used
8791
* when building DNs for creating new users etc.
@@ -179,7 +183,7 @@ private DirContextAdapter loadUserAsContext(final DistinguishedName dn, final St
179183
*/
180184
@Override
181185
public void changePassword(final String oldPassword, final String newPassword) {
182-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
186+
Authentication authentication = this.securityContextHolderStrategy.getContext().getAuthentication();
183187
Assert.notNull(authentication,
184188
"No authentication object found in security context. Can't change current user's password!");
185189
String username = authentication.getName();
@@ -388,6 +392,17 @@ public void setUsePasswordModifyExtensionOperation(boolean usePasswordModifyExte
388392
this.usePasswordModifyExtensionOperation = usePasswordModifyExtensionOperation;
389393
}
390394

395+
/**
396+
* Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
397+
* the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
398+
*
399+
* @since 5.8
400+
*/
401+
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
402+
Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
403+
this.securityContextHolderStrategy = securityContextHolderStrategy;
404+
}
405+
391406
private void changePasswordUsingAttributeModification(DistinguishedName userDn, String oldPassword,
392407
String newPassword) {
393408
ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE,

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

Lines changed: 20 additions & 1 deletion
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-2022 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.
@@ -26,11 +26,16 @@
2626
import org.springframework.security.authentication.TestingAuthenticationToken;
2727
import org.springframework.security.core.authority.AuthorityUtils;
2828
import org.springframework.security.core.context.SecurityContextHolder;
29+
import org.springframework.security.core.context.SecurityContextHolderStrategy;
30+
import org.springframework.security.core.context.SecurityContextImpl;
2931
import org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource;
3032
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;
3133

3234
import static org.assertj.core.api.Assertions.assertThat;
3335
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
36+
import static org.mockito.BDDMockito.given;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.verify;
3439

3540
/**
3641
* @author Luke Taylor
@@ -83,4 +88,18 @@ public void expectedPrincipalIsReturned() {
8388
assertThat(source.getPrincipal()).isEqualTo("uid=joe,ou=users");
8489
}
8590

91+
@Test
92+
public void getPrincipalWhenCustomSecurityContextHolderStrategyThenExpectedPrincipalIsReturned() {
93+
LdapUserDetailsImpl.Essence user = new LdapUserDetailsImpl.Essence();
94+
user.setUsername("joe");
95+
user.setDn(new DistinguishedName("uid=joe,ou=users"));
96+
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
97+
given(strategy.getContext())
98+
.willReturn(new SecurityContextImpl(new TestingAuthenticationToken(user.createUserDetails(), null)));
99+
SpringSecurityAuthenticationSource source = new SpringSecurityAuthenticationSource();
100+
source.setSecurityContextHolderStrategy(strategy);
101+
assertThat(source.getPrincipal()).isEqualTo("uid=joe,ou=users");
102+
verify(strategy).getContext();
103+
}
104+
86105
}

0 commit comments

Comments
 (0)