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:
+18
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user