/** * Creates the user authority list from the values of the {@code memberOf} attribute * obtained from the user's Active Directory entry. */ @Override protected Collection<? extends GrantedAuthority> loadUserAuthorities( DirContextOperations userData, String username, String password) { String[] groups = userData.getStringAttributes("memberOf"); if (groups == null) { logger.debug("No values for 'memberOf' attribute."); return AuthorityUtils.NO_AUTHORITIES; } if (logger.isDebugEnabled()) { logger.debug("'memberOf' attribute values: " + Arrays.asList(groups)); } ArrayList<GrantedAuthority> authorities = new ArrayList<>( groups.length); for (String group : groups) { authorities.add(new SimpleGrantedAuthority(new DistinguishedName(group) .removeLast().getValue())); } return authorities; }
/** * Get the value of the {@link LdapRdnComponent} with the specified key * (Attribute value). If there are several Rdns with the same key, the value * of the first one found (in order of significance) will be returned. * * @param key Attribute name of the {@link LdapRdn} to retrieve. * @return the value. * @throws IllegalArgumentException if no Rdn matches the given key. */ public String getValue(String key) { return getLdapRdn(key).getValue(); }
/** * Get the value of the {@link LdapRdnComponent} with the specified key * (Attribute value). If there are several Rdns with the same key, the value * of the first one found (in order of significance) will be returned. * * @param key Attribute name of the {@link LdapRdn} to retrieve. * @return the value. * @throws IllegalArgumentException if no Rdn matches the given key. */ public String getValue(String key) { return getLdapRdn(key).getValue(); }
/** * Get the value of the {@link LdapRdnComponent} with the specified key * (Attribute value). If there are several Rdns with the same key, the value * of the first one found (in order of significance) will be returned. * * @param key Attribute name of the {@link LdapRdn} to retrieve. * @return the value. * @throws IllegalArgumentException if no Rdn matches the given key. */ public String getValue(String key) { return getLdapRdn(key).getValue(); }
/** * Get the value of the {@link LdapRdnComponent} with the specified key * (Attribute value). If there are several Rdns with the same key, the value * of the first one found (in order of significance) will be returned. * * @param key Attribute name of the {@link LdapRdn} to retrieve. * @return the value. * @throws IllegalArgumentException if no Rdn matches the given key. */ public String getValue(String key) { return getLdapRdn(key).getValue(); }
@SuppressWarnings("unchecked") public List<Long> groups(String username, LdapConfig config, LdapOperations ldap, RoleProvider provider, final AttributeSet attrSet) { final List<Long> groups = new ArrayList<Long>(); final List<LdapRdn> names = dn.getNames(); for (int i = names.size(); i > 0; i--) { LdapRdn name = names.get(i-1); if ("ou".equals(name.getKey())) { final String grpName = name.getValue("ou"); groups.add(provider.createGroup(grpName, null, false, true)); break; } } return groups; }
/** * Creates the user authority list from the values of the {@code memberOf} attribute * obtained from the user's Active Directory entry. */ @Override protected Collection<? extends GrantedAuthority> loadUserAuthorities( DirContextOperations userData, String username, String password) { String[] groups = userData.getStringAttributes("memberOf"); if (groups == null) { logger.debug("No values for 'memberOf' attribute."); return AuthorityUtils.NO_AUTHORITIES; } if (logger.isDebugEnabled()) { logger.debug("'memberOf' attribute values: " + Arrays.asList(groups)); } ArrayList<GrantedAuthority> authorities = new ArrayList<>( groups.length); for (String group : groups) { authorities.add(new SimpleGrantedAuthority(new DistinguishedName(group) .removeLast().getValue())); } return authorities; }
@Test public void testLdapRdn_Parse_MultipleComponents() { LdapRdn rdn = new LdapRdn("cn=John Doe+sn=Doe"); assertThat(rdn.getComponent(0).encodeLdap()).isEqualTo("cn=John Doe"); assertThat(rdn.getComponent(1).encodeLdap()).isEqualTo("sn=Doe"); assertThat(rdn.getLdapEncoded()).isEqualTo("cn=John Doe+sn=Doe"); assertThat(rdn.getKey()).isEqualTo("cn"); assertThat(rdn.getValue()).isEqualTo("John Doe"); assertThat(rdn.getValue("cn")).isEqualTo("John Doe"); assertThat(rdn.getValue("sn")).isEqualTo("Doe"); }
@Test(expected = IllegalArgumentException.class) public void testGetValueNoKeyWithCorrectValue() { LdapRdn tested = new LdapRdn("cn=john doe"); tested.getValue("sn"); }
@Test(expected = IllegalArgumentException.class) public void testGetValueNoComponents() { LdapRdn tested = new LdapRdn(); tested.getValue("sn"); }
@Test public void testLdapRdn_parse_simple() { LdapRdn rdn = new LdapRdn("foo=bar"); assertThat(rdn.getComponent().getKey()).isEqualTo("foo"); assertThat(rdn.getComponent().getValue()).isEqualTo("bar"); assertThat(rdn.getComponent().getLdapEncoded()).isEqualTo("foo=bar"); assertThat(rdn.getKey()).isEqualTo("foo"); assertThat(rdn.getValue()).isEqualTo("bar"); }
authorities.add(new SimpleGrantedAuthority(dn.removeLast().getValue()));
authorities.add(new SimpleGrantedAuthority(dn.removeLast().getValue()));
/** * Test for http://forum.springsource.org/showthread.php?t=86640. */ @Test public void testDistinguishedNameWithDotParsesProperly() { DistinguishedName name = new DistinguishedName("cn=first.last,OU=DevTest Users,DC=xyz,DC=com"); assertThat(name.toCompactString()).isEqualTo("cn=first.last,ou=DevTest Users,dc=xyz,dc=com"); DistinguishedName dn = new DistinguishedName(); dn.parse("cn=first.last,OU=DevTest Users,DC=xyz,DC=com"); assertThat(dn.getValue("cn")).isEqualTo("first.last"); assertThat(dn.getValue("ou")).isEqualTo("DevTest Users"); assertThat(dn.getLdapRdn(1).getValue()).isEqualTo("xyz"); assertThat(dn.getLdapRdn(0).getValue()).isEqualTo("com"); }
if (rdn.getKey().equals(searchConfiguration.getLdapIdAttribute())) entityId = rdn.getValue();