From eef6fdb0664526f027c7fb59eda05ff1acebce07 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 27 Feb 2018 21:01:13 -0800 Subject: [PATCH] Include WebSecurityConfigurer beans in @WebMvcTest Update `WebMvcTypeExcludeFilter` to include `WebSecurityConfigurer` beans. Fixes gh-12275 --- .../web/servlet/WebMvcTypeExcludeFilter.java | 13 ++++++++++++- .../web/servlet/WebMvcTypeExcludeFilterTests.java | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java index f8cda83e81e..10c5463a6da 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-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. @@ -33,6 +33,7 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.GenericConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.stereotype.Controller; +import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.method.support.HandlerMethodArgumentResolver; @@ -45,6 +46,9 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; */ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { + private static final String[] OPTIONAL_INCLUDES = { + "org.springframework.security.config.annotation.web.WebSecurityConfigurer" }; + private static final Set> DEFAULT_INCLUDES; static { @@ -60,6 +64,13 @@ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { includes.add(ErrorAttributes.class); includes.add(Converter.class); includes.add(GenericConverter.class); + for (String optionalInclude : OPTIONAL_INCLUDES) { + try { + includes.add(ClassUtils.forName(optionalInclude, null)); + } + catch (Exception ex) { + } + } DEFAULT_INCLUDES = Collections.unmodifiableSet(includes); } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java index b11acf59b85..105b0dcf116 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-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. @@ -26,6 +26,7 @@ import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; @@ -54,6 +55,7 @@ public class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); + assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse(); } @Test @@ -67,6 +69,7 @@ public class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); + assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse(); } @Test @@ -80,6 +83,7 @@ public class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, ExampleMessageConverter.class)).isTrue(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); + assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isTrue(); } @Test @@ -106,6 +110,7 @@ public class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); + assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse(); } private boolean excludes(WebMvcTypeExcludeFilter filter, Class type) @@ -173,4 +178,8 @@ public class WebMvcTypeExcludeFilterTests { } + static class ExampleWebSecurityConfigurer extends WebSecurityConfigurerAdapter { + + } + }