Allow WithSecurityContextTestExecutionListener to execute after @Before

Fixes: gh-2935
This commit is contained in:
Rob Winch
2018-03-07 17:05:32 -06:00
parent 055a2ca917
commit abae2f3e87
12 changed files with 465 additions and 38 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2018 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.security.test.context.support;
import org.springframework.test.context.TestContext;
/**
* Represents the events on the methods of {@link org.springframework.test.context.TestExecutionListener}
*
* @author Rob Winch
* @since 5.1
*/
public enum TestExecutionEvent {
/**
* Associated to {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)}
* event.
*/
TEST_METHOD,
/**
* Associated to {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)}
* event.
*/
TEST_EXECUTION
}

View File

@@ -22,8 +22,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.test.context.TestContext;
/**
* When used with {@link WithSecurityContextTestExecutionListener} this
@@ -58,4 +60,13 @@ import org.springframework.security.core.context.SecurityContext;
@WithSecurityContext(factory = WithAnonymousUserSecurityContextFactory.class)
public @interface WithAnonymousUser {
/**
* Determines when the {@link SecurityContext} is setup. The default is before
* {@link TestExecutionEvent#TEST_METHOD} which occurs during
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)}
* @return the {@link TestExecutionEvent} to initialize before
* @since 5.1
*/
@AliasFor(annotation = WithSecurityContext.class)
TestExecutionEvent setupBefore() default TestExecutionEvent.TEST_METHOD;
}

View File

@@ -22,10 +22,12 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.TestContext;
import org.springframework.test.web.servlet.MockMvc;
/**
@@ -102,4 +104,14 @@ public @interface WithMockUser {
* @return
*/
String password() default "password";
}
/**
* Determines when the {@link SecurityContext} is setup. The default is before
* {@link TestExecutionEvent#TEST_METHOD} which occurs during
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)}
* @return the {@link TestExecutionEvent} to initialize before
* @since 5.1
*/
@AliasFor(annotation = WithSecurityContext.class)
TestExecutionEvent setupBefore() default TestExecutionEvent.TEST_METHOD;
}

View File

@@ -25,6 +25,7 @@ import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.test.context.TestContext;
/**
* <p>
@@ -61,4 +62,14 @@ public @interface WithSecurityContext {
* @return
*/
Class<? extends WithSecurityContextFactory<? extends Annotation>> factory();
}
/**
* Determines when the {@link SecurityContext} is setup. The default is before
* {@link TestExecutionEvent#TEST_METHOD} which occurs during
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)}
* @return the {@link TestExecutionEvent} to initialize before
* @since 5.1
*/
TestExecutionEvent setupBefore() default TestExecutionEvent.TEST_METHOD;
}

View File

