Remove secure flag from AutoConfigureMockMvc

Closes gh-13822
This commit is contained in:
Madhura Bhave
2019-02-26 12:24:10 -08:00
parent cd13a1685c
commit 64b7466d51
8 changed files with 17 additions and 113 deletions

View File

@@ -85,13 +85,4 @@ public @interface AutoConfigureMockMvc {
@PropertyMapping("webdriver.enabled")
boolean webDriverEnabled() default true;
/**
* If Spring Security's {@link MockMvc} support should be auto-configured when it is
* on the classpath. Defaults to {@code true}.
* @return if Spring Security's MockMvc support is auto-configured
* @deprecated since 2.1.0 in favor of Spring Security's testing support
*/
@Deprecated
boolean secure() default true;
}

View File

@@ -1,37 +0,0 @@
/*
* 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.
* 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.boot.test.autoconfigure.web.servlet;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Auto-configuration for Spring Security's testing support.
*
* @author Andy Wilkinson
* @since 1.4.0
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.test.mockmvc", name = "secure", havingValue = "true", matchIfMissing = true)
@Import({ SecurityAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class,
MockMvcSecurityConfiguration.class })
public class MockMvcSecurityAutoConfiguration {
}

View File

@@ -139,18 +139,6 @@ public @interface WebMvcTest {
*/
Filter[] excludeFilters() default {};
/**
* If Spring Security's {@link MockMvc} support should be auto-configured when it is
* on the classpath. Also determines if
* {@link org.springframework.security.config.annotation.web.WebSecurityConfigurer}
* classes should be included in the application context. Defaults to {@code true}.
* @return if Spring Security's MockMvc support is auto-configured
* @deprecated since 2.1.0 in favor of Spring Security's testing support
*/
@Deprecated
@AliasFor(annotation = AutoConfigureMockMvc.class)
boolean secure() default true;
/**
* Auto-configuration exclusions that should be applied for this test.
* @return auto-configuration exclusions to apply

View File

@@ -47,7 +47,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
*/
class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
private static final String SECURITY_CONFIGURER = "org.springframework.security.config.annotation.web.WebSecurityConfigurer";
private static final String[] OPTIONAL_INCLUDES = {
"org.springframework.security.config.annotation.web.WebSecurityConfigurer" };
private static final Set<Class<?>> DEFAULT_INCLUDES;
@@ -64,21 +65,17 @@ 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) {
// Ignore
}
}
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
}
private static final Set<Class<?>> DEFAULT_INCLUDES_AND_SECURITY_CONFIGURER;
static {
Set<Class<?>> includes = new LinkedHashSet<>(DEFAULT_INCLUDES);
try {
includes.add(ClassUtils.forName(SECURITY_CONFIGURER, null));
}
catch (Exception ex) {
}
DEFAULT_INCLUDES_AND_SECURITY_CONFIGURER = Collections.unmodifiableSet(includes);
}
private static final Set<Class<?>> DEFAULT_INCLUDES_AND_CONTROLLER;
static {
@@ -87,16 +84,6 @@ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
DEFAULT_INCLUDES_AND_CONTROLLER = Collections.unmodifiableSet(includes);
}
private static final Set<Class<?>> DEFAULT_INCLUDES_SECURITY_CONFIGURER_AND_CONTROLLER;
static {
Set<Class<?>> includes = new LinkedHashSet<>(
DEFAULT_INCLUDES_AND_SECURITY_CONFIGURER);
includes.add(Controller.class);
DEFAULT_INCLUDES_SECURITY_CONFIGURER_AND_CONTROLLER = Collections
.unmodifiableSet(includes);
}
private final WebMvcTest annotation;
WebMvcTypeExcludeFilter(Class<?> testClass) {
@@ -128,12 +115,6 @@ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
@Override
@SuppressWarnings("deprecation")
protected Set<Class<?>> getDefaultIncludes() {
if (this.annotation.secure()) {
if (ObjectUtils.isEmpty(this.annotation.controllers())) {
return DEFAULT_INCLUDES_SECURITY_CONFIGURER_AND_CONTROLLER;
}
return DEFAULT_INCLUDES_AND_SECURITY_CONFIGURER;
}
if (ObjectUtils.isEmpty(this.annotation.controllers())) {
return DEFAULT_INCLUDES_AND_CONTROLLER;
}

View File

@@ -102,9 +102,11 @@ org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration
# AutoConfigureMockMvc auto-configuration imports
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityConfiguration
# AutoConfigureMockRestServiceServer
org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer=\

View File

@@ -20,7 +20,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -34,14 +33,14 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Integration tests for {@link MockMvcSecurityAutoConfiguration}.
* Integration tests for MockMvc security.
*
* @author Andy Wilkinson
*/
@WebMvcTest
@RunWith(SpringRunner.class)
@TestPropertySource(properties = { "debug=true" })
public class MockMvcSecurityAutoConfigurationIntegrationTests {
public class MockMvcSecurityIntegrationTests {
@Autowired
private MockMvc mockMvc;

View File

@@ -17,13 +17,12 @@
package org.springframework.boot.test.autoconfigure.security;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Tests application for {@link MockMvcSecurityAutoConfiguration}.
* Tests application for MockMvc Security.
*
* @author Andy Wilkinson
*/

View File

@@ -113,20 +113,6 @@ public class WebMvcTypeExcludeFilterTests {
assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse();
}
@Test
public void matchWhenSecureFalse() throws Exception {
WebMvcTypeExcludeFilter filter = new WebMvcTypeExcludeFilter(
WithSecureFalse.class);
assertThat(excludes(filter, Controller1.class)).isFalse();
assertThat(excludes(filter, Controller2.class)).isFalse();
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
assertThat(excludes(filter, ExampleWeb.class)).isFalse();
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
assertThat(excludes(filter, ExampleService.class)).isTrue();
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isTrue();
}
private boolean excludes(WebMvcTypeExcludeFilter filter, Class<?> type)
throws IOException {
MetadataReader metadataReader = this.metadataReaderFactory
@@ -159,11 +145,6 @@ public class WebMvcTypeExcludeFilterTests {
}
@WebMvcTest(secure = false)
static class WithSecureFalse {
}
@Controller
static class Controller1 {