public class AuthenticationFilter
extends java.lang.Object
implements javax.servlet.Filter
The AuthenticationFilter enables protecting web application
resources with different (pluggable)
authentication mechanisms and signer secret providers.
Out of the box it provides 2 authentication mechanisms: Pseudo and Kerberos SPNEGO.
Additional authentication mechanisms are supported via theAuthenticationHandler interface.
This filter delegates to the configured authentication handler for authentication and once it obtains an
AuthenticationToken from it, sets a signed HTTP cookie with the token. For client requests
that provide the signed HTTP cookie, it verifies the validity of the cookie, extracts the user information
and lets the request proceed to the target resource.
PseudoAuthenticationHandler, 'kerberos' is short for KerberosAuthenticationHandler, otherwise
the full class name of the AuthenticationHandler must be specified.36000 seconds. This is also used for the rollover interval for
the "random" SignerSecretProvider.
The rest of the configuration properties are specific to the AuthenticationHandler implementation and the
AuthenticationFilter will take all the properties that start with the prefix #PREFIX#, it will remove
the prefix from it and it will pass them to the the authentication handler for initialization. Properties that do
not start with the prefix will not be passed to the authentication handler initialization.
Out of the box it provides 2 signer secret provider implementations: "file" and "random"
Additional signer secret providers are supported via theSignerSecretProvider class.
For the HTTP cookies mentioned above, the SignerSecretProvider is used to determine the secret to use for signing the cookies. Different implementations can have different behaviors. The "file" implementation loads the secret from a specified file. The "random" implementation uses a randomly generated secret that rolls over at the interval specified by the [#PREFIX#.]token.validity mentioned above.
The relevant configuration properties are:| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
ALLOWED_RESOURCES
Constant for the configuration property that indicates the resources to skip this filter for
e.g.
|
protected static java.lang.String |
AUTH_FAILED
In case of composite filters, we will pass the state to the next filter.
|
static java.lang.String |
AUTH_TOKEN_VALIDITY
Constant for the configuration property that indicates the validity of the generated token.
|
static java.lang.String |
AUTH_TYPE
Constant for the property that specifies the authentication handler to use.
|
static java.lang.String |
CONFIG_PREFIX
Constant for the property that specifies the configuration prefix.
|
static java.lang.String |
COOKIE_DOMAIN
Constant for the configuration property that indicates the domain to use in the HTTP cookie.
|
static java.lang.String |
COOKIE_PATH
Constant for the configuration property that indicates the path to use in the HTTP cookie.
|
static java.lang.String |
IS_COMPOSITE
If true then the filter will expect there to be multiple filters in the line.
|
static java.lang.String |
SIGNATURE_SECRET
Constant for the property that specifies the secret to use for signing the HTTP Cookies.
|
static java.lang.String |
SIGNATURE_SECRET_FILE |
static java.lang.String |
SIGNER_SECRET_PROVIDER
Constant for the configuration property that indicates the name of the
SignerSecretProvider class to use.
|
static java.lang.String |
SIGNER_SECRET_PROVIDER_ATTRIBUTE
Constant for the ServletContext attribute that can be used for providing a
custom implementation of the SignerSecretProvider.
|
| Constructor and Description |
|---|
AuthenticationFilter() |
| Modifier and Type | Method and Description |
|---|---|
static SignerSecretProvider |
constructSecretProvider(javax.servlet.ServletContext ctx,
java.util.Properties config,
boolean disallowFallbackToRandomSecretProvider) |
static void |
createAuthCookie(javax.servlet.http.HttpServletResponse resp,
java.lang.String token,
java.lang.String domain,
java.lang.String path,
long expires,
boolean isSecure)
Creates the authentication HTTP cookie.
|
void |
destroy()
Destroys the filter.
|
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain filterChain)
If the request has a valid authentication token it allows the request to continue to the target resource,
otherwise it triggers an authentication sequence using the configured
AuthenticationHandler. |
protected AuthenticationHandler |
getAuthenticationHandler()
Returns the authentication handler being used.
|
protected java.util.Properties |
getConfiguration()
Returns the configuration properties of the
AuthenticationFilter
without the prefix. |
protected java.util.Properties |
getConfiguration(java.lang.String configPrefix,
javax.servlet.FilterConfig filterConfig)
Returns the filtered configuration (only properties starting with the specified prefix).
|
protected java.lang.String |
getCookieDomain()
Returns the cookie domain to use for the HTTP cookie.
|
protected java.lang.String |
getCookiePath()
Returns the cookie path to use for the HTTP cookie.
|
protected java.lang.String |
getRequestURL(javax.servlet.http.HttpServletRequest request)
Returns the full URL of the request including the query string.
|
protected AuthenticationToken |
getToken(javax.servlet.http.HttpServletRequest request)
Returns the
AuthenticationToken for the request. |
protected long |
getValidity()
Returns the validity time of the generated tokens.
|
void |
init(javax.servlet.FilterConfig filterConfig)
Initializes the authentication filter and signer secret provider.
|
protected void |
initializeAuthHandler(java.lang.String authHandlerClassName) |
protected void |
initializeSecretProvider(javax.servlet.FilterConfig filterConfig) |
protected boolean |
isCustomSignerSecretProvider()
Returns if a custom implementation of a SignerSecretProvider is being used.
|
protected boolean |
isRandomSecret()
Returns if a random secret is being used.
|
protected void |
proceedToNextFilter(javax.servlet.FilterChain filterChain,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegates call to the servlet filter chain.
|
protected void |
respondWithUnauthorized(javax.servlet.ServletRequest request,
javax.servlet.http.HttpServletResponse httpResponse,
int errCode,
AuthenticationException authenticationEx) |
public static final java.lang.String CONFIG_PREFIX
public static final java.lang.String AUTH_TYPE
public static final java.lang.String IS_COMPOSITE
public static final java.lang.String SIGNATURE_SECRET
public static final java.lang.String SIGNATURE_SECRET_FILE
public static final java.lang.String AUTH_TOKEN_VALIDITY
public static final java.lang.String COOKIE_DOMAIN
public static final java.lang.String COOKIE_PATH
public static final java.lang.String SIGNER_SECRET_PROVIDER
public static final java.lang.String SIGNER_SECRET_PROVIDER_ATTRIBUTE
public static final java.lang.String ALLOWED_RESOURCES
protected static final java.lang.String AUTH_FAILED
public void init(javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
Initializes the authentication filter and signer secret provider.
It instantiates and initializes the specifiedAuthenticationHandler.init in interface javax.servlet.FilterfilterConfig - filter configuration.javax.servlet.ServletException - thrown if the filter or the authentication handler could not be initialized properly.protected void initializeAuthHandler(java.lang.String authHandlerClassName)
throws javax.servlet.ServletException
javax.servlet.ServletExceptionprotected void initializeSecretProvider(javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
javax.servlet.ServletExceptionpublic static SignerSecretProvider constructSecretProvider(javax.servlet.ServletContext ctx, java.util.Properties config, boolean disallowFallbackToRandomSecretProvider) throws java.lang.Exception
java.lang.Exceptionprotected java.util.Properties getConfiguration()
AuthenticationFilter
without the prefix. The returned properties are the same that the
getConfiguration(String, FilterConfig) method returned.protected AuthenticationHandler getAuthenticationHandler()
protected boolean isRandomSecret()
protected boolean isCustomSignerSecretProvider()
protected long getValidity()
protected java.lang.String getCookieDomain()
protected java.lang.String getCookiePath()
public void destroy()
It invokes the AuthenticationHandler.destroy() method to release any resources it may hold.
destroy in interface javax.servlet.Filterprotected java.util.Properties getConfiguration(java.lang.String configPrefix,
javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
Properties object is used to initialized the
AuthenticationHandler.
This method can be overriden by subclasses to obtain the configuration from other configuration source than the web.xml file.
configPrefix - configuration prefix to use for extracting configuration properties.filterConfig - filter configuration objectAuthenticationHandler instance.javax.servlet.ServletException - thrown if the configuration could not be created.protected java.lang.String getRequestURL(javax.servlet.http.HttpServletRequest request)
Used as a convenience method for logging purposes.
request - the request object.protected AuthenticationToken getToken(javax.servlet.http.HttpServletRequest request) throws java.io.IOException, AuthenticationException
AuthenticationToken for the request.
It looks at the received HTTP cookies and extracts the value of the AuthenticatedURL.AUTH_COOKIE
if present. It verifies the signature and if correct it creates the AuthenticationToken and returns
it.
If this method returns null the filter will invoke the configured AuthenticationHandler
to perform user authentication.
request - request object.null otherwise.java.io.IOException - thrown if an IO error occurred.AuthenticationException - thrown if the token is invalid or if it has expired.public void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain filterChain)
throws java.io.IOException,
javax.servlet.ServletException
AuthenticationHandler.doFilter in interface javax.servlet.Filterrequest - the request object.response - the response object.filterChain - the filter chain object.java.io.IOException - thrown if an IO error occurred.javax.servlet.ServletException - thrown if a processing error occurred.protected void respondWithUnauthorized(javax.servlet.ServletRequest request,
javax.servlet.http.HttpServletResponse httpResponse,
int errCode,
AuthenticationException authenticationEx)
throws java.io.IOException
java.io.IOExceptionprotected void proceedToNextFilter(javax.servlet.FilterChain filterChain,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws java.io.IOException,
javax.servlet.ServletException
java.io.IOExceptionjavax.servlet.ServletExceptionpublic static void createAuthCookie(javax.servlet.http.HttpServletResponse resp,
java.lang.String token,
java.lang.String domain,
java.lang.String path,
long expires,
boolean isSecure)
token - authentication token for the cookie.expires - UNIX timestamp that indicates the expire date of the
cookie. It has no effect if its value < 0.