@@ -20,6 +20,7 @@ import java.lang.reflect.AnnotatedElement;
import org.springframework.beans.BeanUtils;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -46,6 +47,8 @@ import org.springframework.test.web.servlet.MockMvc;
public class WithSecurityContextTestExecutionListener
extends AbstractTestExecutionListener {
static final String SECURITY_CONTEXT_ATTR_NAME = WithSecurityContextTestExecutionListener.class.getName().concat(".SECURITY_CONTEXT");
/**
* Sets up the {@link SecurityContext} for each test method. First the specific method
* is inspected for a {@link WithSecurityContext} or {@link Annotation} that has
@@ -54,46 +57,68 @@ public class WithSecurityContextTestExecutionListener
*/
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
SecurityContext securityContext = createSecurityContext(
TestSecurityContext testSecurityContext = createTestSecurityContext(
testContext.getTestMethod(), testContext);
if (securityContext == null) {
securityContext = createSecurityContext(testContext.getTestClass(),
if (testSecurityContext == null) {
testSecurityContext = createTestSecurityContext(testContext.getTestClass(),
testContext);
}
if (securityContext != null) {
if (testSecurityContext == null) {
return;
}
SecurityContext securityContext = testSecurityContext.securityContext;
if (testSecurityContext.getTestExecutionEvent() == TestExecutionEvent.TEST_METHOD) {
TestSecurityContextHolder.setContext(securityContext);
} else {
testContext.setAttribute(SECURITY_CONTEXT_ATTR_NAME, securityContext);
}
}
/**
* If configured before test execution sets the SecurityContext
* @since 5.1
*/
@Override
public void beforeTestExecution(TestContext testContext) {
SecurityContext securityContext = (SecurityContext) testContext.removeAttribute(SECURITY_CONTEXT_ATTR_NAME);
if(securityContext != null) {
TestSecurityContextHolder.setContext(securityContext);
}
}
private SecurityContext createSecurityContext(AnnotatedElement annotated,
private TestSecurityContext createTestSecurityContext(AnnotatedElement annotated,
TestContext context) {
WithSecurityContext withSecurityContext = AnnotationUtils
.findAnnotation(annotated, WithSecurityContext.class);
return createSecurityContext(annotated, withSecurityContext, context);
WithSecurityContext withSecurityContext = AnnotatedElementUtils
.findMergedAnnotation(annotated, WithSecurityContext.class);
return createTestSecurityContext(annotated, withSecurityContext, context);
}
private SecurityContext createSecurityContext(Class<?> annotated,
private TestSecurityContext createTestSecurityContext(Class<?> annotated,
TestContext context) {
MetaAnnotationUtils.AnnotationDescriptor<WithSecurityContext> withSecurityContextDescriptor = MetaAnnotationUtils
.findAnnotationDescriptor(annotated, WithSecurityContext.class);
WithSecurityContext withSecurityContext = withSecurityContextDescriptor == null
? null : withSecurityContextDescriptor.getAnnotation();
return createSecurityContext(annotated, withSecurityContext, context);
return createTestSecurityContext(annotated, withSecurityContext, context);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private SecurityContext createSecurityContext(AnnotatedElement annotated,
private TestSecurityContext createTestSecurityContext(AnnotatedElement annotated,
WithSecurityContext withSecurityContext, TestContext context) {
if (withSecurityContext == null) {
return null;
}
withSecurityContext = AnnotationUtils
.synthesizeAnnotation(withSecurityContext, annotated);
WithSecurityContextFactory factory = createFactory(withSecurityContext, context);
Class<? extends Annotation> type = (Class<? extends Annotation>) GenericTypeResolver
.resolveTypeArgument(factory.getClass(),
WithSecurityContextFactory.class);
Annotation annotation = findAnnotation(annotated, type);
TestExecutionEvent initialize = withSecurityContext.setupBefore();
try {
return factory.createSecurityContext(annotation);
return new TestSecurityContext(factory.createSecurityContext(annotation), initialize);
}
catch (RuntimeException e) {
throw new IllegalStateException(
@@ -150,4 +175,22 @@ public class WithSecurityContextTestExecutionListener
public int getOrder() {
return 10000;
}
static class TestSecurityContext {
private final SecurityContext securityContext;
private final TestExecutionEvent testExecutionEvent;
TestSecurityContext(SecurityContext securityContext, TestExecutionEvent testExecutionEvent) {
this.securityContext = securityContext;
this.testExecutionEvent = testExecutionEvent;
}
public SecurityContext getSecurityContext() {
return this.securityContext;
}
public TestExecutionEvent getTestExecutionEvent() {
return this.testExecutionEvent;
}
}
}

View File

@@ -22,11 +22,13 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.test.context.TestContext;
import org.springframework.test.web.servlet.MockMvc;
/**
@@ -69,4 +71,14 @@ public @interface WithUserDetails {
* @since 4.1
*/
String userDetailsServiceBeanName() default "";
}
/**
* Determines when the {@link SecurityContext} is setup. The default is before
* {@link TestExecutionEvent#TEST_METHOD} which occurs during
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)}
* @return the {@link TestExecutionEvent} to initialize before
* @since 5.1
*/
@AliasFor(annotation = WithSecurityContext.class)
TestExecutionEvent setupBefore() default TestExecutionEvent.TEST_METHOD;
}