Started work on assembly and moved acegi support out of the core module.

This commit is contained in:
Mattias Arthursson
2008-06-05 13:48:32 +00:00
parent 4278113350
commit 56187ce543
18 changed files with 497 additions and 260 deletions

View File

@@ -32,6 +32,17 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>
src/assemble/bin-with-dependencies.xml
</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -0,0 +1,23 @@
<assembly>
<id>bin-with-dependencies</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target</directory>
<includes>
<include>*.jar</include>
</includes>
<outputDirectory>dist</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<scope>provided</scope>
</dependencySet>
</dependencySets>
</assembly>

View File

@@ -1,87 +0,0 @@
/*
* Copyright 2005-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.authentication;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
import org.acegisecurity.userdetails.ldap.LdapUserDetails;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.core.AuthenticationSource;
/**
* An AuthenticationSource to retrieve authentication information stored in
* Acegi's SecurityContextHolder. Use Acegi's LdapAuthenticationProvider have a
* LdapUserDetails object placed in the authentication.
*
* @author Mattias Arthursson
*
*/
public class AcegiAuthenticationSource implements AuthenticationSource {
private static final Log log = LogFactory
.getLog(AcegiAuthenticationSource.class);
/**
* Get the principals of the logged in user, in this case the distinguished
* name.
*
* @return the distinguished name of the logged in user.
*/
public String getPrincipal() {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
if (principal instanceof LdapUserDetails) {
LdapUserDetails details = (LdapUserDetails) principal;
return details.getDn();
} else if (authentication instanceof AnonymousAuthenticationToken) {
if (log.isDebugEnabled()) {
log
.debug("Anonymous Authentication, returning empty String as Principal");
}
return "";
} else {
throw new IllegalArgumentException(
"The principal property of the authentication object -"
+ "needs to be a LdapUserDetails.");
}
} else {
log.warn("No Authentication object set in SecurityContext - "
+ "returning empty String as Principal");
return "";
}
}
/*
* @see org.springframework.ldap.core.AuthenticationSource#getCredentials()
*/
public String getCredentials() {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
if (authentication != null) {
return (String) authentication.getCredentials();
} else {
log.warn("No Authentication object set in SecurityContext - "
+ "returning empty String as Credentials");
return "";
}
}
}

View File

