public class AuthenticatedURL extends Object
AuthenticatedURL
class enables the use of the JDK URL
class
against HTTP endpoints protected with the AuthenticationFilter
.
The authentication mechanisms supported by default are Hadoop Simple authentication (also known as pseudo authentication) and Kerberos SPNEGO authentication.
Additional authentication mechanisms can be supported via Authenticator
implementations.
The default Authenticator
is the KerberosAuthenticator
class which supports
automatic fallback from Kerberos SPNEGO to Hadoop Simple authentication.
AuthenticatedURL
instances are not thread-safe.
The usage pattern of the AuthenticatedURL
is:
// establishing an initial connection URL url = new URL("http://foo:8080/bar"); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL aUrl = new AuthenticatedURL(); HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token); .... // use the 'conn' instance .... // establishing a follow up connection using a token from the previous connection HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token); .... // use the 'conn' instance ....
Modifier and Type | Class and Description |
---|---|
static class |
AuthenticatedURL.Token
Client side authentication token.
|
Modifier and Type | Field and Description |
---|---|
static String |
AUTH_COOKIE
Name of the HTTP cookie used for the authentication token between the client and the server.
|
Constructor and Description |
---|
AuthenticatedURL()
Creates an
AuthenticatedURL . |
AuthenticatedURL(Authenticator authenticator)
Creates an
AuthenticatedURL . |
AuthenticatedURL(Authenticator authenticator,
ConnectionConfigurator connConfigurator)
Creates an
AuthenticatedURL . |
Modifier and Type | Method and Description |
---|---|
static void |
extractToken(HttpURLConnection conn,
AuthenticatedURL.Token token)
Helper method that extracts an authentication token received from a connection.
|
protected Authenticator |
getAuthenticator()
Returns the
Authenticator instance used by the
AuthenticatedURL . |
static Class<? extends Authenticator> |
getDefaultAuthenticator()
Returns the default
Authenticator class to use when an AuthenticatedURL instance
is created without specifying an authenticator. |
static void |
injectToken(HttpURLConnection conn,
AuthenticatedURL.Token token)
Helper method that injects an authentication token to send with a
connection.
|
HttpURLConnection |
openConnection(URL url,
AuthenticatedURL.Token token)
Returns an authenticated
HttpURLConnection . |
static void |
setDefaultAuthenticator(Class<? extends Authenticator> authenticator)
Sets the default
Authenticator class to use when an AuthenticatedURL instance
is created without specifying an authenticator. |
public static final String AUTH_COOKIE
public AuthenticatedURL()
AuthenticatedURL
.public AuthenticatedURL(Authenticator authenticator)
AuthenticatedURL
.authenticator
- the Authenticator
instance to use, if null
a KerberosAuthenticator
is used.public AuthenticatedURL(Authenticator authenticator, ConnectionConfigurator connConfigurator)
AuthenticatedURL
.authenticator
- the Authenticator
instance to use, if null
a KerberosAuthenticator
is used.connConfigurator
- a connection configurator.public static void setDefaultAuthenticator(Class<? extends Authenticator> authenticator)
Authenticator
class to use when an AuthenticatedURL
instance
is created without specifying an authenticator.authenticator
- the authenticator class to use as default.public static Class<? extends Authenticator> getDefaultAuthenticator()
Authenticator
class to use when an AuthenticatedURL
instance
is created without specifying an authenticator.protected Authenticator getAuthenticator()
Authenticator
instance used by the
AuthenticatedURL
.Authenticator
instancepublic HttpURLConnection openConnection(URL url, AuthenticatedURL.Token token) throws IOException, AuthenticationException
HttpURLConnection
.url
- the URL to connect to. Only HTTP/S URLs are supported.token
- the authentication token being used for the user.HttpURLConnection
.IOException
- if an IO error occurred.AuthenticationException
- if an authentication exception occurred.public static void injectToken(HttpURLConnection conn, AuthenticatedURL.Token token)
AuthenticatedURL.Token.openConnection(URL, ConnectionConfigurator)
which
automatically manages authentication tokens.conn
- connection to inject the authentication token into.token
- authentication token to inject.public static void extractToken(HttpURLConnection conn, AuthenticatedURL.Token token) throws IOException, AuthenticationException
This method is used by Authenticator
implementations.
conn
- connection to extract the authentication token from.token
- the authentication token.IOException
- if an IO error occurred.AuthenticationException
- if an authentication exception occurred.Copyright © 2022 Apache Software Foundation. All rights reserved.