Search in sources :

Example 36 with MethodInvocation

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

the class MethodExpressionVoterTests method collectionPreFilteringIsSuccessful.

@Test
public void collectionPreFilteringIsSuccessful() throws Exception {
    List arg = createCollectionArg("joe", "bob", "sam");
    MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingACollection(), arg);
    this.am.vote(this.joe, mi, createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe' or filterObject == 'sam')", "collection", "permitAll")));
    assertThat(arg).containsExactly("joe", "sam");
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) 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)

Example 37 with MethodInvocation

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

the class MethodExpressionVoterTests method ruleDefinedInAClassMethodIsApplied.

@Test
public void ruleDefinedInAClassMethodIsApplied() throws Exception {
    MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAString(), "joe");
    assertThat(this.am.vote(this.joe, mi, createAttributes(new PreInvocationExpressionAttribute(null, null, "T(org.springframework.security.access.expression.method.SecurityRules).isJoe(#argument)")))).isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 38 with MethodInvocation

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

the class MethodExpressionVoterTests method arraysCannotBePrefiltered.

@Test
public void arraysCannotBePrefiltered() throws Exception {
    MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAnArray(), createArrayArg("sam", "joe"));
    assertThatIllegalArgumentException().isThrownBy(() -> this.am.vote(this.joe, mi, createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'jim')", "someArray", null))));
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 39 with MethodInvocation

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

the class MethodSecurityInterceptorTests method afterInvocationManagerIsNotInvokedIfExceptionIsRaised.

@Test
public void afterInvocationManagerIsNotInvokedIfExceptionIsRaised() throws Throwable {
    MethodInvocation mi = mock(MethodInvocation.class);
    this.token.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(this.token);
    mdsReturnsUserRole();
    AfterInvocationManager aim = mock(AfterInvocationManager.class);
    this.interceptor.setAfterInvocationManager(aim);
    given(mi.proceed()).willThrow(new Throwable());
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> this.interceptor.invoke(mi));
    verifyZeroInteractions(aim);
}
Also used : AfterInvocationManager(org.springframework.security.access.intercept.AfterInvocationManager) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 40 with MethodInvocation

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

the class MethodInvocationPrivilegeEvaluatorTests method declinesAccessUsingCreate.

@Test
public void declinesAccessUsingCreate() {
    Object object = new TargetObject();
    final MethodInvocation mi = MethodInvocationUtils.create(object, "makeLowerCase", "foobar");
    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) 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)

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