1
0
mirror of synced 2026-08-04 09:17:02 +00:00

WithSecurityContextTestExecutionListener supports generic Annotation

Previously Spring Security's WithSecurityContextTestExecutionListener
allowed a WithSecurityContextFactory<Annotation> to be used. This
was broken in SEC-3074.

This commit ensures that WithSecurityContextFactory<Annotation> is
supported again.

Fixes gh-3837
This commit is contained in:
Rob Winch
2016-05-03 13:07:45 -05:00
committed by Joe Grandja
parent 04a12f49b1
commit 78bf6e2bd5
2 changed files with 79 additions and 6 deletions
@@ -71,7 +71,7 @@ public class WithSecurityContextTestExecutionListener extends
if (withSecurityContext != null) {
WithSecurityContextFactory factory = createFactory(withSecurityContext, context);
Class<? extends Annotation> type = (Class<? extends Annotation>) GenericTypeResolver.resolveTypeArgument(factory.getClass(), WithSecurityContextFactory.class);
Annotation annotation = AnnotationUtils.findAnnotation(annotated, type);
Annotation annotation = findAnnotation(annotated, type);
try {
return factory.createSecurityContext(annotation);
}
@@ -83,6 +83,23 @@ public class WithSecurityContextTestExecutionListener extends
return null;
}
private Annotation findAnnotation(AnnotatedElement annotated,
Class<? extends Annotation> type) {
Annotation findAnnotation = AnnotationUtils.findAnnotation(annotated, type);
if (findAnnotation != null) {
return findAnnotation;
}
Annotation[] allAnnotations = AnnotationUtils.getAnnotations(annotated);
for (Annotation annotationToTest : allAnnotations) {
WithSecurityContext withSecurityContext = AnnotationUtils.findAnnotation(
annotationToTest.annotationType(), WithSecurityContext.class);
if (withSecurityContext != null) {
return annotationToTest;
}
}
return null;
}
private WithSecurityContextFactory<? extends Annotation> createFactory(
WithSecurityContext withSecurityContext, TestContext testContext) {
Class<? extends WithSecurityContextFactory<? extends Annotation>> clazz = withSecurityContext