APIdia Launch Partner
Top Description Fields Constructors Methods
com.sun.net.httpserver

public abstract Class BasicAuthenticator

extends Authenticator
Class Inheritance
Imports
java.nio.charset.Charset, java.util.Base64, .Objects

BasicAuthenticator provides an implementation of HTTP Basic authentication. It is an abstract class and must be extended to provide an implementation of checkCredentials(String, String) which is called to verify each incoming request.
Since
1.6

Field Summary

Modifier and TypeField and Description
private final Charset
private final boolean
protected final String
realm

The HTTP Basic authentication realm.

Constructor Summary

AccessConstructor and Description
public
BasicAuthenticator(String
the HTTP Basic authentication realm
realm
)

Creates a BasicAuthenticator for the given HTTP realm.

public
BasicAuthenticator(String
the HTTP Basic authentication realm
realm
,
Charset
the Charset to decode incoming credentials from the client
charset
)

Creates a BasicAuthenticator for the given HTTP realm and using the given Charset to decode the Basic authentication credentials (username and password).

Method Summary

Modifier and TypeMethod and Description
public Authenticator.Result
authenticate(HttpExchange t)

Implements abstract com.sun.net.httpserver.Authenticator.authenticate.

Called to authenticate each incoming request.

public abstract boolean

Returns:

true if the credentials are valid, false otherwise
checkCredentials
(String
the username from the request
username
,
String
the password from the request
password
)

Called for each incoming request to verify the given name and password in the context of this authenticator's realm.

public String

Returns:

the authenticator's realm string
getRealm
()

Returns the realm this BasicAuthenticator was created with.

private void

Field Detail

charsetback to summary
private final Charset charset
isUTF8back to summary
private final boolean isUTF8
realmback to summary
protected final String realm

The HTTP Basic authentication realm.

Constructor Detail

BasicAuthenticatorback to summary
public BasicAuthenticator(String realm)

Creates a BasicAuthenticator for the given HTTP realm. The Basic authentication credentials (username and password) are decoded using the platform's default character set.

API Note

The value of the realm parameter will be embedded in a quoted string.

Parameters
realm:String

the HTTP Basic authentication realm

Exceptions
NullPointerException:
if realm is null
IllegalArgumentException:
if realm is an empty string or is not correctly quoted, as specified in RFC 7230 section-3.2. Note, any \ character used for quoting must itself be quoted in source code.
BasicAuthenticatorback to summary
public BasicAuthenticator(String realm, Charset charset)

Creates a BasicAuthenticator for the given HTTP realm and using the given Charset to decode the Basic authentication credentials (username and password).

API Note

UTF-8 is the recommended charset because its usage is communicated to the client, and therefore more likely to be used also by the client.

The value of the realm parameter will be embedded in a quoted string.

Parameters
realm:String

the HTTP Basic authentication realm

charset:Charset

the Charset to decode incoming credentials from the client

Exceptions
NullPointerException:
if realm or charset are null
IllegalArgumentException:
if realm is an empty string or is not correctly quoted, as specified in RFC 7230 section-3.2. Note, any \ character used for quoting must itself be quoted in source code.
Since
14

Method Detail

authenticateback to summary
public Authenticator.Result authenticate(HttpExchange t)

Implements abstract com.sun.net.httpserver.Authenticator.authenticate.

Doc from com.sun.net.httpserver.Authenticator.authenticate.

Called to authenticate each incoming request. The implementation must return a Failure, Success or Retry object as appropriate:

  • Failure means the authentication has completed, but has failed due to invalid credentials.
  • Success means that the authentication has succeeded, and a Principal object representing the user can be retrieved by calling Success#getPrincipal().
  • Retry means that another HTTP exchange is required. Any response headers needing to be sent back to the client are set in the given HttpExchange. The response code to be returned must be provided in the Retry object. Retry may occur multiple times.
checkCredentialsback to summary
public abstract boolean checkCredentials(String username, String password)

Called for each incoming request to verify the given name and password in the context of this authenticator's realm. Any caching of credentials must be done by the implementation of this method.

Parameters
username:String

the username from the request

password:String

the password from the request

Returns:boolean

true if the credentials are valid, false otherwise

getRealmback to summary
public String getRealm()

Returns the realm this BasicAuthenticator was created with.

Returns:String

the authenticator's realm string

setAuthHeaderback to summary
private void setAuthHeader(HttpExchange t)