1
0
mirror of synced 2026-08-04 09:17:02 +00:00

Improvements to LDAP namespace configuration - splitting "ldap" element into ldap-server and ldap-authentication-provider. Also some minor changes to authentication-provider.

This commit is contained in:
Luke Taylor
2007-12-23 00:17:37 +00:00
parent d0490e6322
commit debfbe47cf
21 changed files with 658 additions and 82 deletions
@@ -0,0 +1,49 @@
package org.springframework.security.config;
import org.springframework.security.providers.ProviderManager;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.providers.AuthenticationProvider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.BeansException;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import java.util.List;
/**
* @author Luke Taylor
* @version $Id$
*/
public class AuthenticationProviderBeanDefinitionParserTests {
private static ClassPathXmlApplicationContext appContext;
@BeforeClass
public static void loadContext() {
try {
appContext = new ClassPathXmlApplicationContext("org/springframework/security/config/auth-provider.xml");
} catch (BeansException e) {
e.printStackTrace();
}
}
@AfterClass
public static void closeAppContext() {
if (appContext != null) {
appContext.close();
}
}
@Test
public void configuredProvidersAllAuthenticateUser() {
List<AuthenticationProvider> providers =
((ProviderManager)appContext.getBean(BeanIds.AUTHENTICATION_MANAGER)).getProviders();
UsernamePasswordAuthenticationToken bob = new UsernamePasswordAuthenticationToken("bob", "bobspassword");
for (AuthenticationProvider provider : providers) {
provider.authenticate(bob);
}
}
}
@@ -0,0 +1,49 @@
package org.springframework.security.config;
import org.springframework.security.providers.ProviderManager;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.providers.ldap.LdapAuthenticationProvider;
import org.springframework.security.Authentication;
import org.springframework.security.util.InMemoryXmlApplicationContext;
import org.springframework.security.userdetails.ldap.LdapUserDetailsImpl;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.After;
/**
* @author luke
* @version $Id$
*/
public class LdapProviderBeanDefinitionParserTests {
InMemoryXmlApplicationContext appCtx;
@After
public void closeAppContext() {
if (appCtx != null) {
appCtx.close();
appCtx = null;
}
}
@Test
public void simpleProviderAuthenticatesCorrectly() {
appCtx = new InMemoryXmlApplicationContext("<ldap-server /> <ldap-authentication-provider />");
ProviderManager authManager = (ProviderManager) appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER);
assertEquals(1, authManager.getProviders().size());
LdapAuthenticationProvider provider = (LdapAuthenticationProvider) authManager.getProviders().get(0);
Authentication auth = provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "benspassword"));
LdapUserDetailsImpl ben = (LdapUserDetailsImpl) auth.getPrincipal();
assertEquals(2, ben.getAuthorities().length);
}
@Test(expected = SecurityConfigurationException.class)
public void missingServerEltCausesConfigException() {
appCtx = new InMemoryXmlApplicationContext("<ldap-authentication-provider />");
}
}
@@ -0,0 +1,66 @@
package org.springframework.security.config;
import org.springframework.security.util.InMemoryXmlApplicationContext;
import org.springframework.security.ldap.SpringSecurityContextSource;
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.ldap.core.LdapTemplate;
import org.junit.Test;
import org.junit.After;
/**
* @author Luke Taylor
* @version $Id$
*/
public class LdapServerBeanDefinitionParserTests {
InMemoryXmlApplicationContext appCtx;
@After
public void closeAppContext() {
if (appCtx != null) {
appCtx.close();
appCtx = null;
}
}
@Test
public void embeddedServerCreationContainsExpectedContextSourceAndData() {
appCtx = new InMemoryXmlApplicationContext("<ldap-server />");
SpringSecurityContextSource contextSource = (SpringSecurityContextSource) appCtx.getBean(BeanIds.CONTEXT_SOURCE);
// Check data is loaded
LdapTemplate template = new LdapTemplate(contextSource);
template.lookup("uid=ben,ou=people");
}
@Test
public void useOfUrlAttributeCreatesCorrectContextSource() {
// Create second "server" with a url pointing at embedded one
appCtx = new InMemoryXmlApplicationContext("<ldap-server port=\"33388\"/>" +
"<ldap-server id=\"blah\" url=\"ldap://127.0.0.1:33388/dc=springframework,dc=org\" />");
// Check the default context source is still there.
appCtx.getBean(BeanIds.CONTEXT_SOURCE);
SpringSecurityContextSource contextSource = (SpringSecurityContextSource) appCtx.getBean("blah");
// Check data is loaded as before
LdapTemplate template = new LdapTemplate(contextSource);
template.lookup("uid=ben,ou=people");
}
@Test
public void loadingSpecificLdifFileIsSuccessful() {
appCtx = new InMemoryXmlApplicationContext(
"<ldap-server ldif=\"classpath*:test-server2.xldif\" root=\"dc=monkeymachine,dc=co,dc=uk\" />");
SpringSecurityContextSource contextSource = (SpringSecurityContextSource) appCtx.getBean(BeanIds.CONTEXT_SOURCE);
LdapTemplate template = new LdapTemplate(contextSource);
template.lookup("uid=pg,ou=gorillas");
}
}
@@ -14,6 +14,7 @@
*/
package org.springframework.security.ldap;
import org.springframework.security.config.BeanIds;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.core.io.ClassPathResource;
@@ -42,7 +43,6 @@ import java.util.Set;
*/
public abstract class AbstractLdapIntegrationTests {
private static ClassPathXmlApplicationContext appContext;
private boolean dirty = false;
protected AbstractLdapIntegrationTests() {
}
@@ -77,19 +77,9 @@ public abstract class AbstractLdapIntegrationTests {
public void onSetUp() throws Exception {
}
/** Reloads the server data file */
protected void setDirty() {
dirty = true;
}
@After
public final void reloadServerDataIfDirty() throws Exception {
// if (!dirty) {
// return;
// }
// closeContext();
// loadContext();
ClassPathResource ldifs = new ClassPathResource("test-server.ldif");
if (!ldifs.getFile().exists()) {
@@ -111,7 +101,7 @@ public abstract class AbstractLdapIntegrationTests {
}
public SpringSecurityContextSource getContextSource() {
return (SpringSecurityContextSource) appContext.getBean("contextSource");
return (SpringSecurityContextSource) appContext.getBean(BeanIds.CONTEXT_SOURCE);
}
/**
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
<!-- All combinations should authenticate as bob/password -->
<authentication-provider>
<user-service>
<user name="bob" password="bobspassword" authorities="ROLE_A" />
</user-service>
</authentication-provider>
<authentication-provider user-service-ref="myUserService" />
<user-service id="myUserService">
<user name="bob" password="bobspassword" authorities="ROLE_A" />
</user-service>
<authentication-provider>
<password-encoder hash="md5"/>
<user-service>
<user name="bob" password="12b141f35d58b8b3a46eea65e6ac179e" authorities="ROLE_A" />
</user-service>
</authentication-provider>
<authentication-provider>
<password-encoder hash="{sha}"/>
<user-service>
<user name="bob" password="{SSHA}PpuEwfdj7M1rs0C2W4ssSM2XEN/Y6S5U" authorities="ROLE_A" />
</user-service>
</authentication-provider>
</beans:beans>
@@ -32,7 +32,7 @@ http://www.springframework.org/schema/security http://www.springframework.org/sc
</user-service>
</authentication-provider>
<beans:bean name="tokenRepo" class="org.springframework.security.ui.rememberme.InMemoryTokenRepositoryImpl"/>
<beans:bean id="tokenRepo" class="org.springframework.security.ui.rememberme.InMemoryTokenRepositoryImpl"/>
<!-- bean name="rememberMeServices" class="org.springframework.security.ui.rememberme.NullRememberMeServices"/ -->
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
<security:ldap />
<ldap-server ldif="classpath*:test-server2.xldif" root="dc=monkeymachine,dc=co,dc=uk" />
</beans>
</b:beans>
@@ -5,7 +5,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
<security:ldap port="53389" ldif="classpath:test-server.ldif"/>
<security:ldap-server port="53389" ldif="classpath:test-server.ldif"/>
<!--<import resource="classpath:/org/springframework/security/ldap/apacheDsContext.xml"/>-->
@@ -0,0 +1,16 @@
dn: ou=gorillas,dc=monkeymachine,dc=co,dc=uk
objectclass: top
objectclass: organizationalUnit
ou: gorillas
dn: uid=pg,ou=gorillas,dc=monkeymachine,dc=co,dc=uk
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Pierre
sn: Gorille
uid: pg
userPassword: password