Top Description Fields Constructors Methods
org.apache.catalina.valves

public abstract Class RequestFilterValve

extends ValveBase
Class Inheritance
Known Direct Subclasses
org.apache.catalina.valves.RemoteAddrValve, org.apache.catalina.valves.RemoteCIDRValve, org.apache.catalina.valves.RemoteHostValve
Imports
java.io.IOException, java.util.regex.Pattern, jakarta.servlet.ServletException, jakarta.servlet.http.HttpServletResponse, org.apache.catalina.Context, .LifecycleException, org.apache.catalina.connector.Request, .Response, org.apache.juli.logging.Log

Implementation of a Valve that performs filtering based on comparing the appropriate request property (selected based on which subclass you choose to configure into your Container's pipeline) against the regular expressions configured for this Valve.

This valve is configured by setting the allow and/or deny properties to a regular expressions (in the syntax supported by Pattern) to which the appropriate request property will be compared. Evaluation proceeds as follows:

As an option the valve can generate an invalid authenticate header instead of denying the request. This can be combined with the context attribute preemptiveAuthentication="true" and an authenticator to force authentication instead of denial.

This Valve may be attached to any Container, depending on the granularity of the filtering you wish to perform.

Author
Craig R. McClanahan

Field Summary

Modifier and TypeField and Description
private volatile boolean
addConnectorPort

Flag deciding whether we add the server connector port to the property compared in the filtering method.

protected volatile Pattern
allow

The regular expression used to test for allowed requests.

protected volatile boolean
allowValid

Helper variable to catch configuration errors.

protected volatile String
allowValue

The current allow configuration value that may or may not compile into a valid Pattern.

protected volatile Pattern
deny

The regular expression used to test for denied requests.

protected int
denyStatus

The HTTP response status code that is used when rejecting denied request.

protected volatile boolean
denyValid

Helper variable to catch configuration errors.

protected volatile String
denyValue

The current deny configuration value that may or may not compile into a valid Pattern.

private boolean
invalidAuthenticationWhenDeny

If invalidAuthenticationWhenDeny is true and the context has preemptiveAuthentication set, set an invalid authorization header to trigger basic auth instead of denying the request..

private volatile boolean
usePeerAddress

Flag deciding whether we use the connection peer address or the remote address.

Inherited from org.apache.catalina.valves.ValveBase:
asyncSupportedcontainercontainerLognextsm

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
denyRequest(Request
The servlet request to be processed
request
,
Response
The servlet response to be processed
response
)

Reject the request that was denied by this valve.

public boolean

Returns:

true to add the connector port
getAddConnectorPort
()

Get the flag deciding whether we add the server connector port to the property compared in the filtering method.

public String

Returns:

the regular expression
getAllow
()

Return the regular expression used to test for allowed requests for this Valve, if any; otherwise, return null.

public String

Returns:

the regular expression
getDeny
()

Return the regular expression used to test for denied requests for this Valve, if any; otherwise, return null.

public int

Returns:

response status code that is used to reject denied request.
getDenyStatus
()

public boolean

Returns:

true if a deny is handled by setting an invalid auth header.
getInvalidAuthenticationWhenDeny
()

protected abstract Log
public boolean

Returns:

true if we use the connection peer address
getUsePeerAddress
()

Get the flag deciding whether we use the connection peer address or the remote address.

protected void
initInternal()

Overrides org.apache.catalina.valves.ValveBase.initInternal.

Sub-classes wishing to perform additional initialization should override this method, ensuring that super.initInternal() is the first call in the overriding method.

public abstract void
invoke(Request
The servlet request to be processed
request
,
Response
The servlet response to be created
response
)

Redeclares org.apache.catalina.Valve.invoke.

Extract the desired request property, and pass it (along with the specified request and response objects) to the protected process() method to perform the actual filtering.

public boolean

Returns:

true if the request is allowed
isAllowed
(String
The request property value on which to filter
property
)

Perform the test implemented by this Valve, matching against the specified request property value.

public final boolean

Returns:

false if the current pattern is invalid
isAllowValid
()

Returns false if the last change to the allow pattern did not apply successfully.

public final boolean

Returns:

false if the current pattern is invalid
isDenyValid
()

Returns false if the last change to the deny pattern did not apply successfully.

protected void
process(String
The request property on which to filter
property
,
Request
The servlet request to be processed
request
,
Response
The servlet response to be processed
response
)

Perform the filtering that has been configured for this Valve, matching against the specified request property.

public void
setAddConnectorPort(boolean
The new flag
addConnectorPort
)

Set the flag deciding whether we add the server connector port to the property compared in the filtering method.

public void
setAllow(String
The new allow expression
allow
)

Set the regular expression used to test for allowed requests for this Valve, if any.

public void
setDeny(String
The new deny expression
deny
)

Set the regular expression used to test for denied requests for this Valve, if any.

public void
setDenyStatus(int
The status code
denyStatus
)

Set response status code that is used to reject denied request.

public void
setInvalidAuthenticationWhenDeny(boolean
true to handle a deny by setting an invalid auth header
value
)

Set invalidAuthenticationWhenDeny property.

public void
setUsePeerAddress(boolean
The new flag
usePeerAddress
)

Set the flag deciding whether we use the connection peer address or the remote address.

protected void
Inherited from org.apache.catalina.valves.ValveBase:
backgroundProcessgetContainergetDomainInternalgetNextgetObjectNameKeyPropertiesisAsyncSupportedsetAsyncSupportedsetContainersetNextstopInternaltoString

Field Detail

addConnectorPortback to summary
private volatile boolean addConnectorPort

Flag deciding whether we add the server connector port to the property compared in the filtering method. The port will be appended using a ";" as a separator.

allowback to summary
protected volatile Pattern allow

The regular expression used to test for allowed requests.

allowValidback to summary
protected volatile boolean allowValid

Helper variable to catch configuration errors. It is true by default, but becomes false if there was an attempt to assign an invalid value to the allow pattern.

allowValueback to summary
protected volatile String allowValue

The current allow configuration value that may or may not compile into a valid Pattern.

denyback to summary
protected volatile Pattern deny

The regular expression used to test for denied requests.

denyStatusback to summary
protected int denyStatus

The HTTP response status code that is used when rejecting denied request. It is 403 by default, but may be changed to be 404.

denyValidback to summary
protected volatile boolean denyValid

Helper variable to catch configuration errors. It is true by default, but becomes false if there was an attempt to assign an invalid value to the deny pattern.

denyValueback to summary
protected volatile String denyValue

The current deny configuration value that may or may not compile into a valid Pattern.

invalidAuthenticationWhenDenyback to summary
private boolean invalidAuthenticationWhenDeny

If invalidAuthenticationWhenDeny is true and the context has preemptiveAuthentication set, set an invalid authorization header to trigger basic auth instead of denying the request..

usePeerAddressback to summary
private volatile boolean usePeerAddress

Flag deciding whether we use the connection peer address or the remote address. This makes a dfifference when using AJP or the RemoteIpValve.

Constructor Detail

RequestFilterValveback to summary
public RequestFilterValve()

Method Detail

denyRequestback to summary
protected void denyRequest(Request request, Response response) throws IOException, ServletException

Reject the request that was denied by this valve.

If invalidAuthenticationWhenDeny is true and the context has preemptiveAuthentication set, set an invalid authorization header to trigger basic auth.

Parameters
request:Request

The servlet request to be processed

response:Response

The servlet response to be processed

Exceptions
IOException:
if an input/output error occurs
ServletException:
if a servlet error occurs
getAddConnectorPortback to summary
public boolean getAddConnectorPort()

Get the flag deciding whether we add the server connector port to the property compared in the filtering method. The port will be appended using a ";" as a separator.

Returns:boolean

true to add the connector port

getAllowback to summary
public String getAllow()

Return the regular expression used to test for allowed requests for this Valve, if any; otherwise, return null.

Returns:String

the regular expression

getDenyback to summary
public String getDeny()

Return the regular expression used to test for denied requests for this Valve, if any; otherwise, return null.

Returns:String

the regular expression

getDenyStatusback to summary
public int getDenyStatus()
Returns:int

response status code that is used to reject denied request.

getInvalidAuthenticationWhenDenyback to summary
public boolean getInvalidAuthenticationWhenDeny()
Returns:boolean

true if a deny is handled by setting an invalid auth header.

getLogback to summary
protected abstract Log getLog()
getUsePeerAddressback to summary
public boolean getUsePeerAddress()

Get the flag deciding whether we use the connection peer address or the remote address. This makes a dfifference when using AJP or the RemoteIpValve.

Returns:boolean

true if we use the connection peer address

initInternalback to summary
protected void initInternal() throws LifecycleException

Overrides org.apache.catalina.valves.ValveBase.initInternal.

Doc from org.apache.catalina.util.LifecycleMBeanBase.initInternal.

Sub-classes wishing to perform additional initialization should override this method, ensuring that super.initInternal() is the first call in the overriding method.

Annotations
@Override
Exceptions
LifecycleException:

Doc from org.apache.catalina.util.LifecycleBase.initInternal.

If the initialisation fails

invokeback to summary
public abstract void invoke(Request request, Response response) throws IOException, ServletException

Redeclares org.apache.catalina.Valve.invoke.

Extract the desired request property, and pass it (along with the specified request and response objects) to the protected process() method to perform the actual filtering. This method must be implemented by a concrete subclass.

Parameters
request:Request

The servlet request to be processed

response:Response

The servlet response to be created

Annotations
@Override
Exceptions
IOException:
if an input/output error occurs
ServletException:
if a servlet error occurs
isAllowedback to summary
public boolean isAllowed(String property)

Perform the test implemented by this Valve, matching against the specified request property value. This method is public so that it can be called through JMX, e.g. to test whether certain IP address is allowed or denied by the valve configuration.

Parameters
property:String

The request property value on which to filter

Returns:boolean

true if the request is allowed

isAllowValidback to summary
public final boolean isAllowValid()

Returns false if the last change to the allow pattern did not apply successfully. E.g. if the pattern is syntactically invalid.

Returns:boolean

false if the current pattern is invalid

isDenyValidback to summary
public final boolean isDenyValid()

Returns false if the last change to the deny pattern did not apply successfully. E.g. if the pattern is syntactically invalid.

Returns:boolean

false if the current pattern is invalid

processback to summary
protected void process(String property, Request request, Response response) throws IOException, ServletException

Perform the filtering that has been configured for this Valve, matching against the specified request property.

Parameters
property:String

The request property on which to filter

request:Request

The servlet request to be processed

response:Response

The servlet response to be processed

Exceptions
IOException:
if an input/output error occurs
ServletException:
if a servlet error occurs
setAddConnectorPortback to summary
public void setAddConnectorPort(boolean addConnectorPort)

Set the flag deciding whether we add the server connector port to the property compared in the filtering method. The port will be appended using a ";" as a separator.

Parameters
addConnectorPort:boolean

The new flag

setAllowback to summary
public void setAllow(String allow)

Set the regular expression used to test for allowed requests for this Valve, if any.

Parameters
allow:String

The new allow expression

setDenyback to summary
public void setDeny(String deny)

Set the regular expression used to test for denied requests for this Valve, if any.

Parameters
deny:String

The new deny expression

setDenyStatusback to summary
public void setDenyStatus(int denyStatus)

Set response status code that is used to reject denied request.

Parameters
denyStatus:int

The status code

setInvalidAuthenticationWhenDenyback to summary
public void setInvalidAuthenticationWhenDeny(boolean value)

Set invalidAuthenticationWhenDeny property.

Parameters
value:boolean

true to handle a deny by setting an invalid auth header

setUsePeerAddressback to summary
public void setUsePeerAddress(boolean usePeerAddress)

Set the flag deciding whether we use the connection peer address or the remote address. This makes a dfifference when using AJP or the RemoteIpValve.

Parameters
usePeerAddress:boolean

The new flag

startInternalback to summary
protected void startInternal() throws LifecycleException

Overrides org.apache.catalina.valves.ValveBase.startInternal.

Doc from org.apache.catalina.valves.ValveBase.startInternal.

Start this component and implement the requirements of org.apache.catalina.util.LifecycleBase#startInternal().

Annotations
@Override
Exceptions
LifecycleException:
if this component detects a fatal error that prevents this component from being used