@@ -23,7 +23,7 @@ import org.springframework.ldap.core.AuthenticationSource;
/**
* Decorator on AuthenticationSource to have default authentication information
* be returned should the target return empty principal and credentials. Useful
* in combination with {@link AcegiAuthenticationSource} if users are to be
* in combination with <code>AcegiAuthenticationSource</code> if users are to be
* allowed to read some information even though they are not logged in.
* <p>
* <b>Note:</b> The <code>defaultUser</code> should be an non-privileged

View File

@@ -1,144 +0,0 @@
/*
* Copyright 2005-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.authentication;
import junit.framework.TestCase;
import org.acegisecurity.Authentication;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.ldap.LdapUserDetails;
import org.easymock.MockControl;
import org.springframework.ldap.authentication.AcegiAuthenticationSource;
public class AcegiAuthenticationSourceTest extends TestCase {
private MockControl authenticationControl;
private Authentication authenticationMock;
private MockControl ldapUserDetailsControl;
private LdapUserDetails ldapUserDetailsMock;
private AcegiAuthenticationSource tested;
protected void setUp() throws Exception {
super.setUp();
authenticationControl = MockControl.createControl(Authentication.class);
authenticationMock = (Authentication) authenticationControl.getMock();
ldapUserDetailsControl = MockControl
.createControl(LdapUserDetails.class);
ldapUserDetailsMock = (LdapUserDetails) ldapUserDetailsControl
.getMock();
tested = new AcegiAuthenticationSource();
}
protected void tearDown() throws Exception {
super.tearDown();
authenticationControl = null;
authenticationMock = null;
ldapUserDetailsControl = null;
ldapUserDetailsMock = null;
tested = null;
}
protected void replay() {
authenticationControl.replay();
ldapUserDetailsControl.replay();
}
protected void verify() {
authenticationControl.verify();
ldapUserDetailsControl.verify();
}
public void testGetPrincipalAndCredentials() {
authenticationControl.expectAndReturn(
authenticationMock.getPrincipal(), ldapUserDetailsMock);
authenticationControl.expectAndReturn(authenticationMock
.getCredentials(), "secret");
ldapUserDetailsControl.expectAndDefaultReturn(ldapUserDetailsMock
.getDn(), "cn=Manager");
SecurityContextHolder.getContext()
.setAuthentication(authenticationMock);
replay();
String principal = tested.getPrincipal();
String credentials = tested.getCredentials();
verify();
assertEquals("secret", credentials);
assertEquals("cn=Manager", principal);
}
public void testGetPrincipalAndCredentials_nullAuthentication() {
SecurityContextHolder.getContext().setAuthentication(null);
replay();
assertEquals("", tested.getPrincipal());
assertEquals("", tested.getCredentials());
verify();
}
public void testGetPrincipal_InvalidPrincipal() {
authenticationControl.expectAndReturn(
authenticationMock.getPrincipal(), new User("dummy", "dummy",
true, true, true, true, new GrantedAuthority[0]));
SecurityContextHolder.getContext()
.setAuthentication(authenticationMock);
replay();
try {
tested.getPrincipal();
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
verify();
}
public void testGetPrincipalWithAnonymousAuthenticationToken() {
SecurityContextHolder.getContext().setAuthentication(
new AnonymousAuthenticationToken("dummy", "dummy",
new GrantedAuthority[] { new DummyAuthoroty() }));
assertEquals("", tested.getPrincipal());
}
private static class DummyAuthoroty implements GrantedAuthority {
public String getAuthority() {
return null;
}
}
}

View File

@@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-acegi-support-integration-tests</artifactId>
<name>Spring LDAP Acegi Support Integration Tests</name>
<version>1.2.2-SNAPSHOT</version>
<properties>
<spring.version>2.0.8</spring.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>test</phase>
<goals></goals>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<forkMode>once</forkMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-test</artifactId>
<version>1.2.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-acegi-support</artifactId>
<version>1.2.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>ldapbp</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/../../lib/ldapbp.jar</systemPath>
</dependency>
<!-- Spring Dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.acegisecurity</groupId>
<artifactId>acegi-security</artifactId>
<version>1.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>gsbase</groupId>
<artifactId>gsbase</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<!--
This context configuration file defines common beans,
minimizing duplication in the various test context files.
-->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/conf/ldap.properties" />
</bean>
</beans>

View File

@@ -0,0 +1,4 @@
urls=ldap://127.0.0.1:3905
userDn=uid=admin,ou=system
password=secret
base=dc=jayway,dc=se

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<import resource="classpath:/conf/commonTestContext.xml" />
<bean id="contextSource"
class="org.springframework.ldap.test.TestContextSourceFactoryBean">
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" />
<property name="defaultPartitionName" value="jayway" />
<property name="principal" value="${userDn}" />
<property name="password" value="${password}" />
<property name="ldifFile" value="classpath:/setup_data.ldif" />
<property name="port" value="3905" />
<property name="authenticationSource">
<bean
class="org.springframework.ldap.authentication.AcegiAuthenticationSource" />
</property>
<property name="baseOnTarget" value="false" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="initialDirContextFactory"
class="org.acegisecurity.ldap.DefaultInitialDirContextFactory">
<constructor-arg value="${urls}/${base}" />
<property name="managerDn" value="${userDn}" />
<property name="managerPassword" value="${password}" />
</bean>
<bean id="userSearch"
class="org.acegisecurity.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg index="0">
<value></value>
</constructor-arg>
<constructor-arg index="1"
value="(&amp;(cn={0})(objectclass=person))" />
<constructor-arg index="2" ref="initialDirContextFactory" />
<property name="searchSubtree" value="true" />
</bean>
<bean id="ldapAuthProvider"
class="org.acegisecurity.providers.ldap.LdapAuthenticationProvider">
<constructor-arg>
<bean
class="org.acegisecurity.providers.ldap.authenticator.BindAuthenticator">
<constructor-arg ref="initialDirContextFactory" />
<property name="userSearch" ref="userSearch" />
</bean>
</constructor-arg>
<constructor-arg>
<bean
class="org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator">
<constructor-arg ref="initialDirContextFactory" />
<constructor-arg value="c=Sweden" />
<property name="groupRoleAttribute" value="ou" />
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,9 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
#Enable debug logging
log4j.category.net.sf.ldaptemplate=INFO
log4j.category.net.sf.ldaptemplate.LdapTemplate=DEBUG

View File

@@ -0,0 +1,110 @@
dn: ou=groups,dc=jayway,dc=se
objectclass: top
objectclass: organizationalUnit
ou: groups
dn: cn=ROLE_USER,ou=groups,dc=jayway,dc=se
objectclass: top
objectclass: groupOfUniqueNames
cn: ROLE_USER
uniqueMember: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se
uniqueMember: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se
uniqueMember: cn=Some Person,ou=company1,c=Norway,dc=jayway,dc=se
uniqueMember: cn=Some Person,ou=company2,c=Sweden,dc=jayway,dc=se
uniqueMember: cn=Some Person3,ou=company1,c=Sweden,dc=jayway,dc=se
dn: cn=ROLE_ADMIN,ou=groups,dc=jayway,dc=se
objectclass: top
objectclass: groupOfUniqueNames
cn: ROLE_ADMIN
uniqueMember: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se
dn: c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: country
c: Sweden
description: The country of Sweden
dn: c=Norway,dc=jayway,dc=se
objectclass: top
objectclass: country
c: Norway
description: The country of Norway
dn: ou=company1,c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: organizationalUnit
ou: company1
description: First company in Sweden
dn: ou=company2,c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: organizationalUnit
ou: company2
description: Second company in Sweden
dn: ou=company1,c=Norway,dc=jayway,dc=se
objectclass: top
objectclass: organizationalUnit
ou: company1
description: First company in Norway
dn: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: some.person
userPassword: password
cn: Some Person
sn: Person
description: Sweden, Company1, Some Person
telephoneNumber: +46 555-123456
dn: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: some.person2
userPassword: password
cn: Some Person2
sn: Person2
description: Sweden, Company1, Some Person2
telephoneNumber: +46 555-654321
dn: cn=Some Person3,ou=company1,c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: some.person3
userPassword: password
cn: Some Person3
sn: Person3
description: Sweden, Company1, Some Person3
telephoneNumber: +46 555-123654
dn: cn=Some Person,ou=company2,c=Sweden,dc=jayway,dc=se
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: some.person4
userPassword: password
cn: Some Person
sn: Person
description: Sweden, Company2, Some Person
telephoneNumber: +46 555-456321
dn: cn=Some Person+sn=Person,ou=company1,c=Norway,dc=jayway,dc=se
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: some.norwegian
userPassword: password
cn: Some Person
sn: Person
description: Norway, Company1, Some Person+Person
telephoneNumber: +45 555-654123

View File

@@ -0,0 +1,5 @@
ou=groups,dc=jayway,dc=se
c=Sweden,dc=jayway,dc=se
c=Norway,dc=jayway,dc=se

View File

@@ -26,9 +26,33 @@
<target>1.4</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>test</phase>
<goals></goals>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<forkMode>once</forkMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.ldap</groupId>
@@ -48,12 +72,6 @@
<scope>system</scope>
<systemPath>${basedir}/../../lib/ldapbp.jar</systemPath>
</dependency>
<dependency>
<groupId>org.acegisecurity</groupId>
<artifactId>acegi-security</artifactId>
<version>1.0.3</version>
<scope>provided</scope>
</dependency>
<!-- Spring Dependencies -->
<dependency>
<groupId>org.springframework</groupId>

View File

@@ -14,5 +14,36 @@
<modules>
<module>core-integration-tests</module>
<module>tiger-integration-tests</module>
<module>acegi-support-integration-tests</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>test</phase>
<goals></goals>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<forkMode>once</forkMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -26,6 +26,31 @@
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>test</phase>
<goals></goals>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<forkMode>once</forkMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@@ -14,6 +14,7 @@
<modules>
<module>core</module>
<module>tiger</module>
<module>acegi-support</module>
<module>test-support</module>
<module>integration-tests</module>
</modules>
@@ -32,25 +33,16 @@
-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.4</version>
<configuration>
<aggregate>true</aggregate>
<header>Spring LDAP Integration</header>
<link>
http://java.sun.com/j2se/1.5.0/docs/api
</link>
<link>
http://static.springframework.org/spring/docs/2.5.x/api/
</link>
<link>
http://commons.apache.org/pool/apidocs/
</link>
</configuration>
</plugin>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<forkMode>once</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>com.agilejava.docbkx</groupId>
<artifactId>docbkx-maven-plugin</artifactId>
@@ -104,5 +96,27 @@
</configuration>
</plugin>
</plugins>
</build>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.4</version>
<configuration>
<aggregate>true</aggregate>
<header>Spring LDAP Integration</header>
<link>
http://java.sun.com/j2se/1.5.0/docs/api
</link>
<link>
http://static.springframework.org/spring/docs/2.5.x/api/
</link>
<link>
http://commons.apache.org/pool/apidocs/
</link>
</configuration>
</plugin>
</plugins>
</reporting>
</project>

View File

@@ -134,7 +134,12 @@ public class LdapTestUtils {
clearSubContexts(ctx, name);
}
finally {
ctx.close();
try {
ctx.close();
}
catch (Exception e) {
// Never mind this
}
}
}