Search in sources :

Example 46 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class Jsr250MethodSecurityMetadataSourceTests method classLevelAnnotationsIgnoredByExplicitMemberAnnotation.

@Test
public void classLevelAnnotationsIgnoredByExplicitMemberAnnotation() throws Exception {
    Child target = new Child();
    MockMethodInvocation mi = new MockMethodInvocation(target, target.getClass(), "explicitMethod");
    Collection<ConfigAttribute> accessAttributes = this.mds.getAttributes(mi);
    assertThat(accessAttributes).hasSize(1);
    assertThat(accessAttributes.toArray()[0].toString()).isEqualTo("ROLE_EXPLICIT");
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Example 47 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class Jsr250MethodSecurityMetadataSourceTests method classLevelAnnotationsOnlyAffectTheClassTheyAnnotateAndTheirMembers.

// JSR-250 Spec Tests
/**
 * Class-level annotations only affect the class they annotate and their members, that
 * is, its methods and fields. They never affect a member declared by a superclass,
 * even if it is not hidden or overridden by the class in question.
 * @throws Exception
 */
@Test
public void classLevelAnnotationsOnlyAffectTheClassTheyAnnotateAndTheirMembers() throws Exception {
    Child target = new Child();
    MockMethodInvocation mi = new MockMethodInvocation(target, target.getClass(), "notOverriden");
    Collection<ConfigAttribute> accessAttributes = this.mds.getAttributes(mi);
    assertThat(accessAttributes).isNull();
}
Also used : ConfigAttribute(org.springframework.security.access.ConfigAttribute) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) Test(org.junit.jupiter.api.Test)

Example 48 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class ReactiveMethodSecurityConfigurationTests method rolePrefixWithGrantedAuthorityDefaults.

@Test
public void rolePrefixWithGrantedAuthorityDefaults() throws NoSuchMethodException {
    this.spring.register(WithRolePrefixConfiguration.class).autowire();
    TestingAuthenticationToken authentication = new TestingAuthenticationToken("principal", "credential", "CUSTOM_ABC");
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new Foo(), Foo.class, "bar", String.class);
    EvaluationContext context = this.methodSecurityExpressionHandler.createEvaluationContext(authentication, methodInvocation);
    SecurityExpressionRoot root = (SecurityExpressionRoot) context.getRootObject().getValue();
    assertThat(root.hasRole("ROLE_ABC")).isFalse();
    assertThat(root.hasRole("ROLE_CUSTOM_ABC")).isFalse();
    assertThat(root.hasRole("CUSTOM_ABC")).isTrue();
    assertThat(root.hasRole("ABC")).isTrue();
}
Also used : MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) EvaluationContext(org.springframework.expression.EvaluationContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) SecurityExpressionRoot(org.springframework.security.access.expression.SecurityExpressionRoot) Test(org.junit.jupiter.api.Test)

Example 49 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class PostAuthorizeAuthorizationManagerTests method checkDoSomethingListWhenReturnObjectContainsGrantThenGrantedDecision.

@Test
public void checkDoSomethingListWhenReturnObjectContainsGrantThenGrantedDecision() throws Exception {
    List<String> list = Arrays.asList("grant", "deny");
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "doSomethingList", new Class[] { List.class }, new Object[] { list });
    MethodInvocationResult result = new MethodInvocationResult(methodInvocation, list);
    PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, result);
    assertThat(decision).isNotNull();
    assertThat(decision.isGranted()).isTrue();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Example 50 with MockMethodInvocation

use of org.springframework.security.access.intercept.method.MockMethodInvocation in project spring-security by spring-projects.

the class PostAuthorizeAuthorizationManagerTests method checkDoSomethingStringWhenArgIsGrantThenGrantedDecision.

@Test
public void checkDoSomethingStringWhenArgIsGrantThenGrantedDecision() throws Exception {
    MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class, "doSomethingString", new Class[] { String.class }, new Object[] { "grant" });
    PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager();
    MethodInvocationResult result = new MethodInvocationResult(methodInvocation, null);
    AuthorizationDecision decision = manager.check(TestAuthentication::authenticatedUser, result);
    assertThat(decision).isNotNull();
    assertThat(decision.isGranted()).isTrue();
}
Also used : AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) MockMethodInvocation(org.springframework.security.access.intercept.method.MockMethodInvocation) TestAuthentication(org.springframework.security.authentication.TestAuthentication) Test(org.junit.jupiter.api.Test)

Aggregations

MockMethodInvocation (org.springframework.security.access.intercept.method.MockMethodInvocation)72 Test (org.junit.jupiter.api.Test)70 TestAuthentication (org.springframework.security.authentication.TestAuthentication)36 AuthorizationDecision (org.springframework.security.authorization.AuthorizationDecision)27 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)23 Authentication (org.springframework.security.core.Authentication)20 AnnotationConfigurationException (org.springframework.core.annotation.AnnotationConfigurationException)13 ConfigAttribute (org.springframework.security.access.ConfigAttribute)12 PreInvocationAttribute (org.springframework.security.access.prepost.PreInvocationAttribute)7 EvaluationContext (org.springframework.expression.EvaluationContext)3 SecurityExpressionRoot (org.springframework.security.access.expression.SecurityExpressionRoot)3 ArrayList (java.util.ArrayList)2 List (java.util.List)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 Expression (org.springframework.expression.Expression)1