New module for acegi support
This commit is contained in:
173
mvn-build/acegi-support/pom.xml
Normal file
173
mvn-build/acegi-support/pom.xml
Normal file
@@ -0,0 +1,173 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.springframework.ldap</groupId>
|
||||
<artifactId>spring-ldap-acegi-support</artifactId>
|
||||
<name>Spring LDAP Acegi Support</name>
|
||||
<version>1.2.2-SNAPSHOT</version>
|
||||
<parent>
|
||||
<groupId>org.springframework.ldap</groupId>
|
||||
<artifactId>master</artifactId>
|
||||
<version>1.2.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.4</source>
|
||||
<target>1.4</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>
|
||||
src/assemble/bin-with-dependencies.xml
|
||||
</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.ldap</groupId>
|
||||
<artifactId>spring-ldap</artifactId>
|
||||
<version>1.2.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.acegisecurity</groupId>
|
||||
<artifactId>acegi-security</artifactId>
|
||||
<version>1.0.7</version>
|
||||
<scope>provided</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>aspectj</groupId>
|
||||
<artifactId>aspectj</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>cas</groupId>
|
||||
<artifactId>casclient</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-collections</groupId>
|
||||
<artifactId>commons-collections</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jsp-api</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>jmock</groupId>
|
||||
<artifactId>jmock</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>net.sj.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.directory.server</groupId>
|
||||
<artifactId>apacheds-core</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-ldap</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-mock</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-remoting</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-support</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>oro</groupId>
|
||||
<artifactId>oro</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>taglibs</groupId>
|
||||
<artifactId>standard</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>1.2_Java1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user