SEC-99/428/429/563: Various refactoring of method security metadata support.
This commit is contained in:
@@ -34,12 +34,14 @@ public class MockJoinPoint implements JoinPoint {
|
||||
|
||||
private Method beingInvoked;
|
||||
private Object object;
|
||||
private Class declaringType;
|
||||
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public MockJoinPoint(Object object, Method beingInvoked) {
|
||||
this.object = object;
|
||||
this.beingInvoked = beingInvoked;
|
||||
this.declaringType = object.getClass();
|
||||
}
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
@@ -61,7 +63,7 @@ public class MockJoinPoint implements JoinPoint {
|
||||
}
|
||||
|
||||
public StaticPart getStaticPart() {
|
||||
return new MockStaticPart(beingInvoked);
|
||||
return new MockStaticPart(beingInvoked, declaringType);
|
||||
}
|
||||
|
||||
public Object getTarget() {
|
||||
@@ -84,13 +86,15 @@ public class MockJoinPoint implements JoinPoint {
|
||||
|
||||
private class MockCodeSignature implements CodeSignature {
|
||||
private Method beingInvoked;
|
||||
private Class declaringType;
|
||||
|
||||
public MockCodeSignature(Method beingInvoked) {
|
||||
public MockCodeSignature(Method beingInvoked, Class declaringType) {
|
||||
this.beingInvoked = beingInvoked;
|
||||
this.declaringType = declaringType;
|
||||
}
|
||||
|
||||
public Class getDeclaringType() {
|
||||
throw new UnsupportedOperationException("mock not implemented");
|
||||
return this.declaringType;
|
||||
}
|
||||
|
||||
public String getDeclaringTypeName() {
|
||||
@@ -128,9 +132,11 @@ public class MockJoinPoint implements JoinPoint {
|
||||
|
||||
private class MockStaticPart implements StaticPart {
|
||||
private Method beingInvoked;
|
||||
|
||||
public MockStaticPart(Method beingInvoked) {
|
||||
private Class declaringType;
|
||||
|
||||
public MockStaticPart(Method beingInvoked, Class declaringType) {
|
||||
this.beingInvoked = beingInvoked;
|
||||
this.declaringType = declaringType;
|
||||
}
|
||||
|
||||
public String getKind() {
|
||||
@@ -138,7 +144,7 @@ public class MockJoinPoint implements JoinPoint {
|
||||
}
|
||||
|
||||
public Signature getSignature() {
|
||||
return new MockCodeSignature(beingInvoked);
|
||||
return new MockCodeSignature(beingInvoked, declaringType);
|
||||
}
|
||||
|
||||
public SourceLocation getSourceLocation() {
|
||||
|
||||
@@ -29,10 +29,6 @@ package org.springframework.security;
|
||||
public class OtherTargetObject extends TargetObject implements ITargetObject {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public int countLength(String input) {
|
||||
return super.countLength(input);
|
||||
}
|
||||
|
||||
public String makeLowerCase(String input) {
|
||||
return super.makeLowerCase(input);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public class ContextPropagatingRemoteInvocationTests extends TestCase {
|
||||
throws Exception {
|
||||
Class clazz = TargetObject.class;
|
||||
Method method = clazz.getMethod("makeLowerCase", new Class[] {String.class});
|
||||
MethodInvocation mi = new SimpleMethodInvocation(method, new Object[] {"SOME_STRING"});
|
||||
MethodInvocation mi = new SimpleMethodInvocation(new TargetObject(), method, new Object[] {"SOME_STRING"});
|
||||
|
||||
ContextPropagatingRemoteInvocationFactory factory = new ContextPropagatingRemoteInvocationFactory();
|
||||
|
||||
|
||||
@@ -15,33 +15,13 @@
|
||||
|
||||
package org.springframework.security.intercept.method;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.security.ConfigAttribute;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.ITargetObject;
|
||||
import org.springframework.security.OtherTargetObject;
|
||||
import org.springframework.security.SecurityConfig;
|
||||
import org.springframework.security.TargetObject;
|
||||
|
||||
import org.springframework.security.acl.basic.SomeDomain;
|
||||
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
|
||||
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import org.springframework.security.util.SimpleMethodInvocation;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.ITargetObject;
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,180 +31,27 @@ import java.util.Set;
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class MethodDefinitionAttributesTests extends TestCase {
|
||||
//~ Instance fields ================================================================================================
|
||||
public class MethodDefinitionAttributesTests {
|
||||
|
||||
ClassPathXmlApplicationContext applicationContext;
|
||||
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
public MethodDefinitionAttributesTests(String a) {
|
||||
super(a);
|
||||
private MethodDefinitionAttributes build() {
|
||||
MethodDefinitionAttributes mda = new MethodDefinitionAttributes();
|
||||
mda.setAttributes(new MockAttributes());
|
||||
return mda;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodsReturned() throws Exception {
|
||||
Class clazz = ITargetObject.class;
|
||||
Method method = clazz.getMethod("countLength", new Class[] {String.class});
|
||||
ConfigAttributeDefinition result = build().findAttributes(method, ITargetObject.class);
|
||||
Assert.assertEquals(1, result.getConfigAttributes().size());
|
||||
}
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
SecurityContextHolder.clearContext();
|
||||
@Test
|
||||
public void testClassesReturned() throws Exception {
|
||||
Class clazz = ITargetObject.class;
|
||||
ConfigAttributeDefinition result = build().findAttributes(ITargetObject.class);
|
||||
Assert.assertEquals(1, result.getConfigAttributes().size());
|
||||
}
|
||||
|
||||
private ConfigAttributeDefinition getConfigAttributeDefinition(Class clazz, String methodName, Class[] args)
|
||||
throws Exception {
|
||||
|
||||
final Method method = clazz.getMethod(methodName, args);
|
||||
MethodDefinitionAttributes source = new MethodDefinitionAttributes();
|
||||
source.setAttributes(new MockAttributes());
|
||||
|
||||
ConfigAttributeDefinition config = source.getAttributes(new SimpleMethodInvocation() {
|
||||
public Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
});
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
private ITargetObject makeInterceptedTarget() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"org/springframework/security/intercept/method/applicationContext.xml");
|
||||
|
||||
return (ITargetObject) context.getBean("target");
|
||||
}
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void testAttributesForInterfaceTargetObject() throws Exception {
|
||||
ConfigAttributeDefinition def1 = getConfigAttributeDefinition(ITargetObject.class, "countLength",
|
||||
new Class[] {String.class});
|
||||
Set set1 = toSet(def1);
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_COUNT_LENGTH")));
|
||||
|
||||
ConfigAttributeDefinition def2 = getConfigAttributeDefinition(ITargetObject.class, "makeLowerCase",
|
||||
new Class[] {String.class});
|
||||
Set set2 = toSet(def2);
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")));
|
||||
|
||||
ConfigAttributeDefinition def3 = getConfigAttributeDefinition(ITargetObject.class, "makeUpperCase",
|
||||
new Class[] {String.class});
|
||||
Set set3 = toSet(def3);
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")));
|
||||
}
|
||||
|
||||
public void testAttributesForOtherTargetObject() throws Exception {
|
||||
ConfigAttributeDefinition def1 = getConfigAttributeDefinition(OtherTargetObject.class, "countLength",
|
||||
new Class[] {String.class});
|
||||
Set set1 = toSet(def1);
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_COUNT_LENGTH")));
|
||||
|
||||
// Confirm MOCK_CLASS_METHOD_COUNT_LENGTH not added, as it's a String (not a ConfigAttribute)
|
||||
// Confirm also MOCK_CLASS not added, as we return null for class
|
||||
assertEquals(2, set1.size());
|
||||
|
||||
ConfigAttributeDefinition def2 = getConfigAttributeDefinition(OtherTargetObject.class, "makeLowerCase",
|
||||
new Class[] {String.class});
|
||||
Set set2 = toSet(def2);
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")));
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_CLASS_METHOD_MAKE_LOWER_CASE")));
|
||||
|
||||
// Confirm MOCK_CLASS not added, as we return null for class
|
||||
assertEquals(3, set2.size());
|
||||
|
||||
ConfigAttributeDefinition def3 = getConfigAttributeDefinition(OtherTargetObject.class, "makeUpperCase",
|
||||
new Class[] {String.class});
|
||||
Set set3 = toSet(def3);
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")));
|
||||
assertTrue(set3.contains(new SecurityConfig("RUN_AS"))); // defined against interface
|
||||
|
||||
assertEquals(3, set3.size());
|
||||
}
|
||||
|
||||
public void testAttributesForTargetObject() throws Exception {
|
||||
ConfigAttributeDefinition def1 = getConfigAttributeDefinition(TargetObject.class, "countLength",
|
||||
new Class[] {String.class});
|
||||
Set set1 = toSet(def1);
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_COUNT_LENGTH")));
|
||||
|
||||
assertTrue(set1.contains(new SecurityConfig("MOCK_CLASS")));
|
||||
|
||||
// Confirm the MOCK_CLASS_METHOD_COUNT_LENGTH was not added, as it's not a ConfigAttribute
|
||||
assertEquals(3, set1.size());
|
||||
|
||||
ConfigAttributeDefinition def2 = getConfigAttributeDefinition(TargetObject.class, "makeLowerCase",
|
||||
new Class[] {String.class});
|
||||
Set set2 = toSet(def2);
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")));
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_CLASS")));
|
||||
assertTrue(set2.contains(new SecurityConfig("MOCK_CLASS_METHOD_MAKE_LOWER_CASE")));
|
||||
assertEquals(4, set2.size());
|
||||
|
||||
ConfigAttributeDefinition def3 = getConfigAttributeDefinition(TargetObject.class, "makeUpperCase",
|
||||
new Class[] {String.class});
|
||||
Set set3 = toSet(def3);
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE")));
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")));
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_CLASS")));
|
||||
assertTrue(set3.contains(new SecurityConfig("MOCK_CLASS_METHOD_MAKE_UPPER_CASE")));
|
||||
assertTrue(set3.contains(new SecurityConfig("RUN_AS")));
|
||||
assertEquals(5, set3.size());
|
||||
}
|
||||
|
||||
public void testMethodCallWithRunAsReplacement() throws Exception {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("MOCK_INTERFACE_METHOD_MAKE_UPPER_CASE")});
|
||||
SecurityContextHolder.getContext().setAuthentication(token);
|
||||
|
||||
ITargetObject target = makeInterceptedTarget();
|
||||
String result = target.makeUpperCase("hello");
|
||||
assertEquals("HELLO org.springframework.security.MockRunAsAuthenticationToken true", result);
|
||||
}
|
||||
|
||||
public void testMethodCallWithoutRunAsReplacement() throws Exception {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("MOCK_INTERFACE_METHOD_MAKE_LOWER_CASE")});
|
||||
SecurityContextHolder.getContext().setAuthentication(token);
|
||||
|
||||
ITargetObject target = makeInterceptedTarget();
|
||||
String result = target.makeLowerCase("HELLO");
|
||||
|
||||
assertEquals("hello org.springframework.security.providers.UsernamePasswordAuthenticationToken true", result);
|
||||
}
|
||||
|
||||
public void testNullReturnedIfZeroAttributesDefinedForMethodInvocation()
|
||||
throws Exception {
|
||||
// SomeDomain is not defined in the MockAttributes()
|
||||
// (which getConfigAttributeDefinition refers to)
|
||||
ConfigAttributeDefinition def = getConfigAttributeDefinition(SomeDomain.class, "getId", null);
|
||||
assertNull(def);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert a <code>ConfigAttributeDefinition</code> into a set of <code>ConfigAttribute</code>(s)
|
||||
*
|
||||
* @param def the <code>ConfigAttributeDefinition</code> to cover
|
||||
*
|
||||
* @return a Set of <code>ConfigAttributes</code>
|
||||
*/
|
||||
private Set toSet(ConfigAttributeDefinition def) {
|
||||
Set set = new HashSet();
|
||||
Iterator i = def.getConfigAttributes().iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
ConfigAttribute a = (ConfigAttribute) i.next();
|
||||
set.add(a);
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ package org.springframework.security.intercept.method;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.ITargetObject;
|
||||
import org.springframework.security.MockJoinPoint;
|
||||
import org.springframework.security.OtherTargetObject;
|
||||
import org.springframework.security.TargetObject;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
@@ -30,7 +32,7 @@ import java.util.Iterator;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link MethodDefinitionSourceEditor} and its associated {@link MethodDefinitionMap}.
|
||||
* Tests {@link MethodDefinitionSourceEditor} and its associated {@link MapBasedMethodDefinitionSource}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
@@ -55,7 +57,7 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("org.springframework.security.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
|
||||
Class clazz = TargetObject.class;
|
||||
Method method = clazz.getMethod("countLength", new Class[] {String.class});
|
||||
@@ -101,32 +103,41 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testConcreteClassInvocationsAlsoReturnDefinitionsAgainstInterface()
|
||||
throws Exception {
|
||||
public void testConcreteClassInvocationsAlsoReturnDefinitionsAgainstInterface() throws Exception {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText(
|
||||
"org.springframework.security.ITargetObject.makeLower*=ROLE_FROM_INTERFACE\r\norg.springframework.security.ITargetObject.makeUpper*=ROLE_FROM_INTERFACE\r\norg.springframework.security.TargetObject.makeUpper*=ROLE_FROM_IMPLEMENTATION");
|
||||
"org.springframework.security.ITargetObject.computeHashCode*=ROLE_FROM_INTERFACE\r\n" +
|
||||
"org.springframework.security.ITargetObject.makeLower*=ROLE_FROM_INTERFACE\r\n" +
|
||||
"org.springframework.security.ITargetObject.makeUpper*=ROLE_FROM_INTERFACE\r\n" +
|
||||
"org.springframework.security.TargetObject.computeHashCode*=ROLE_FROM_TO\r\n" +
|
||||
"org.springframework.security.OtherTargetObject.computeHashCode*=ROLE_FROM_OTO\r\n" +
|
||||
"org.springframework.security.OtherTargetObject.makeUpper*=ROLE_FROM_IMPLEMENTATION");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
assertEquals(3, map.getMethodMapSize());
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
assertEquals(6, map.getMethodMapSize());
|
||||
|
||||
ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation(TargetObject.class,
|
||||
"makeLowerCase", new Class[] {String.class}));
|
||||
ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation(ITargetObject.class, "makeLowerCase", new Class[] {String.class}, new OtherTargetObject()));
|
||||
ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition("ROLE_FROM_INTERFACE");
|
||||
assertEquals(expectedMakeLower, returnedMakeLower);
|
||||
|
||||
ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation(TargetObject.class,
|
||||
"makeUpperCase", new Class[] {String.class}));
|
||||
ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition(
|
||||
new String[]{"ROLE_FROM_IMPLEMENTATION","ROLE_FROM_INTERFACE"});
|
||||
ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation(ITargetObject.class, "makeUpperCase", new Class[] {String.class}, new OtherTargetObject()));
|
||||
ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition(new String[]{"ROLE_FROM_IMPLEMENTATION"});
|
||||
assertEquals(expectedMakeUpper, returnedMakeUpper);
|
||||
|
||||
ConfigAttributeDefinition returnedComputeHashCode = map.getAttributes(new MockMethodInvocation(ITargetObject.class, "computeHashCode", new Class[] {String.class}, new OtherTargetObject()));
|
||||
ConfigAttributeDefinition expectedComputeHashCode = new ConfigAttributeDefinition(new String[]{"ROLE_FROM_OTO"});
|
||||
assertEquals(expectedComputeHashCode, returnedComputeHashCode);
|
||||
|
||||
returnedComputeHashCode = map.getAttributes(new MockMethodInvocation(ITargetObject.class, "computeHashCode", new Class[] {String.class}, new TargetObject()));
|
||||
expectedComputeHashCode = new ConfigAttributeDefinition(new String[]{"ROLE_FROM_TO"});
|
||||
assertEquals(expectedComputeHashCode, returnedComputeHashCode);
|
||||
}
|
||||
|
||||
public void testEmptyStringReturnsEmptyMap() {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
assertEquals(0, map.getMethodMapSize());
|
||||
}
|
||||
|
||||
@@ -135,7 +146,7 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
editor.setAsText(
|
||||
"org.springframework.security.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY\r\norg.springframework.security.TargetObject.make*=ROLE_NINE,ROLE_SUPERVISOR");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
Iterator iter = map.getConfigAttributeDefinitions().iterator();
|
||||
int counter = 0;
|
||||
|
||||
@@ -152,7 +163,7 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
editor.setAsText(
|
||||
"org.springframework.security.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY\r\norg.springframework.security.TargetObject.make*=ROLE_NINE,ROLE_SUPERVISOR");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
assertEquals(3, map.getMethodMapSize());
|
||||
}
|
||||
|
||||
@@ -161,21 +172,21 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
editor.setAsText(
|
||||
"org.springframework.security.TargetObject.*=ROLE_GENERAL\r\norg.springframework.security.TargetObject.makeLower*=ROLE_LOWER\r\norg.springframework.security.TargetObject.make*=ROLE_MAKE\r\norg.springframework.security.TargetObject.makeUpper*=ROLE_UPPER");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
assertEquals(5, map.getMethodMapSize());
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
assertEquals(14, map.getMethodMapSize());
|
||||
|
||||
ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation(TargetObject.class,
|
||||
"makeLowerCase", new Class[] {String.class}));
|
||||
ConfigAttributeDefinition returnedMakeLower = map.getAttributes(new MockMethodInvocation(ITargetObject.class,
|
||||
"makeLowerCase", new Class[] {String.class}, new TargetObject()));
|
||||
ConfigAttributeDefinition expectedMakeLower = new ConfigAttributeDefinition("ROLE_LOWER");
|
||||
assertEquals(expectedMakeLower, returnedMakeLower);
|
||||
|
||||
ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation(TargetObject.class,
|
||||
"makeUpperCase", new Class[] {String.class}));
|
||||
ConfigAttributeDefinition returnedMakeUpper = map.getAttributes(new MockMethodInvocation(ITargetObject.class,
|
||||
"makeUpperCase", new Class[] {String.class}, new TargetObject()));
|
||||
ConfigAttributeDefinition expectedMakeUpper = new ConfigAttributeDefinition("ROLE_UPPER");
|
||||
assertEquals(expectedMakeUpper, returnedMakeUpper);
|
||||
|
||||
ConfigAttributeDefinition returnedCountLength = map.getAttributes(new MockMethodInvocation(TargetObject.class,
|
||||
"countLength", new Class[] {String.class}));
|
||||
ConfigAttributeDefinition returnedCountLength = map.getAttributes(new MockMethodInvocation(ITargetObject.class,
|
||||
"countLength", new Class[] {String.class}, new TargetObject()));
|
||||
ConfigAttributeDefinition expectedCountLength = new ConfigAttributeDefinition("ROLE_GENERAL");
|
||||
assertEquals(expectedCountLength, returnedCountLength);
|
||||
}
|
||||
@@ -184,10 +195,10 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("org.springframework.security.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
|
||||
ConfigAttributeDefinition configAttributeDefinition = map.getAttributes(new MockMethodInvocation(
|
||||
TargetObject.class, "makeLowerCase", new Class[] {String.class}));
|
||||
ITargetObject.class, "makeLowerCase", new Class[] {String.class}, new TargetObject()));
|
||||
assertNull(configAttributeDefinition);
|
||||
}
|
||||
|
||||
@@ -195,7 +206,7 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText(null);
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
assertEquals(0, map.getMethodMapSize());
|
||||
}
|
||||
|
||||
@@ -203,10 +214,10 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("org.springframework.security.TargetObject.countLength=ROLE_ONE,ROLE_TWO,RUN_AS_ENTRY");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
|
||||
ConfigAttributeDefinition returnedCountLength = map.getAttributes(new MockMethodInvocation(TargetObject.class,
|
||||
"countLength", new Class[] {String.class}));
|
||||
ConfigAttributeDefinition returnedCountLength = map.getAttributes(new MockMethodInvocation(ITargetObject.class,
|
||||
"countLength", new Class[] {String.class}, new TargetObject()));
|
||||
ConfigAttributeDefinition expectedCountLength = new ConfigAttributeDefinition(
|
||||
new String[] {"ROLE_ONE", "ROLE_TWO", "RUN_AS_ENTRY"});
|
||||
assertEquals(expectedCountLength, returnedCountLength);
|
||||
@@ -215,11 +226,13 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
private class MockMethodInvocation implements MethodInvocation {
|
||||
Method method;
|
||||
private Method method;
|
||||
private Object targetObject;
|
||||
|
||||
public MockMethodInvocation(Class clazz, String methodName, Class[] parameterTypes)
|
||||
public MockMethodInvocation(Class clazz, String methodName, Class[] parameterTypes, Object targetObject)
|
||||
throws NoSuchMethodException {
|
||||
method = clazz.getMethod(methodName, parameterTypes);
|
||||
this.method = clazz.getMethod(methodName, parameterTypes);
|
||||
this.targetObject = targetObject;
|
||||
}
|
||||
|
||||
public Object[] getArguments() {
|
||||
@@ -235,7 +248,7 @@ public class MethodDefinitionSourceEditorTests extends TestCase {
|
||||
}
|
||||
|
||||
public Object getThis() {
|
||||
return null;
|
||||
return targetObject;
|
||||
}
|
||||
|
||||
public Object proceed() throws Throwable {
|
||||
|
||||
@@ -20,6 +20,7 @@ import junit.framework.TestCase;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.ITargetObject;
|
||||
import org.springframework.security.OtherTargetObject;
|
||||
|
||||
import org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor;
|
||||
|
||||
@@ -88,7 +89,7 @@ public class MethodInvocationPrivilegeEvaluatorTests extends TestCase {
|
||||
throws Exception {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("MOCK_LOWER")});
|
||||
MethodInvocation mi = MethodInvocationUtils.createFromClass(ITargetObject.class, "makeLowerCase",
|
||||
MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase",
|
||||
new Class[] {String.class}, new Object[] {"Hello world"});
|
||||
MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
|
||||
|
||||
@@ -117,7 +118,7 @@ public class MethodInvocationPrivilegeEvaluatorTests extends TestCase {
|
||||
throws Exception {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_NOT_HELD")});
|
||||
MethodInvocation mi = MethodInvocationUtils.createFromClass(ITargetObject.class, "makeLowerCase",
|
||||
MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase",
|
||||
new Class[] {String.class}, new Object[] {"helloWorld"});
|
||||
MethodSecurityInterceptor interceptor = makeSecurityInterceptor();
|
||||
|
||||
|
||||
@@ -71,4 +71,8 @@ public class MockMethodDefinitionSource extends AbstractMethodDefinitionSource {
|
||||
protected ConfigAttributeDefinition lookupAttributes(Method method) {
|
||||
throw new UnsupportedOperationException("mock method not implemented");
|
||||
}
|
||||
|
||||
public ConfigAttributeDefinition getAttributes(Method method, Class targetClass) {
|
||||
throw new UnsupportedOperationException("mock method not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,17 +15,14 @@
|
||||
|
||||
package org.springframework.security.intercept.method.aopalliance;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.security.TargetObject;
|
||||
|
||||
import org.springframework.security.intercept.method.MethodDefinitionMap;
|
||||
import org.springframework.security.intercept.method.MapBasedMethodDefinitionSource;
|
||||
import org.springframework.security.intercept.method.MethodDefinitionSourceEditor;
|
||||
|
||||
import org.springframework.aop.framework.AopConfigException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link MethodDefinitionSourceAdvisor}.
|
||||
@@ -50,7 +47,7 @@ public class MethodDefinitionSourceAdvisorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("org.springframework.security.TargetObject.countLength=ROLE_NOT_USED");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
|
||||
MethodSecurityInterceptor msi = new MethodSecurityInterceptor();
|
||||
msi.setObjectDefinitionSource(map);
|
||||
@@ -90,7 +87,7 @@ public class MethodDefinitionSourceAdvisorTests extends TestCase {
|
||||
try {
|
||||
new MethodDefinitionSourceAdvisor(msi);
|
||||
fail("Should have detected null ObjectDefinitionSource and thrown AopConfigException");
|
||||
} catch (AopConfigException expected) {
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -99,7 +96,7 @@ public class MethodDefinitionSourceAdvisorTests extends TestCase {
|
||||
Class clazz = TargetObject.class;
|
||||
Method method = clazz.getMethod("countLength", new Class[] {String.class});
|
||||
|
||||
MethodDefinitionSourceAdvisor.InternalMethodInvocation imi = new MethodDefinitionSourceAdvisor(getInterceptor()).new InternalMethodInvocation(method);
|
||||
MethodDefinitionSourceAdvisor.InternalMethodInvocation imi = new MethodDefinitionSourceAdvisor(getInterceptor()).new InternalMethodInvocation(method, clazz);
|
||||
|
||||
try {
|
||||
imi.getArguments();
|
||||
@@ -115,13 +112,6 @@ public class MethodDefinitionSourceAdvisorTests extends TestCase {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
imi.getThis();
|
||||
fail("Should have thrown UnsupportedOperationException");
|
||||
} catch (UnsupportedOperationException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
imi.proceed();
|
||||
fail("Should have thrown UnsupportedOperationException");
|
||||
|
||||
@@ -455,7 +455,11 @@ public class MethodSecurityInterceptorTests extends TestCase {
|
||||
throw new UnsupportedOperationException("mock method not implemented");
|
||||
}
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
public ConfigAttributeDefinition getAttributes(Method method, Class targetClass) {
|
||||
throw new UnsupportedOperationException("mock method not implemented");
|
||||
}
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
if (String.class.isAssignableFrom(clazz)) {
|
||||
return true;
|
||||
} else {
|
||||
|
||||
@@ -15,27 +15,24 @@
|
||||
|
||||
package org.springframework.security.intercept.method.aspectj;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.security.AccessDeniedException;
|
||||
import org.springframework.security.GrantedAuthority;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.MockAccessDecisionManager;
|
||||
import org.springframework.security.MockApplicationEventPublisher;
|
||||
import org.springframework.security.MockAuthenticationManager;
|
||||
import org.springframework.security.MockJoinPoint;
|
||||
import org.springframework.security.MockRunAsManager;
|
||||
import org.springframework.security.TargetObject;
|
||||
import org.springframework.security.MockApplicationEventPublisher;
|
||||
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
|
||||
import org.springframework.security.intercept.method.MethodDefinitionMap;
|
||||
import org.springframework.security.intercept.method.MapBasedMethodDefinitionSource;
|
||||
import org.springframework.security.intercept.method.MethodDefinitionSourceEditor;
|
||||
|
||||
import org.springframework.security.providers.TestingAuthenticationToken;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link AspectJSecurityInterceptor}.
|
||||
@@ -74,7 +71,7 @@ public class AspectJSecurityInterceptorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("org.springframework.security.TargetObject.countLength=MOCK_ONE,MOCK_TWO");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
si.setObjectDefinitionSource(map);
|
||||
assertEquals(map, si.getObjectDefinitionSource());
|
||||
|
||||
@@ -105,7 +102,7 @@ public class AspectJSecurityInterceptorTests extends TestCase {
|
||||
MethodDefinitionSourceEditor editor = new MethodDefinitionSourceEditor();
|
||||
editor.setAsText("org.springframework.security.TargetObject.countLength=MOCK_ONE,MOCK_TWO");
|
||||
|
||||
MethodDefinitionMap map = (MethodDefinitionMap) editor.getValue();
|
||||
MapBasedMethodDefinitionSource map = (MapBasedMethodDefinitionSource) editor.getValue();
|
||||
si.setObjectDefinitionSource(map);
|
||||
|
||||
si.afterPropertiesSet();
|
||||
|
||||
@@ -56,7 +56,7 @@ public class BasicAclEntryVoterTests extends TestCase {
|
||||
Class clazz = SomeDomainObjectManager.class;
|
||||
Method method = clazz.getMethod("someServiceMethod", new Class[] {SomeDomainObject.class});
|
||||
|
||||
return new SimpleMethodInvocation(method, new Object[] {domainObject});
|
||||
return new SimpleMethodInvocation(new SomeDomainObjectManager(), method, new Object[] {domainObject});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -419,7 +419,7 @@ public class BasicAclEntryVoterTests extends TestCase {
|
||||
Class clazz = String.class;
|
||||
Method method = clazz.getMethod("toString", new Class[]{});
|
||||
|
||||
MethodInvocation mi = new SimpleMethodInvocation(method, new Object[]{domainObject});
|
||||
MethodInvocation mi = new SimpleMethodInvocation(new String(), method, new Object[]{domainObject});
|
||||
|
||||
try {
|
||||
voter.vote(new UsernamePasswordAuthenticationToken("rod", null), mi, attr);
|
||||
|
||||
@@ -11,8 +11,8 @@ http://www.springframework.org/schema/security http://www.springframework.org/sc
|
||||
<intercept-methods>
|
||||
<!-- TODO: It would be better if we didn't need the package/interface names here -->
|
||||
<protect method="org.springframework.security.config.TestBusinessBean.set*" access="ROLE_ADMIN" />
|
||||
<protect method="org.springframework.security.config.TestBusinessBean.get*" access="ROLE_ADMIN,ROLE_USER" />
|
||||
<protect method="org.springframework.security.config.TestBusinessBean.doSomething" access="ROLE_USER" />
|
||||
<protect method="get*" access="ROLE_ADMIN,ROLE_USER" />
|
||||
<protect method="doSomething" access="ROLE_USER" />
|
||||
</intercept-methods>
|
||||
</b:bean>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user