Always use 'this.' when accessing fields
Apply an Eclipse cleanup rules to ensure that fields are always accessed using `this.`. This aligns with the style used by Spring Framework and helps users quickly see the difference between a local and member variable. Issue gh-8945
This commit is contained in:
@@ -55,27 +55,27 @@ public class SpringSecurityLdapTemplateITests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
template = new SpringSecurityLdapTemplate(this.contextSource);
|
||||
this.template = new SpringSecurityLdapTemplate(this.contextSource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOfCorrectValueSucceeds() {
|
||||
assertThat(template.compare("uid=bob,ou=people", "uid", "bob")).isTrue();
|
||||
assertThat(this.template.compare("uid=bob,ou=people", "uid", "bob")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOfCorrectByteValueSucceeds() {
|
||||
assertThat(template.compare("uid=bob,ou=people", "userPassword", Utf8.encode("bobspassword"))).isTrue();
|
||||
assertThat(this.template.compare("uid=bob,ou=people", "userPassword", Utf8.encode("bobspassword"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOfWrongByteValueFails() {
|
||||
assertThat(template.compare("uid=bob,ou=people", "userPassword", Utf8.encode("wrongvalue"))).isFalse();
|
||||
assertThat(this.template.compare("uid=bob,ou=people", "userPassword", Utf8.encode("wrongvalue"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareOfWrongValueFails() {
|
||||
assertThat(template.compare("uid=bob,ou=people", "uid", "wrongvalue")).isFalse();
|
||||
assertThat(this.template.compare("uid=bob,ou=people", "uid", "wrongvalue")).isFalse();
|
||||
}
|
||||
|
||||
// @Test
|
||||
@@ -91,7 +91,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
@Test
|
||||
public void namingExceptionIsTranslatedCorrectly() {
|
||||
try {
|
||||
template.executeReadOnly((ContextExecutor) dirContext -> {
|
||||
this.template.executeReadOnly((ContextExecutor) dirContext -> {
|
||||
throw new NamingException();
|
||||
});
|
||||
fail("Expected UncategorizedLdapException on NamingException");
|
||||
@@ -104,7 +104,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
public void roleSearchReturnsCorrectNumberOfRoles() {
|
||||
String param = "uid=ben,ou=people,dc=springframework,dc=org";
|
||||
|
||||
Set<String> values = template.searchForSingleAttributeValues("ou=groups", "(member={0})",
|
||||
Set<String> values = this.template.searchForSingleAttributeValues("ou=groups", "(member={0})",
|
||||
new String[] { param }, "ou");
|
||||
|
||||
assertThat(values).as("Expected 3 results from search").hasSize(3);
|
||||
@@ -115,7 +115,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
|
||||
@Test
|
||||
public void testMultiAttributeRetrievalWithNullAttributeNames() {
|
||||
Set<Map<String, List<String>>> values = template.searchForMultipleAttributeValues("ou=people", "(uid={0})",
|
||||
Set<Map<String, List<String>>> values = this.template.searchForMultipleAttributeValues("ou=people", "(uid={0})",
|
||||
new String[] { "bob" }, null);
|
||||
assertThat(values).hasSize(1);
|
||||
Map<String, List<String>> record = values.iterator().next();
|
||||
@@ -128,7 +128,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
|
||||
@Test
|
||||
public void testMultiAttributeRetrievalWithZeroLengthAttributeNames() {
|
||||
Set<Map<String, List<String>>> values = template.searchForMultipleAttributeValues("ou=people", "(uid={0})",
|
||||
Set<Map<String, List<String>>> values = this.template.searchForMultipleAttributeValues("ou=people", "(uid={0})",
|
||||
new String[] { "bob" }, new String[0]);
|
||||
assertThat(values).hasSize(1);
|
||||
Map<String, List<String>> record = values.iterator().next();
|
||||
@@ -141,7 +141,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
|
||||
@Test
|
||||
public void testMultiAttributeRetrievalWithSpecifiedAttributeNames() {
|
||||
Set<Map<String, List<String>>> values = template.searchForMultipleAttributeValues("ou=people", "(uid={0})",
|
||||
Set<Map<String, List<String>>> values = this.template.searchForMultipleAttributeValues("ou=people", "(uid={0})",
|
||||
new String[] { "bob" }, new String[] { "uid", "cn", "sn" });
|
||||
assertThat(values).hasSize(1);
|
||||
Map<String, List<String>> record = values.iterator().next();
|
||||
@@ -164,7 +164,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
public void testRoleSearchForMissingAttributeFailsGracefully() {
|
||||
String param = "uid=ben,ou=people,dc=springframework,dc=org";
|
||||
|
||||
Set<String> values = template.searchForSingleAttributeValues("ou=groups", "(member={0})",
|
||||
Set<String> values = this.template.searchForSingleAttributeValues("ou=groups", "(member={0})",
|
||||
new String[] { param }, "mail");
|
||||
|
||||
assertThat(values).isEmpty();
|
||||
@@ -174,7 +174,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
public void roleSearchWithEscapedCharacterSucceeds() {
|
||||
String param = "cn=mouse\\, jerry,ou=people,dc=springframework,dc=org";
|
||||
|
||||
Set<String> values = template.searchForSingleAttributeValues("ou=groups", "(member={0})",
|
||||
Set<String> values = this.template.searchForSingleAttributeValues("ou=groups", "(member={0})",
|
||||
new String[] { param }, "cn");
|
||||
|
||||
assertThat(values).hasSize(1);
|
||||
@@ -205,7 +205,7 @@ public class SpringSecurityLdapTemplateITests {
|
||||
public void searchForSingleEntryWithEscapedCharsInDnSucceeds() {
|
||||
String param = "mouse, jerry";
|
||||
|
||||
template.searchForSingleEntry("ou=people", "(cn={0})", new String[] { param });
|
||||
this.template.searchForSingleEntry("ou=people", "(cn={0})", new String[] { param });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -59,29 +59,30 @@ public class PasswordComparisonAuthenticatorTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
authenticator = new PasswordComparisonAuthenticator(this.contextSource);
|
||||
authenticator.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
|
||||
authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
|
||||
bob = new UsernamePasswordAuthenticationToken("bob", "bobspassword");
|
||||
ben = new UsernamePasswordAuthenticationToken("ben", "benspassword");
|
||||
this.authenticator = new PasswordComparisonAuthenticator(this.contextSource);
|
||||
this.authenticator.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
|
||||
this.authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
|
||||
this.bob = new UsernamePasswordAuthenticationToken("bob", "bobspassword");
|
||||
this.ben = new UsernamePasswordAuthenticationToken("ben", "benspassword");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllAttributesAreRetrievedByDefault() {
|
||||
DirContextAdapter user = (DirContextAdapter) authenticator.authenticate(bob);
|
||||
DirContextAdapter user = (DirContextAdapter) this.authenticator.authenticate(this.bob);
|
||||
// System.out.println(user.getAttributes().toString());
|
||||
assertThat(user.getAttributes().size()).withFailMessage("User should have 5 attributes").isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailedSearchGivesUserNotFoundException() throws Exception {
|
||||
authenticator = new PasswordComparisonAuthenticator(this.contextSource);
|
||||
assertThat(authenticator.getUserDns("Bob")).withFailMessage("User DN matches shouldn't be available").isEmpty();
|
||||
authenticator.setUserSearch(new MockUserSearch(null));
|
||||
authenticator.afterPropertiesSet();
|
||||
this.authenticator = new PasswordComparisonAuthenticator(this.contextSource);
|
||||
assertThat(this.authenticator.getUserDns("Bob")).withFailMessage("User DN matches shouldn't be available")
|
||||
.isEmpty();
|
||||
this.authenticator.setUserSearch(new MockUserSearch(null));
|
||||
this.authenticator.afterPropertiesSet();
|
||||
|
||||
try {
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("Joe", "pass"));
|
||||
this.authenticator.authenticate(new UsernamePasswordAuthenticationToken("Joe", "pass"));
|
||||
fail("Expected exception on failed user search");
|
||||
}
|
||||
catch (UsernameNotFoundException expected) {
|
||||
@@ -91,69 +92,70 @@ public class PasswordComparisonAuthenticatorTests {
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void testLdapPasswordCompareFailsWithWrongPassword() {
|
||||
// Don't retrieve the password
|
||||
authenticator.setUserAttributes(new String[] { "uid", "cn", "sn" });
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "wrongpass"));
|
||||
this.authenticator.setUserAttributes(new String[] { "uid", "cn", "sn" });
|
||||
this.authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "wrongpass"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleDnPatternsWorkOk() {
|
||||
authenticator.setUserDnPatterns(new String[] { "uid={0},ou=nonexistent", "uid={0},ou=people" });
|
||||
authenticator.authenticate(bob);
|
||||
this.authenticator.setUserDnPatterns(new String[] { "uid={0},ou=nonexistent", "uid={0},ou=people" });
|
||||
this.authenticator.authenticate(this.bob);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnlySpecifiedAttributesAreRetrieved() {
|
||||
authenticator.setUserAttributes(new String[] { "uid", "userPassword" });
|
||||
this.authenticator.setUserAttributes(new String[] { "uid", "userPassword" });
|
||||
|
||||
DirContextAdapter user = (DirContextAdapter) authenticator.authenticate(bob);
|
||||
DirContextAdapter user = (DirContextAdapter) this.authenticator.authenticate(this.bob);
|
||||
assertThat(user.getAttributes().size()).withFailMessage("Should have retrieved 2 attribute (uid)").isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLdapCompareSucceedsWithCorrectPassword() {
|
||||
// Don't retrieve the password
|
||||
authenticator.setUserAttributes(new String[] { "uid" });
|
||||
authenticator.authenticate(bob);
|
||||
this.authenticator.setUserAttributes(new String[] { "uid" });
|
||||
this.authenticator.authenticate(this.bob);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLdapCompareSucceedsWithShaEncodedPassword() {
|
||||
// Don't retrieve the password
|
||||
authenticator.setUserAttributes(new String[] { "uid" });
|
||||
authenticator.setPasswordEncoder(new LdapShaPasswordEncoder(KeyGenerators.shared(0)));
|
||||
authenticator.setUsePasswordAttrCompare(false);
|
||||
authenticator.authenticate(ben);
|
||||
this.authenticator.setUserAttributes(new String[] { "uid" });
|
||||
this.authenticator.setPasswordEncoder(new LdapShaPasswordEncoder(KeyGenerators.shared(0)));
|
||||
this.authenticator.setUsePasswordAttrCompare(false);
|
||||
this.authenticator.authenticate(this.ben);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testPasswordEncoderCantBeNull() {
|
||||
authenticator.setPasswordEncoder(null);
|
||||
this.authenticator.setPasswordEncoder(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseOfDifferentPasswordAttributeSucceeds() {
|
||||
authenticator.setPasswordAttributeName("uid");
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "bob"));
|
||||
this.authenticator.setPasswordAttributeName("uid");
|
||||
this.authenticator.authenticate(new UsernamePasswordAuthenticationToken("bob", "bob"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLdapCompareWithDifferentPasswordAttributeSucceeds() {
|
||||
authenticator.setUserAttributes(new String[] { "uid" });
|
||||
authenticator.setPasswordAttributeName("cn");
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("ben", "Ben Alex"));
|
||||
this.authenticator.setUserAttributes(new String[] { "uid" });
|
||||
this.authenticator.setPasswordAttributeName("cn");
|
||||
this.authenticator.authenticate(new UsernamePasswordAuthenticationToken("ben", "Ben Alex"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithUserSearch() {
|
||||
authenticator = new PasswordComparisonAuthenticator(this.contextSource);
|
||||
authenticator.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
|
||||
assertThat(authenticator.getUserDns("Bob")).withFailMessage("User DN matches shouldn't be available").isEmpty();
|
||||
this.authenticator = new PasswordComparisonAuthenticator(this.contextSource);
|
||||
this.authenticator.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
|
||||
assertThat(this.authenticator.getUserDns("Bob")).withFailMessage("User DN matches shouldn't be available")
|
||||
.isEmpty();
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("uid=Bob,ou=people"));
|
||||
ctx.setAttributeValue("userPassword", "bobspassword");
|
||||
|
||||
authenticator.setUserSearch(new MockUserSearch(ctx));
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("shouldntbeused", "bobspassword"));
|
||||
this.authenticator.setUserSearch(new MockUserSearch(ctx));
|
||||
this.authenticator.authenticate(new UsernamePasswordAuthenticationToken("shouldntbeused", "bobspassword"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ public class ApacheDSContainerTests {
|
||||
public void startWithLdapOverSslWithWrongPassword() throws Exception {
|
||||
final ClassPathResource keyStoreResource = new ClassPathResource(
|
||||
"/org/springframework/security/ldap/server/spring.keystore");
|
||||
final File temporaryKeyStoreFile = new File(temporaryFolder.getRoot(), "spring.keystore");
|
||||
final File temporaryKeyStoreFile = new File(this.temporaryFolder.getRoot(), "spring.keystore");
|
||||
FileCopyUtils.copy(keyStoreResource.getInputStream(), new FileOutputStream(temporaryKeyStoreFile));
|
||||
|
||||
assertThat(temporaryKeyStoreFile).isFile();
|
||||
@@ -166,7 +166,7 @@ public class ApacheDSContainerTests {
|
||||
|
||||
final ClassPathResource keyStoreResource = new ClassPathResource(
|
||||
"/org/springframework/security/ldap/server/spring.keystore");
|
||||
final File temporaryKeyStoreFile = new File(temporaryFolder.getRoot(), "spring.keystore");
|
||||
final File temporaryKeyStoreFile = new File(this.temporaryFolder.getRoot(), "spring.keystore");
|
||||
FileCopyUtils.copy(keyStoreResource.getInputStream(), new FileOutputStream(temporaryKeyStoreFile));
|
||||
|
||||
assertThat(temporaryKeyStoreFile).isFile();
|
||||
|
||||
@@ -41,17 +41,17 @@ public class UnboundIdContainerLdifTests {
|
||||
|
||||
@After
|
||||
public void closeAppContext() {
|
||||
if (appCtx != null) {
|
||||
appCtx.close();
|
||||
appCtx = null;
|
||||
if (this.appCtx != null) {
|
||||
this.appCtx.close();
|
||||
this.appCtx = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unboundIdContainerWhenCustomLdifNameThenLdifLoaded() {
|
||||
appCtx = new AnnotationConfigApplicationContext(CustomLdifConfig.class);
|
||||
this.appCtx = new AnnotationConfigApplicationContext(CustomLdifConfig.class);
|
||||
|
||||
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) appCtx
|
||||
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) this.appCtx
|
||||
.getBean(ContextSource.class);
|
||||
|
||||
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);
|
||||
@@ -85,9 +85,9 @@ public class UnboundIdContainerLdifTests {
|
||||
|
||||
@Test
|
||||
public void unboundIdContainerWhenWildcardLdifNameThenLdifLoaded() {
|
||||
appCtx = new AnnotationConfigApplicationContext(WildcardLdifConfig.class);
|
||||
this.appCtx = new AnnotationConfigApplicationContext(WildcardLdifConfig.class);
|
||||
|
||||
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) appCtx
|
||||
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) this.appCtx
|
||||
.getBean(ContextSource.class);
|
||||
|
||||
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);
|
||||
@@ -122,7 +122,7 @@ public class UnboundIdContainerLdifTests {
|
||||
@Test
|
||||
public void unboundIdContainerWhenMalformedLdifThenException() {
|
||||
try {
|
||||
appCtx = new AnnotationConfigApplicationContext(MalformedLdifConfig.class);
|
||||
this.appCtx = new AnnotationConfigApplicationContext(MalformedLdifConfig.class);
|
||||
failBecauseExceptionWasNotThrown(IllegalStateException.class);
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -153,7 +153,7 @@ public class UnboundIdContainerLdifTests {
|
||||
@Test
|
||||
public void unboundIdContainerWhenMissingLdifThenException() {
|
||||
try {
|
||||
appCtx = new AnnotationConfigApplicationContext(MissingLdifConfig.class);
|
||||
this.appCtx = new AnnotationConfigApplicationContext(MissingLdifConfig.class);
|
||||
failBecauseExceptionWasNotThrown(IllegalStateException.class);
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -54,28 +54,28 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
populator = new DefaultLdapAuthoritiesPopulator(this.contextSource, "ou=groups");
|
||||
populator.setIgnorePartialResultException(false);
|
||||
this.populator = new DefaultLdapAuthoritiesPopulator(this.contextSource, "ou=groups");
|
||||
this.populator.setIgnorePartialResultException(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultRoleIsAssignedWhenSet() {
|
||||
populator.setDefaultRole("ROLE_USER");
|
||||
assertThat(populator.getContextSource()).isSameAs(this.contextSource);
|
||||
this.populator.setDefaultRole("ROLE_USER");
|
||||
assertThat(this.populator.getContextSource()).isSameAs(this.contextSource);
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName("cn=notfound"));
|
||||
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "notfound");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "notfound");
|
||||
assertThat(authorities).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_USER")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullSearchBaseIsAccepted() {
|
||||
populator = new DefaultLdapAuthoritiesPopulator(this.contextSource, null);
|
||||
populator.setDefaultRole("ROLE_USER");
|
||||
this.populator = new DefaultLdapAuthoritiesPopulator(this.contextSource, null);
|
||||
this.populator.setDefaultRole("ROLE_USER");
|
||||
|
||||
Collection<GrantedAuthority> authorities = populator
|
||||
Collection<GrantedAuthority> authorities = this.populator
|
||||
.getGrantedAuthorities(new DirContextAdapter(new DistinguishedName("cn=notused")), "notused");
|
||||
assertThat(authorities).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_USER")).isTrue();
|
||||
@@ -83,17 +83,17 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void groupSearchReturnsExpectedRoles() {
|
||||
populator.setRolePrefix("ROLE_");
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setSearchSubtree(true);
|
||||
populator.setSearchSubtree(false);
|
||||
populator.setConvertToUpperCase(true);
|
||||
populator.setGroupSearchFilter("(member={0})");
|
||||
this.populator.setRolePrefix("ROLE_");
|
||||
this.populator.setGroupRoleAttribute("ou");
|
||||
this.populator.setSearchSubtree(true);
|
||||
this.populator.setSearchSubtree(false);
|
||||
this.populator.setConvertToUpperCase(true);
|
||||
this.populator.setGroupSearchFilter("(member={0})");
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(
|
||||
new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "ben"));
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(this.populator.getGrantedAuthorities(ctx, "ben"));
|
||||
|
||||
assertThat(authorities).as("Should have 2 roles").hasSize(2);
|
||||
|
||||
@@ -103,14 +103,15 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void useOfUsernameParameterReturnsExpectedRoles() {
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setConvertToUpperCase(true);
|
||||
populator.setGroupSearchFilter("(ou={1})");
|
||||
this.populator.setGroupRoleAttribute("ou");
|
||||
this.populator.setConvertToUpperCase(true);
|
||||
this.populator.setGroupSearchFilter("(ou={1})");
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(
|
||||
new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "manager"));
|
||||
Set<String> authorities = AuthorityUtils
|
||||
.authorityListToSet(this.populator.getGrantedAuthorities(ctx, "manager"));
|
||||
|
||||
assertThat(authorities).as("Should have 1 role").hasSize(1);
|
||||
assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
|
||||
@@ -118,13 +119,14 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void subGroupRolesAreNotFoundByDefault() {
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setConvertToUpperCase(true);
|
||||
this.populator.setGroupRoleAttribute("ou");
|
||||
this.populator.setConvertToUpperCase(true);
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(
|
||||
new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "manager"));
|
||||
Set<String> authorities = AuthorityUtils
|
||||
.authorityListToSet(this.populator.getGrantedAuthorities(ctx, "manager"));
|
||||
|
||||
assertThat(authorities).as("Should have 2 roles").hasSize(2);
|
||||
assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
|
||||
@@ -133,14 +135,15 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void subGroupRolesAreFoundWhenSubtreeSearchIsEnabled() {
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setConvertToUpperCase(true);
|
||||
populator.setSearchSubtree(true);
|
||||
this.populator.setGroupRoleAttribute("ou");
|
||||
this.populator.setConvertToUpperCase(true);
|
||||
this.populator.setSearchSubtree(true);
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(
|
||||
new DistinguishedName("uid=ben,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "manager"));
|
||||
Set<String> authorities = AuthorityUtils
|
||||
.authorityListToSet(this.populator.getGrantedAuthorities(ctx, "manager"));
|
||||
|
||||
assertThat(authorities).as("Should have 3 roles").hasSize(3);
|
||||
assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
|
||||
@@ -150,14 +153,14 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void extraRolesAreAdded() {
|
||||
populator = new DefaultLdapAuthoritiesPopulator(this.contextSource, null) {
|
||||
this.populator = new DefaultLdapAuthoritiesPopulator(this.contextSource, null) {
|
||||
@Override
|
||||
protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, String username) {
|
||||
return new HashSet<>(AuthorityUtils.createAuthorityList("ROLE_EXTRA"));
|
||||
}
|
||||
};
|
||||
|
||||
Collection<GrantedAuthority> authorities = populator
|
||||
Collection<GrantedAuthority> authorities = this.populator
|
||||
.getGrantedAuthorities(new DirContextAdapter(new DistinguishedName("cn=notused")), "notused");
|
||||
assertThat(authorities).hasSize(1);
|
||||
assertThat(AuthorityUtils.authorityListToSet(authorities).contains("ROLE_EXTRA")).isTrue();
|
||||
@@ -165,14 +168,15 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void userDnWithEscapedCharacterParameterReturnsExpectedRoles() {
|
||||
populator.setGroupRoleAttribute("ou");
|
||||
populator.setConvertToUpperCase(true);
|
||||
populator.setGroupSearchFilter("(member={0})");
|
||||
this.populator.setGroupRoleAttribute("ou");
|
||||
this.populator.setConvertToUpperCase(true);
|
||||
this.populator.setGroupSearchFilter("(member={0})");
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(
|
||||
new DistinguishedName("cn=mouse\\, jerry,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
Set<String> authorities = AuthorityUtils.authorityListToSet(populator.getGrantedAuthorities(ctx, "notused"));
|
||||
Set<String> authorities = AuthorityUtils
|
||||
.authorityListToSet(this.populator.getGrantedAuthorities(ctx, "notused"));
|
||||
|
||||
assertThat(authorities).as("Should have 1 role").hasSize(1);
|
||||
assertThat(authorities.contains("ROLE_MANAGER")).isTrue();
|
||||
@@ -180,23 +184,23 @@ public class DefaultLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Test
|
||||
public void customAuthoritiesMappingFunction() {
|
||||
populator.setAuthorityMapper(record -> {
|
||||
this.populator.setAuthorityMapper(record -> {
|
||||
String dn = record.get(SpringSecurityLdapTemplate.DN_KEY).get(0);
|
||||
String role = record.get(populator.getGroupRoleAttribute()).get(0);
|
||||
String role = record.get(this.populator.getGroupRoleAttribute()).get(0);
|
||||
return new LdapAuthority(role, dn);
|
||||
});
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter(
|
||||
new DistinguishedName("cn=mouse\\, jerry,ou=people,dc=springframework,dc=org"));
|
||||
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "notused");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "notused");
|
||||
|
||||
assertThat(authorities).allMatch(LdapAuthority.class::isInstance);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void customAuthoritiesMappingFunctionThrowsIfNull() {
|
||||
populator.setAuthorityMapper(null);
|
||||
this.populator.setAuthorityMapper(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -61,32 +61,32 @@ public class LdapUserDetailsManagerTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mgr = new LdapUserDetailsManager(this.contextSource);
|
||||
template = new SpringSecurityLdapTemplate(this.contextSource);
|
||||
this.mgr = new LdapUserDetailsManager(this.contextSource);
|
||||
this.template = new SpringSecurityLdapTemplate(this.contextSource);
|
||||
DirContextAdapter ctx = new DirContextAdapter();
|
||||
|
||||
ctx.setAttributeValue("objectclass", "organizationalUnit");
|
||||
ctx.setAttributeValue("ou", "test people");
|
||||
template.bind("ou=test people", ctx, null);
|
||||
this.template.bind("ou=test people", ctx, null);
|
||||
|
||||
ctx.setAttributeValue("ou", "testgroups");
|
||||
template.bind("ou=testgroups", ctx, null);
|
||||
this.template.bind("ou=testgroups", ctx, null);
|
||||
|
||||
DirContextAdapter group = new DirContextAdapter();
|
||||
|
||||
group.setAttributeValue("objectclass", "groupOfNames");
|
||||
group.setAttributeValue("cn", "clowns");
|
||||
group.setAttributeValue("member", "cn=nobody,ou=test people,dc=springframework,dc=org");
|
||||
template.bind("cn=clowns,ou=testgroups", group, null);
|
||||
this.template.bind("cn=clowns,ou=testgroups", group, null);
|
||||
|
||||
group.setAttributeValue("cn", "acrobats");
|
||||
template.bind("cn=acrobats,ou=testgroups", group, null);
|
||||
this.template.bind("cn=acrobats,ou=testgroups", group, null);
|
||||
|
||||
mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=test people", "uid"));
|
||||
mgr.setGroupSearchBase("ou=testgroups");
|
||||
mgr.setGroupRoleAttributeName("cn");
|
||||
mgr.setGroupMemberAttributeName("member");
|
||||
mgr.setUserDetailsMapper(new PersonContextMapper());
|
||||
this.mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=test people", "uid"));
|
||||
this.mgr.setGroupSearchBase("ou=testgroups");
|
||||
this.mgr.setGroupRoleAttributeName("cn");
|
||||
this.mgr.setGroupMemberAttributeName("member");
|
||||
this.mgr.setUserDetailsMapper(new PersonContextMapper());
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -100,17 +100,17 @@ public class LdapUserDetailsManagerTests {
|
||||
// template.unbind((String) people.next() + ",ou=testpeople");
|
||||
// }
|
||||
|
||||
template.unbind("ou=test people", true);
|
||||
template.unbind("ou=testgroups", true);
|
||||
this.template.unbind("ou=test people", true);
|
||||
this.template.unbind("ou=testgroups", true);
|
||||
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadUserByUsernameReturnsCorrectData() {
|
||||
mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
|
||||
mgr.setGroupSearchBase("ou=groups");
|
||||
LdapUserDetails bob = (LdapUserDetails) mgr.loadUserByUsername("bob");
|
||||
this.mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
|
||||
this.mgr.setGroupSearchBase("ou=groups");
|
||||
LdapUserDetails bob = (LdapUserDetails) this.mgr.loadUserByUsername("bob");
|
||||
assertThat(bob.getUsername()).isEqualTo("bob");
|
||||
assertThat(bob.getDn()).isEqualTo("uid=bob,ou=people,dc=springframework,dc=org");
|
||||
assertThat(bob.getPassword()).isEqualTo("bobspassword");
|
||||
@@ -120,18 +120,18 @@ public class LdapUserDetailsManagerTests {
|
||||
|
||||
@Test(expected = UsernameNotFoundException.class)
|
||||
public void testLoadingInvalidUsernameThrowsUsernameNotFoundException() {
|
||||
mgr.loadUserByUsername("jim");
|
||||
this.mgr.loadUserByUsername("jim");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserExistsReturnsTrueForValidUser() {
|
||||
mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
|
||||
assertThat(mgr.userExists("bob")).isTrue();
|
||||
this.mgr.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
|
||||
assertThat(this.mgr.userExists("bob")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserExistsReturnsFalseForInValidUser() {
|
||||
assertThat(mgr.userExists("jim")).isFalse();
|
||||
assertThat(this.mgr.userExists("jim")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,7 +154,7 @@ public class LdapUserDetailsManagerTests {
|
||||
|
||||
p.setAuthorities(TEST_AUTHORITIES);
|
||||
|
||||
mgr.createUser(p.createUserDetails());
|
||||
this.mgr.createUser(p.createUserDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -166,17 +166,17 @@ public class LdapUserDetailsManagerTests {
|
||||
p.setUid("don");
|
||||
p.setAuthorities(TEST_AUTHORITIES);
|
||||
|
||||
mgr.createUser(p.createUserDetails());
|
||||
mgr.setUserDetailsMapper(new InetOrgPersonContextMapper());
|
||||
this.mgr.createUser(p.createUserDetails());
|
||||
this.mgr.setUserDetailsMapper(new InetOrgPersonContextMapper());
|
||||
|
||||
InetOrgPerson don = (InetOrgPerson) mgr.loadUserByUsername("don");
|
||||
InetOrgPerson don = (InetOrgPerson) this.mgr.loadUserByUsername("don");
|
||||
|
||||
assertThat(don.getAuthorities()).hasSize(2);
|
||||
|
||||
mgr.deleteUser("don");
|
||||
this.mgr.deleteUser("don");
|
||||
|
||||
try {
|
||||
mgr.loadUserByUsername("don");
|
||||
this.mgr.loadUserByUsername("don");
|
||||
fail("Expected UsernameNotFoundException after deleting user");
|
||||
}
|
||||
catch (UsernameNotFoundException expected) {
|
||||
@@ -184,7 +184,7 @@ public class LdapUserDetailsManagerTests {
|
||||
}
|
||||
|
||||
// Check that no authorities are left
|
||||
assertThat(mgr.getUserAuthorities(mgr.usernameMapper.buildDn("don"), "don")).hasSize(0);
|
||||
assertThat(this.mgr.getUserAuthorities(this.mgr.usernameMapper.buildDn("don"), "don")).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -197,14 +197,14 @@ public class LdapUserDetailsManagerTests {
|
||||
p.setPassword("yossarianspassword");
|
||||
p.setAuthorities(TEST_AUTHORITIES);
|
||||
|
||||
mgr.createUser(p.createUserDetails());
|
||||
this.mgr.createUser(p.createUserDetails());
|
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new UsernamePasswordAuthenticationToken("johnyossarian", "yossarianspassword", TEST_AUTHORITIES));
|
||||
|
||||
mgr.changePassword("yossarianspassword", "yossariansnewpassword");
|
||||
this.mgr.changePassword("yossarianspassword", "yossariansnewpassword");
|
||||
|
||||
assertThat(template.compare("uid=johnyossarian,ou=test people", "userPassword", "yossariansnewpassword"))
|
||||
assertThat(this.template.compare("uid=johnyossarian,ou=test people", "userPassword", "yossariansnewpassword"))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@@ -218,12 +218,12 @@ public class LdapUserDetailsManagerTests {
|
||||
p.setPassword("yossarianspassword");
|
||||
p.setAuthorities(TEST_AUTHORITIES);
|
||||
|
||||
mgr.createUser(p.createUserDetails());
|
||||
this.mgr.createUser(p.createUserDetails());
|
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(
|
||||
new UsernamePasswordAuthenticationToken("johnyossarian", "yossarianspassword", TEST_AUTHORITIES));
|
||||
|
||||
mgr.changePassword("wrongpassword", "yossariansnewpassword");
|
||||
this.mgr.changePassword("wrongpassword", "yossariansnewpassword");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,69 +60,69 @@ public class NestedLdapAuthoritiesPopulatorTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
populator = new NestedLdapAuthoritiesPopulator(this.contextSource, "ou=jdeveloper");
|
||||
populator.setGroupSearchFilter("(member={0})");
|
||||
populator.setIgnorePartialResultException(false);
|
||||
populator.setRolePrefix("");
|
||||
populator.setSearchSubtree(true);
|
||||
populator.setConvertToUpperCase(false);
|
||||
jDevelopers = new LdapAuthority("j-developers", "cn=j-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
javaDevelopers = new LdapAuthority("java-developers",
|
||||
this.populator = new NestedLdapAuthoritiesPopulator(this.contextSource, "ou=jdeveloper");
|
||||
this.populator.setGroupSearchFilter("(member={0})");
|
||||
this.populator.setIgnorePartialResultException(false);
|
||||
this.populator.setRolePrefix("");
|
||||
this.populator.setSearchSubtree(true);
|
||||
this.populator.setConvertToUpperCase(false);
|
||||
this.jDevelopers = new LdapAuthority("j-developers", "cn=j-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
this.javaDevelopers = new LdapAuthority("java-developers",
|
||||
"cn=java-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
groovyDevelopers = new LdapAuthority("groovy-developers",
|
||||
this.groovyDevelopers = new LdapAuthority("groovy-developers",
|
||||
"cn=groovy-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
scalaDevelopers = new LdapAuthority("scala-developers",
|
||||
this.scalaDevelopers = new LdapAuthority("scala-developers",
|
||||
"cn=scala-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
closureDevelopers = new LdapAuthority("closure-developers",
|
||||
this.closureDevelopers = new LdapAuthority("closure-developers",
|
||||
"cn=closure-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
circularJavaDevelopers = new LdapAuthority("circular-java-developers",
|
||||
this.circularJavaDevelopers = new LdapAuthority("circular-java-developers",
|
||||
"cn=circular-java-developers,ou=jdeveloper,dc=springframework,dc=org");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScalaDudeJDevelopersAuthorities() {
|
||||
DirContextAdapter ctx = new DirContextAdapter("uid=scaladude,ou=people,dc=springframework,dc=org");
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "scaladude");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "scaladude");
|
||||
assertThat(authorities).hasSize(5);
|
||||
assertThat(authorities).isEqualTo(
|
||||
Arrays.asList(javaDevelopers, circularJavaDevelopers, scalaDevelopers, groovyDevelopers, jDevelopers));
|
||||
assertThat(authorities).isEqualTo(Arrays.asList(this.javaDevelopers, this.circularJavaDevelopers,
|
||||
this.scalaDevelopers, this.groovyDevelopers, this.jDevelopers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaDudeJDevelopersAuthorities() {
|
||||
DirContextAdapter ctx = new DirContextAdapter("uid=javadude,ou=people,dc=springframework,dc=org");
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "javadude");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "javadude");
|
||||
assertThat(authorities).hasSize(4);
|
||||
assertThat(authorities).contains(javaDevelopers);
|
||||
assertThat(authorities).contains(this.javaDevelopers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScalaDudeJDevelopersAuthoritiesWithSearchLimit() {
|
||||
populator.setMaxSearchDepth(1);
|
||||
this.populator.setMaxSearchDepth(1);
|
||||
DirContextAdapter ctx = new DirContextAdapter("uid=scaladude,ou=people,dc=springframework,dc=org");
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "scaladude");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "scaladude");
|
||||
assertThat(authorities).hasSize(1);
|
||||
assertThat(authorities).isEqualTo(Arrays.asList(scalaDevelopers));
|
||||
assertThat(authorities).isEqualTo(Arrays.asList(this.scalaDevelopers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroovyDudeJDevelopersAuthorities() {
|
||||
DirContextAdapter ctx = new DirContextAdapter("uid=groovydude,ou=people,dc=springframework,dc=org");
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "groovydude");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "groovydude");
|
||||
assertThat(authorities).hasSize(4);
|
||||
assertThat(authorities)
|
||||
.isEqualTo(Arrays.asList(javaDevelopers, circularJavaDevelopers, groovyDevelopers, jDevelopers));
|
||||
assertThat(authorities).isEqualTo(Arrays.asList(this.javaDevelopers, this.circularJavaDevelopers,
|
||||
this.groovyDevelopers, this.jDevelopers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClosureDudeJDevelopersWithMembershipAsAttributeValues() {
|
||||
populator.setAttributeNames(new HashSet(Arrays.asList("member")));
|
||||
this.populator.setAttributeNames(new HashSet(Arrays.asList("member")));
|
||||
|
||||
DirContextAdapter ctx = new DirContextAdapter("uid=closuredude,ou=people,dc=springframework,dc=org");
|
||||
Collection<GrantedAuthority> authorities = populator.getGrantedAuthorities(ctx, "closuredude");
|
||||
Collection<GrantedAuthority> authorities = this.populator.getGrantedAuthorities(ctx, "closuredude");
|
||||
assertThat(authorities).hasSize(5);
|
||||
assertThat(authorities).isEqualTo(Arrays.asList(javaDevelopers, circularJavaDevelopers, closureDevelopers,
|
||||
groovyDevelopers, jDevelopers));
|
||||
assertThat(authorities).isEqualTo(Arrays.asList(this.javaDevelopers, this.circularJavaDevelopers,
|
||||
this.closureDevelopers, this.groovyDevelopers, this.jDevelopers));
|
||||
|
||||
LdapAuthority[] ldapAuthorities = authorities.toArray(new LdapAuthority[0]);
|
||||
assertThat(ldapAuthorities).hasSize(5);
|
||||
@@ -137,7 +137,7 @@ public class NestedLdapAuthoritiesPopulatorTests {
|
||||
assertThat(ldapAuthorities[1].getAttributes().containsKey("member")).isTrue();
|
||||
assertThat(ldapAuthorities[1].getAttributes().get("member")).isNotNull();
|
||||
assertThat(ldapAuthorities[1].getAttributes().get("member")).hasSize(3);
|
||||
assertThat(groovyDevelopers.getDn()).isEqualTo(ldapAuthorities[1].getFirstAttributeValue("member"));
|
||||
assertThat(this.groovyDevelopers.getDn()).isEqualTo(ldapAuthorities[1].getFirstAttributeValue("member"));
|
||||
assertThat(ldapAuthorities[2].getAttributes().get("member"))
|
||||
.contains("uid=closuredude,ou=people,dc=springframework,dc=org");
|
||||
|
||||
@@ -146,7 +146,7 @@ public class NestedLdapAuthoritiesPopulatorTests {
|
||||
assertThat(ldapAuthorities[2].getAttributeValues("test")).isNotNull();
|
||||
assertThat(ldapAuthorities[2].getAttributeValues("test")).isEmpty();
|
||||
// test role name
|
||||
assertThat(ldapAuthorities[3].getAuthority()).isEqualTo(groovyDevelopers.getAuthority());
|
||||
assertThat(ldapAuthorities[3].getAuthority()).isEqualTo(this.groovyDevelopers.getAuthority());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user