This commit is contained in:
Phillip Webb
2015-06-04 00:37:15 -07:00
parent fca192fa41
commit 412b7b9e50
47 changed files with 272 additions and 242 deletions

View File

@@ -20,8 +20,6 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.CollectionUtils;
import org.springframework.web.cors.CorsConfiguration;
/**
* Configuration properties for MVC endpoints' CORS support.
@@ -30,23 +28,22 @@ import org.springframework.web.cors.CorsConfiguration;
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "endpoints.cors")
public class MvcEndpointCorsProperties {
public class EndpointCorsProperties {
/**
* Comma-separated list of origins to allow. '*' allows all origins. When
* not set, CORS support is disabled.
* Comma-separated list of origins to allow. '*' allows all origins. When not set,
* CORS support is disabled.
*/
private List<String> allowedOrigins = new ArrayList<String>();
/**
* Comma-separated list of methods to allow. '*' allows all methods. When
* not set, defaults to GET.
* Comma-separated list of methods to allow. '*' allows all methods. When not set,
* defaults to GET.
*/
private List<String> allowedMethods = new ArrayList<String>();
/**
* Comma-separated list of headers to allow in a request. '*' allows all
* headers.
* Comma-separated list of headers to allow in a request. '*' allows all headers.
*/
private List<String> allowedHeaders = new ArrayList<String>();
@@ -114,28 +111,4 @@ public class MvcEndpointCorsProperties {
this.maxAge = maxAge;
}
CorsConfiguration toCorsConfiguration() {
if (CollectionUtils.isEmpty(this.allowedOrigins)) {
return null;
}
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedOrigins(this.allowedOrigins);
if (!CollectionUtils.isEmpty(this.allowedHeaders)) {
corsConfiguration.setAllowedHeaders(this.allowedHeaders);
}
if (!CollectionUtils.isEmpty(this.allowedMethods)) {
corsConfiguration.setAllowedMethods(this.allowedMethods);
}
if (!CollectionUtils.isEmpty(this.exposedHeaders)) {
corsConfiguration.setExposedHeaders(this.exposedHeaders);
}
if (this.maxAge != null) {
corsConfiguration.setMaxAge(this.maxAge);
}
if (this.allowCredentials != null) {
corsConfiguration.setAllowCredentials(this.allowCredentials);
}
return corsConfiguration;
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -45,6 +46,7 @@ import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCusto
import org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
import org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -71,7 +73,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.DispatcherServlet;
@@ -94,7 +98,7 @@ import org.springframework.web.servlet.DispatcherServlet;
EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class })
@EnableConfigurationProperties({ HealthMvcEndpointProperties.class,
MvcEndpointCorsProperties.class })
EndpointCorsProperties.class })
public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
SmartInitializingSingleton {
@@ -109,7 +113,7 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
private ManagementServerProperties managementServerProperties;
@Autowired
private MvcEndpointCorsProperties corsMvcEndpointProperties;
private EndpointCorsProperties corsProperties;
@Autowired(required = false)
private List<EndpointHandlerMappingCustomizer> mappingCustomizers;
@@ -123,8 +127,10 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
@Bean
@ConditionalOnMissingBean
public EndpointHandlerMapping endpointHandlerMapping() {
EndpointHandlerMapping mapping = new EndpointHandlerMapping(mvcEndpoints()
.getEndpoints(), this.corsMvcEndpointProperties.toCorsConfiguration());
Set<? extends MvcEndpoint> endpoints = mvcEndpoints().getEndpoints();
CorsConfiguration corsConfiguration = getCorsConfiguration(this.corsProperties);
EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints,
corsConfiguration);
boolean disabled = ManagementServerPort.get(this.applicationContext) != ManagementServerPort.SAME;
mapping.setDisabled(disabled);
if (!disabled) {
@@ -138,6 +144,30 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
return mapping;
}
private CorsConfiguration getCorsConfiguration(EndpointCorsProperties properties) {
if (CollectionUtils.isEmpty(properties.getAllowedOrigins())) {
return null;
}
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(properties.getAllowedOrigins());
if (!CollectionUtils.isEmpty(properties.getAllowedHeaders())) {
configuration.setAllowedHeaders(properties.getAllowedHeaders());
}
if (!CollectionUtils.isEmpty(properties.getAllowedMethods())) {
configuration.setAllowedMethods(properties.getAllowedMethods());
}
if (!CollectionUtils.isEmpty(properties.getExposedHeaders())) {
configuration.setExposedHeaders(properties.getExposedHeaders());
}
if (properties.getMaxAge() != null) {
configuration.setMaxAge(properties.getMaxAge());
}
if (properties.getAllowCredentials() != null) {
configuration.setAllowCredentials(properties.getAllowCredentials());
}
return configuration;
}
@Override
public void afterSingletonsInstantiated() {
ManagementServerPort managementPort = ManagementServerPort

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -19,7 +19,8 @@ package org.springframework.boot.actuate;
import org.springframework.boot.test.AbstractConfigurationClassTests;
/**
* Tests for the actuator module's @Configuration classes
* Tests for the actuator module's {@code @Configuration} classes.
*
* @author Andy Wilkinson
*/
public class ActuatorConfigurationClassTests extends AbstractConfigurationClassTests {

View File

@@ -52,18 +52,15 @@ public class SpringApplicationHierarchyTests {
"--server.port=0");
}
@EnableAutoConfiguration(exclude = {
ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class},
excludeName = {"org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration"})
@EnableAutoConfiguration(exclude = { ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class }, excludeName = { "org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration" })
public static class Child {
}
@EnableAutoConfiguration(exclude = {JolokiaAutoConfiguration.class,
@EnableAutoConfiguration(exclude = { JolokiaAutoConfiguration.class,
EndpointMBeanExportAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class},
excludeName = {"org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration"})
ElasticsearchRepositoriesAutoConfiguration.class }, excludeName = { "org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration" })
public static class Parent {
}