Remove JSR-330 configuration annotations

Replace JSR-330 validation annotations from all internal
`@ConfigurationProperties` classes with standard Asserts.

Prior to this commit validation of our own configuration properties
would only occur when the user happens to have compliant JSR-330
implementation on their classpath.

See gh-7579
This commit is contained in:
Phillip Webb
2017-01-18 20:32:46 -08:00
parent 0a6456a748
commit f42ebe428c
9 changed files with 69 additions and 27 deletions

View File

@@ -20,8 +20,8 @@ import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import org.springframework.boot.autoconfigure.security.SecurityPrerequisite;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
@@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.embedded.Ssl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -78,7 +79,6 @@ public class ManagementServerProperties implements SecurityPrerequisite {
/**
* Management endpoint context-path.
*/
@NotNull
private String contextPath = "";
/**
@@ -88,6 +88,11 @@ public class ManagementServerProperties implements SecurityPrerequisite {
private final Security security = new Security();
@PostConstruct
private void validate() {
Assert.notNull(this.contextPath, "ContextPath must not be null");
}
/**
* Returns the management port or {@code null} if the
* {@link ServerProperties#getPort() server port} should be used.

View File

@@ -16,11 +16,13 @@
package org.springframework.boot.actuate.endpoint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
/**
* Abstract base for {@link Endpoint} implementations.
@@ -31,14 +33,14 @@ import org.springframework.core.env.Environment;
*/
public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAware {
private static final Pattern ID_PATTERN = Pattern.compile("\\w+");
private Environment environment;
/**
* Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped
* to a URL (e.g. 'foo' is mapped to '/foo').
*/
@NotNull
@Pattern(regexp = "\\w+", message = "ID must only contains letters, numbers and '_'")
private String id;
private final boolean sensitiveDefault;
@@ -53,6 +55,13 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
*/
private Boolean enabled;
@PostConstruct
private void validate() {
Assert.notNull(this.id, "Id must not be null");
Assert.isTrue(ID_PATTERN.matcher(this.id).matches(),
"ID must only contains letters, numbers and '_'");
}
/**
* Create a new sensitive endpoint instance. The endpoint will enabled flag will be
* based on the spring {@link Environment} unless explicitly set.

View File

@@ -62,9 +62,11 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) {
try {
DataSource dataSource = entry.getValue().getDataSource();
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
JdbcConnection connection = new JdbcConnection(
dataSource.getConnection());
try {
Database database = factory.findCorrectDatabaseImplementation(connection);
Database database = factory
.findCorrectDatabaseImplementation(connection);
reports.add(new LiquibaseReport(entry.getKey(),
service.queryDatabaseChangeLogTable(database)));
}

View File

@@ -16,13 +16,13 @@
package org.springframework.boot.actuate.endpoint.mvc;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.annotation.PostConstruct;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
@@ -41,8 +41,6 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
/**
* Endpoint URL path.
*/
@NotNull
@Pattern(regexp = "/.*|^$", message = "Path must start with / or be empty")
private String path;
/**
@@ -68,6 +66,13 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
this.enabled = enabled;
}
@PostConstruct
private void validate() {
Assert.notNull(this.path, "Path must not be null");
Assert.isTrue(this.path.length() == 0 || this.path.startsWith("/"),
"Path must start with / or be empty");
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;