Search in sources :

Example 41 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class MethodInvocationPrivilegeEvaluatorTests method allowsAccessUsingCreate.

@Test
public void allowsAccessUsingCreate() throws Exception {
    Object object = new TargetObject();
    final MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", "foobar");
    MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
    given(this.mds.getAttributes(mi)).willReturn(this.role);
    mipe.setSecurityInterceptor(this.interceptor);
    mipe.afterPropertiesSet();
    assertThat(mipe.isAllowed(mi, this.token)).isTrue();
}
Also used : MethodInvocationPrivilegeEvaluator(org.springframework.security.access.intercept.MethodInvocationPrivilegeEvaluator) ITargetObject(org.springframework.security.ITargetObject) OtherTargetObject(org.springframework.security.OtherTargetObject) TargetObject(org.springframework.security.TargetObject) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ITargetObject(org.springframework.security.ITargetObject) OtherTargetObject(org.springframework.security.OtherTargetObject) TargetObject(org.springframework.security.TargetObject) Test(org.junit.jupiter.api.Test)

Example 42 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class MethodInvocationPrivilegeEvaluatorTests method allowsAccessUsingCreateFromClass.

@Test
public void allowsAccessUsingCreateFromClass() {
    final MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase", new Class[] { String.class }, new Object[] { "Hello world" });
    MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
    mipe.setSecurityInterceptor(this.interceptor);
    given(this.mds.getAttributes(mi)).willReturn(this.role);
    assertThat(mipe.isAllowed(mi, this.token)).isTrue();
}
Also used : MethodInvocationPrivilegeEvaluator(org.springframework.security.access.intercept.MethodInvocationPrivilegeEvaluator) MethodInvocation(org.aopalliance.intercept.MethodInvocation) OtherTargetObject(org.springframework.security.OtherTargetObject) Test(org.junit.jupiter.api.Test)

Example 43 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class MethodInvocationPrivilegeEvaluatorTests method declinesAccessUsingCreateFromClass.

@Test
public void declinesAccessUsingCreateFromClass() {
    final MethodInvocation mi = MethodInvocationUtils.createFromClass(new OtherTargetObject(), ITargetObject.class, "makeLowerCase", new Class[] { String.class }, new Object[] { "helloWorld" });
    MethodInvocationPrivilegeEvaluator mipe = new MethodInvocationPrivilegeEvaluator();
    mipe.setSecurityInterceptor(this.interceptor);
    given(this.mds.getAttributes(mi)).willReturn(this.role);
    willThrow(new AccessDeniedException("rejected")).given(this.adm).decide(this.token, mi, this.role);
    assertThat(mipe.isAllowed(mi, this.token)).isFalse();
}
Also used : MethodInvocationPrivilegeEvaluator(org.springframework.security.access.intercept.MethodInvocationPrivilegeEvaluator) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodInvocation(org.aopalliance.intercept.MethodInvocation) OtherTargetObject(org.springframework.security.OtherTargetObject) Test(org.junit.jupiter.api.Test)

Example 44 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class DelegatingMethodSecurityMetadataSourceTests method returnsDelegateAttributes.

@Test
public void returnsDelegateAttributes() throws Exception {
    List sources = new ArrayList();
    MethodSecurityMetadataSource delegate = mock(MethodSecurityMetadataSource.class);
    ConfigAttribute ca = mock(ConfigAttribute.class);
    List attributes = Arrays.asList(ca);
    Method toString = String.class.getMethod("toString");
    given(delegate.getAttributes(toString, String.class)).willReturn(attributes);
    sources.add(delegate);
    this.mds = new DelegatingMethodSecurityMetadataSource(sources);
    assertThat(this.mds.getMethodSecurityMetadataSources()).isSameAs(sources);
    assertThat(this.mds.getAllConfigAttributes().isEmpty()).isTrue();
    MethodInvocation mi = new SimpleMethodInvocation("", toString);
    assertThat(this.mds.getAttributes(mi)).isSameAs(attributes);
    // Exercise the cached case
    assertThat(this.mds.getAttributes(mi)).isSameAs(attributes);
    assertThat(this.mds.getAttributes(new SimpleMethodInvocation(null, String.class.getMethod("length")))).isEmpty();
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 45 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class DelegatingMethodSecurityMetadataSourceTests method returnsEmptyListIfDelegateReturnsNull.

@Test
public void returnsEmptyListIfDelegateReturnsNull() throws Exception {
    List sources = new ArrayList();
    MethodSecurityMetadataSource delegate = mock(MethodSecurityMetadataSource.class);
    given(delegate.getAttributes(ArgumentMatchers.<Method>any(), ArgumentMatchers.any(Class.class))).willReturn(null);
    sources.add(delegate);
    this.mds = new DelegatingMethodSecurityMetadataSource(sources);
    assertThat(this.mds.getMethodSecurityMetadataSources()).isSameAs(sources);
    assertThat(this.mds.getAllConfigAttributes().isEmpty()).isTrue();
    MethodInvocation mi = new SimpleMethodInvocation(null, String.class.getMethod("toString"));
    assertThat(this.mds.getAttributes(mi)).isEqualTo(Collections.emptyList());
    // Exercise the cached case
    assertThat(this.mds.getAttributes(mi)).isEqualTo(Collections.emptyList());
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)117 Test (org.junit.jupiter.api.Test)50 Test (org.junit.Test)35 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)25 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 Method (java.lang.reflect.Method)21 ArrayList (java.util.ArrayList)11 Log (org.apache.commons.logging.Log)11 Authentication (org.springframework.security.core.Authentication)10 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 List (java.util.List)7 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 Promise (ratpack.exec.Promise)5