Commit f42ebe42 authored by Phillip Webb's avatar Phillip Webb

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
parent 0a6456a7
...@@ -20,8 +20,8 @@ import java.net.InetAddress; ...@@ -20,8 +20,8 @@ import java.net.InetAddress;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import org.springframework.boot.autoconfigure.security.SecurityPrerequisite; import org.springframework.boot.autoconfigure.security.SecurityPrerequisite;
import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties;
...@@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; ...@@ -29,6 +29,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.embedded.Ssl; import org.springframework.boot.context.embedded.Ssl;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -78,7 +79,6 @@ public class ManagementServerProperties implements SecurityPrerequisite { ...@@ -78,7 +79,6 @@ public class ManagementServerProperties implements SecurityPrerequisite {
/** /**
* Management endpoint context-path. * Management endpoint context-path.
*/ */
@NotNull
private String contextPath = ""; private String contextPath = "";
/** /**
...@@ -88,6 +88,11 @@ public class ManagementServerProperties implements SecurityPrerequisite { ...@@ -88,6 +88,11 @@ public class ManagementServerProperties implements SecurityPrerequisite {
private final Security security = new Security(); 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 * Returns the management port or {@code null} if the
* {@link ServerProperties#getPort() server port} should be used. * {@link ServerProperties#getPort() server port} should be used.
......
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
package org.springframework.boot.actuate.endpoint; package org.springframework.boot.actuate.endpoint;
import javax.validation.constraints.NotNull; import java.util.regex.Pattern;
import javax.validation.constraints.Pattern;
import javax.annotation.PostConstruct;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
/** /**
* Abstract base for {@link Endpoint} implementations. * Abstract base for {@link Endpoint} implementations.
...@@ -31,14 +33,14 @@ import org.springframework.core.env.Environment; ...@@ -31,14 +33,14 @@ import org.springframework.core.env.Environment;
*/ */
public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAware { public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAware {
private static final Pattern ID_PATTERN = Pattern.compile("\\w+");
private Environment environment; private Environment environment;
/** /**
* Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped * Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped
* to a URL (e.g. 'foo' is mapped to '/foo'). * 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 String id;
private final boolean sensitiveDefault; private final boolean sensitiveDefault;
...@@ -53,6 +55,13 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa ...@@ -53,6 +55,13 @@ public abstract class AbstractEndpoint<T> implements Endpoint<T>, EnvironmentAwa
*/ */
private Boolean enabled; 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 * Create a new sensitive endpoint instance. The endpoint will enabled flag will be
* based on the spring {@link Environment} unless explicitly set. * based on the spring {@link Environment} unless explicitly set.
......
...@@ -62,9 +62,11 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> { ...@@ -62,9 +62,11 @@ public class LiquibaseEndpoint extends AbstractEndpoint<List<LiquibaseReport>> {
for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) { for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) {
try { try {
DataSource dataSource = entry.getValue().getDataSource(); DataSource dataSource = entry.getValue().getDataSource();
JdbcConnection connection = new JdbcConnection(dataSource.getConnection()); JdbcConnection connection = new JdbcConnection(
dataSource.getConnection());
try { try {
Database database = factory.findCorrectDatabaseImplementation(connection); Database database = factory
.findCorrectDatabaseImplementation(connection);
reports.add(new LiquibaseReport(entry.getKey(), reports.add(new LiquibaseReport(entry.getKey(),
service.queryDatabaseChangeLogTable(database))); service.queryDatabaseChangeLogTable(database)));
} }
......
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
package org.springframework.boot.actuate.endpoint.mvc; package org.springframework.boot.actuate.endpoint.mvc;
import javax.validation.constraints.NotNull; import javax.annotation.PostConstruct;
import javax.validation.constraints.Pattern;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.EndpointProperties; import org.springframework.boot.actuate.endpoint.EndpointProperties;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** /**
...@@ -41,8 +41,6 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter ...@@ -41,8 +41,6 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
/** /**
* Endpoint URL path. * Endpoint URL path.
*/ */
@NotNull
@Pattern(regexp = "/.*|^$", message = "Path must start with / or be empty")
private String path; private String path;
/** /**
...@@ -68,6 +66,13 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter ...@@ -68,6 +66,13 @@ public abstract class AbstractMvcEndpoint extends WebMvcConfigurerAdapter
this.enabled = enabled; 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 @Override
public void setEnvironment(Environment environment) { public void setEnvironment(Environment environment) {
this.environment = environment; this.environment = environment;
......
...@@ -37,10 +37,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -37,10 +37,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class FlywayProperties { public class FlywayProperties {
/** /**
* Locations of migrations scripts. Can contain the special "{vendor}" placeholder * Locations of migrations scripts. Can contain the special "{vendor}" placeholder to
* to use vendor-specific locations. * use vendor-specific locations.
*/ */
private List<String> locations = new ArrayList<String>(Collections.singletonList("db/migration")); private List<String> locations = new ArrayList<String>(
Collections.singletonList("db/migration"));
/** /**
* Check that migration scripts location exists. * Check that migration scripts location exists.
......
...@@ -16,10 +16,10 @@ ...@@ -16,10 +16,10 @@
package org.springframework.boot.autoconfigure.h2; package org.springframework.boot.autoconfigure.h2;
import javax.validation.constraints.NotNull; import javax.annotation.PostConstruct;
import javax.validation.constraints.Pattern;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
/** /**
* Configuration properties for H2's console. * Configuration properties for H2's console.
...@@ -35,8 +35,6 @@ public class H2ConsoleProperties { ...@@ -35,8 +35,6 @@ public class H2ConsoleProperties {
/** /**
* Path at which the console will be available. * Path at which the console will be available.
*/ */
@NotNull
@Pattern(regexp = "/[^?#]*", message = "Path must start with /")
private String path = "/h2-console"; private String path = "/h2-console";
/** /**
...@@ -46,6 +44,13 @@ public class H2ConsoleProperties { ...@@ -46,6 +44,13 @@ public class H2ConsoleProperties {
private final Settings settings = new Settings(); private final Settings settings = new Settings();
@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");
}
public String getPath() { public String getPath() {
return this.path; return this.path;
} }
......
...@@ -19,11 +19,12 @@ package org.springframework.boot.autoconfigure.liquibase; ...@@ -19,11 +19,12 @@ package org.springframework.boot.autoconfigure.liquibase;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
import javax.validation.constraints.NotNull; import javax.annotation.PostConstruct;
import liquibase.integration.spring.SpringLiquibase; import liquibase.integration.spring.SpringLiquibase;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
/** /**
* Configuration properties to configure {@link SpringLiquibase}. * Configuration properties to configure {@link SpringLiquibase}.
...@@ -37,7 +38,6 @@ public class LiquibaseProperties { ...@@ -37,7 +38,6 @@ public class LiquibaseProperties {
/** /**
* Change log configuration path. * Change log configuration path.
*/ */
@NotNull
private String changeLog = "classpath:/db/changelog/db.changelog-master.yaml"; private String changeLog = "classpath:/db/changelog/db.changelog-master.yaml";
/** /**
...@@ -96,6 +96,11 @@ public class LiquibaseProperties { ...@@ -96,6 +96,11 @@ public class LiquibaseProperties {
*/ */
private File rollbackFile; private File rollbackFile;
@PostConstruct
private void validate() {
Assert.notNull(this.changeLog, "ChangeLog must not be null");
}
public String getChangeLog() { public String getChangeLog() {
return this.changeLog; return this.changeLog;
} }
......
...@@ -26,11 +26,11 @@ import java.util.List; ...@@ -26,11 +26,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig; import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode; import javax.servlet.SessionTrackingMode;
import javax.validation.constraints.NotNull;
import io.undertow.Undertow.Builder; import io.undertow.Undertow.Builder;
import io.undertow.UndertowOptions; import io.undertow.UndertowOptions;
...@@ -74,6 +74,7 @@ import org.springframework.boot.web.servlet.ServletContextInitializer; ...@@ -74,6 +74,7 @@ import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -122,7 +123,6 @@ public class ServerProperties ...@@ -122,7 +123,6 @@ public class ServerProperties
/** /**
* Path of the main dispatcher servlet. * Path of the main dispatcher servlet.
*/ */
@NotNull
private String servletPath = "/"; private String servletPath = "/";
/** /**
...@@ -176,6 +176,11 @@ public class ServerProperties ...@@ -176,6 +176,11 @@ public class ServerProperties
private Environment environment; private Environment environment;
@PostConstruct
private void validate() {
Assert.notNull(this.servletPath, "ServletPath must not be null");
}
@Override @Override
public int getOrder() { public int getOrder() {
return 0; return 0;
......
...@@ -19,10 +19,10 @@ package org.springframework.boot.autoconfigure.webservices; ...@@ -19,10 +19,10 @@ package org.springframework.boot.autoconfigure.webservices;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.validation.constraints.NotNull; import javax.annotation.PostConstruct;
import javax.validation.constraints.Pattern;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
/** /**
* {@link ConfigurationProperties} for Spring Web Services. * {@link ConfigurationProperties} for Spring Web Services.
...@@ -37,12 +37,17 @@ public class WebServicesProperties { ...@@ -37,12 +37,17 @@ public class WebServicesProperties {
/** /**
* Path that serves as the base URI for the services. * Path that serves as the base URI for the services.
*/ */
@NotNull
@Pattern(regexp = "/[^?#]*", message = "Path must start with /")
private String path = "/services"; private String path = "/services";
private final Servlet servlet = new Servlet(); private final Servlet servlet = new Servlet();
@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");
}
public String getPath() { public String getPath() {
return this.path; return this.path;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment