java.lang.Object
de.cuioss.http.security.config.SecurityConfigurationBuilder
Builder class for constructing
SecurityConfiguration instances with fluent API.
This builder provides a convenient way to construct SecurityConfiguration objects with sensible defaults while allowing fine-grained control over all security settings. The builder follows the standard builder pattern with method chaining.
Design Principles
- Fluent API - All setter methods return the builder for chaining
- Sensible Defaults - Pre-configured with reasonable security defaults
- Validation - Input validation on all parameters
- Immutability - Produces immutable SecurityConfiguration instances
Usage Examples
// Basic configuration with defaults
SecurityConfiguration config = SecurityConfiguration.builder().build();
// Custom configuration
SecurityConfiguration custom = SecurityConfiguration.builder()
.maxPathLength(2048)
.allowPathTraversal(false)
.maxParameterCount(50)
.requireSecureCookies(true)
.addBlockedHeaderName("X-Debug")
.addAllowedContentType("application/json")
.addAllowedContentType("text/plain")
.build();
// Chain multiple settings
SecurityConfiguration strict = SecurityConfiguration.builder()
.pathSecurity(1024, false)
.cookieSecurity(true, true, 10, 64, 512)
.bodySecurity(1024 * 1024, Set.of("application/json"))
.encoding(false, false, false, true)
.policies(true, true, true)
.build();
Default Values
The builder is initialized with balanced default values that provide reasonable security without being overly restrictive:
- Path length: 4096 characters
- Parameter count: 100
- Header count: 50
- Cookie count: 20
- Body size: 5MB
- Path traversal: disabled
- Cookie security flags: recommended but not required
- Since:
- 1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddAllowedContentType(String contentType) Adds a content type to the allowed list.addAllowedHeaderName(String headerName) Adds a header name to the allowed list.addBlockedContentType(String contentType) Adds a content type to the blocked list.addBlockedHeaderName(String headerName) Adds a header name to the blocked list.allowControlCharacters(boolean allow) Sets whether control characters are allowed in content.allowDoubleEncoding(boolean allow) Sets whether double URL encoding is allowed.allowedContentTypes(@Nullable Set<String> contentTypes) Sets the complete list of allowed content types.allowedHeaderNames(@Nullable Set<String> headerNames) Sets the complete list of allowed header names.allowExtendedAscii(boolean allow) Sets whether extended ASCII characters (128-255) are allowed in content.allowNullBytes(boolean allow) Sets whether null bytes are allowed in content.allowPathTraversal(boolean allow) Sets whether path traversal patterns (../) are allowed.blockedContentTypes(Set<String> contentTypes) Sets the complete list of blocked content types.blockedHeaderNames(Set<String> headerNames) Sets the complete list of blocked header names.bodySecurity(long maxSize, @Nullable Set<String> allowedTypes) Configures body security settings in one call.build()Builds the SecurityConfiguration with the current settings.caseSensitiveComparison(boolean caseSensitive) Sets whether string comparisons should be case-sensitive.cookieSecurity(boolean requireSecure, boolean requireHttpOnly, int maxCount, int maxNameLength, int maxValueLength) Configures cookie security settings in one call.encoding(boolean allowNulls, boolean allowControls, boolean allowHighBit, boolean normalizeUni) Configures encoding security settings in one call.failOnSuspiciousPatterns(boolean fail) Sets whether to fail on detection of suspicious patterns.headerSecurity(int maxCount, int maxNameLength, int maxValueLength) Configures header security settings in one call.logSecurityViolations(boolean log) Sets whether to log security violations.maxBodySize(long maxSize) Sets the maximum body size in bytes.maxCookieCount(int maxCount) Sets the maximum number of cookies allowed.maxCookieNameLength(int maxLength) Sets the maximum length for cookie names.maxCookieValueLength(int maxLength) Sets the maximum length for cookie values.maxHeaderCount(int maxCount) Sets the maximum number of HTTP headers allowed.maxHeaderNameLength(int maxLength) Sets the maximum length for header names.maxHeaderValueLength(int maxLength) Sets the maximum length for header values.maxParameterCount(int maxCount) Sets the maximum number of query parameters allowed.maxParameterNameLength(int maxLength) Sets the maximum length for parameter names.maxParameterValueLength(int maxLength) Sets the maximum length for parameter values.maxPathLength(int maxLength) Sets the maximum allowed path length.normalizeUnicode(boolean normalize) Sets whether Unicode normalization should be performed.parameterSecurity(int maxCount, int maxNameLength, int maxValueLength) Configures parameter security settings in one call.pathSecurity(int maxLength, boolean allowTraversal) Configures path security settings in one call.policies(boolean caseSensitive, boolean failOnSuspicious, boolean logViolations) Configures general policy settings in one call.requireHttpOnlyCookies(boolean require) Sets whether all cookies must have the HttpOnly flag.requireSecureCookies(boolean require) Sets whether all cookies must have the Secure flag.
-
Method Details
-
maxPathLength
Sets the maximum allowed path length.- Parameters:
maxLength- Maximum path length in characters (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
allowPathTraversal
Sets whether path traversal patterns (../) are allowed.- Parameters:
allow- true to allow path traversal, false to block it- Returns:
- This builder for method chaining
-
allowDoubleEncoding
Sets whether double URL encoding is allowed.- Parameters:
allow- true to allow double encoding, false to block it- Returns:
- This builder for method chaining
-
pathSecurity
Configures path security settings in one call.- Parameters:
maxLength- Maximum path lengthallowTraversal- Whether to allow path traversal- Returns:
- This builder for method chaining
-
maxParameterCount
Sets the maximum number of query parameters allowed.- Parameters:
maxCount- Maximum parameter count (must be non-negative)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxCount is negative
-
maxParameterNameLength
Sets the maximum length for parameter names.- Parameters:
maxLength- Maximum name length (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
maxParameterValueLength
Sets the maximum length for parameter values.- Parameters:
maxLength- Maximum value length (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
parameterSecurity
public SecurityConfigurationBuilder parameterSecurity(int maxCount, int maxNameLength, int maxValueLength) Configures parameter security settings in one call.- Parameters:
maxCount- Maximum parameter countmaxNameLength- Maximum parameter name lengthmaxValueLength- Maximum parameter value length- Returns:
- This builder for method chaining
-
maxHeaderCount
Sets the maximum number of HTTP headers allowed.- Parameters:
maxCount- Maximum header count (must be non-negative)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxCount is negative
-
maxHeaderNameLength
Sets the maximum length for header names.- Parameters:
maxLength- Maximum name length (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
maxHeaderValueLength
Sets the maximum length for header values.- Parameters:
maxLength- Maximum value length (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
addAllowedHeaderName
Adds a header name to the allowed list. If the allowed list is null, this method initializes it with the given header name.- Parameters:
headerName- Header name to allow (must not be null)- Returns:
- This builder for method chaining
- Throws:
NullPointerException- if headerName is null
-
allowedHeaderNames
Sets the complete list of allowed header names.- Parameters:
headerNames- Set of allowed header names (null means all allowed)- Returns:
- This builder for method chaining
-
addBlockedHeaderName
Adds a header name to the blocked list.- Parameters:
headerName- Header name to block (must not be null)- Returns:
- This builder for method chaining
- Throws:
NullPointerException- if headerName is null
-
blockedHeaderNames
Sets the complete list of blocked header names.- Parameters:
headerNames- Set of blocked header names (must not be null)- Returns:
- This builder for method chaining
- Throws:
NullPointerException- if headerNames is null
-
headerSecurity
public SecurityConfigurationBuilder headerSecurity(int maxCount, int maxNameLength, int maxValueLength) Configures header security settings in one call.- Parameters:
maxCount- Maximum header countmaxNameLength- Maximum header name lengthmaxValueLength- Maximum header value length- Returns:
- This builder for method chaining
-
maxCookieCount
Sets the maximum number of cookies allowed.- Parameters:
maxCount- Maximum cookie count (must be non-negative)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxCount is negative
-
maxCookieNameLength
Sets the maximum length for cookie names.- Parameters:
maxLength- Maximum name length (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
maxCookieValueLength
Sets the maximum length for cookie values.- Parameters:
maxLength- Maximum value length (must be positive)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxLength is not positive
-
requireSecureCookies
Sets whether all cookies must have the Secure flag.- Parameters:
require- true to require Secure flag on all cookies- Returns:
- This builder for method chaining
-
requireHttpOnlyCookies
Sets whether all cookies must have the HttpOnly flag.- Parameters:
require- true to require HttpOnly flag on all cookies- Returns:
- This builder for method chaining
-
cookieSecurity
public SecurityConfigurationBuilder cookieSecurity(boolean requireSecure, boolean requireHttpOnly, int maxCount, int maxNameLength, int maxValueLength) Configures cookie security settings in one call.- Parameters:
requireSecure- Whether to require Secure flagrequireHttpOnly- Whether to require HttpOnly flagmaxCount- Maximum cookie countmaxNameLength- Maximum cookie name lengthmaxValueLength- Maximum cookie value length- Returns:
- This builder for method chaining
-
maxBodySize
Sets the maximum body size in bytes.- Parameters:
maxSize- Maximum body size (must be non-negative)- Returns:
- This builder for method chaining
- Throws:
IllegalArgumentException- if maxSize is negative
-
addAllowedContentType
Adds a content type to the allowed list. If the allowed list is null, this method initializes it with the given content type.- Parameters:
contentType- Content type to allow (must not be null)- Returns:
- This builder for method chaining
- Throws:
NullPointerException- if contentType is null
-
allowedContentTypes
Sets the complete list of allowed content types.- Parameters:
contentTypes- Set of allowed content types (null means all allowed)- Returns:
- This builder for method chaining
-
addBlockedContentType
Adds a content type to the blocked list.- Parameters:
contentType- Content type to block (must not be null)- Returns:
- This builder for method chaining
- Throws:
NullPointerException- if contentType is null
-
blockedContentTypes
Sets the complete list of blocked content types.- Parameters:
contentTypes- Set of blocked content types (must not be null)- Returns:
- This builder for method chaining
- Throws:
NullPointerException- if contentTypes is null
-
bodySecurity
Configures body security settings in one call.- Parameters:
maxSize- Maximum body sizeallowedTypes- Set of allowed content types (null = all allowed)- Returns:
- This builder for method chaining
-
allowNullBytes
Sets whether null bytes are allowed in content.- Parameters:
allow- true to allow null bytes, false to block them- Returns:
- This builder for method chaining
-
allowControlCharacters
Sets whether control characters are allowed in content.- Parameters:
allow- true to allow control characters, false to block them- Returns:
- This builder for method chaining
-
allowExtendedAscii
Sets whether extended ASCII characters (128-255) are allowed in content. For URL paths and parameters, this only affects characters 128-255. For headers and body content, this also enables Unicode support.- Parameters:
allow- true to allow extended ASCII and applicable Unicode characters, false to block them- Returns:
- This builder for method chaining
-
normalizeUnicode
Sets whether Unicode normalization should be performed.- Parameters:
normalize- true to normalize Unicode, false to leave as-is- Returns:
- This builder for method chaining
-
encoding
public SecurityConfigurationBuilder encoding(boolean allowNulls, boolean allowControls, boolean allowHighBit, boolean normalizeUni) Configures encoding security settings in one call.- Parameters:
allowNulls- Whether to allow null bytesallowControls- Whether to allow control charactersallowHighBit- Whether to allow high-bit charactersnormalizeUni- Whether to normalize Unicode- Returns:
- This builder for method chaining
-
caseSensitiveComparison
Sets whether string comparisons should be case-sensitive.- Parameters:
caseSensitive- true for case-sensitive comparisons- Returns:
- This builder for method chaining
-
failOnSuspiciousPatterns
Sets whether to fail on detection of suspicious patterns.- Parameters:
fail- true to fail on suspicious patterns, false to log only- Returns:
- This builder for method chaining
-
logSecurityViolations
Sets whether to log security violations.- Parameters:
log- true to enable logging, false to disable- Returns:
- This builder for method chaining
-
policies
public SecurityConfigurationBuilder policies(boolean caseSensitive, boolean failOnSuspicious, boolean logViolations) Configures general policy settings in one call.- Parameters:
caseSensitive- Whether comparisons are case-sensitivefailOnSuspicious- Whether to fail on suspicious patternslogViolations- Whether to log security violations- Returns:
- This builder for method chaining
-
build
Builds the SecurityConfiguration with the current settings.- Returns:
- A new immutable SecurityConfiguration instance
- Throws:
IllegalArgumentException- if any configuration values are invalid
-