Class BasicAuthenticator
java.lang.Object
com.sun.net.httpserver.Authenticator
com.sun.net.httpserver.BasicAuthenticator
public abstract class BasicAuthenticator extends Authenticator
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.Nested Class Summary
Nested classes/interfaces declared in class com.sun.net.httpserver.Authenticator
Authenticator.Failure, Authenticator.Result, Authenticator.Retry, Authenticator.Success
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
protected final String |
realm |
The HTTP Basic authentication realm. |
Constructor Summary
| Constructor | Description |
|---|---|
BasicAuthenticator |
Creates a BasicAuthenticator for the given HTTP realm. |
BasicAuthenticator |
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 Type | Method | Description |
|---|---|---|
Authenticator.Result |
authenticate |
Called to authenticate each incoming request. |
abstract boolean |
checkCredentials |
Called for each incoming request to verify the given name and password in the context of this authenticator's realm. |
String |
getRealm() |
Returns the realm this BasicAuthenticator was created with. |
Field Details
realm
protected final String realm
The HTTP Basic authentication realm.
Constructor Details
BasicAuthenticator
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.- Parameters:
-
realm- the HTTP Basic authentication realm - Throws:
-
NullPointerException- if realm isnull -
IllegalArgumentException- if realm is an empty string
BasicAuthenticator
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-8is the recommended charset because its usage is communicated to the client, and therefore more likely to be used also by the client. - Parameters:
-
realm- the HTTP Basic authentication realm -
charset- theCharsetto decode incoming credentials from the client - Throws:
-
NullPointerException- if realm or charset arenull -
IllegalArgumentException- if realm is an empty string
Method Details
getRealm
public String getRealm()
Returns the realm this
BasicAuthenticator was created with.- Returns:
- the authenticator's realm string
authenticate
public Authenticator.Result authenticate(HttpExchange t)
Description copied from class:
AuthenticatorCalled to authenticate each incoming request. The implementation must return a
Authenticator.Failure, Authenticator.Success or Authenticator.Retry object as appropriate: -
Failuremeans the authentication has completed, but has failed due to invalid credentials. -
Successmeans that the authentication has succeeded, and aPrincipalobject representing the user can be retrieved by callingAuthenticator.Success.getPrincipal(). -
Retrymeans that another HTTP exchange is required. Any response headers needing to be sent back to the client are set in the givenHttpExchange. The response code to be returned must be provided in theRetryobject.Retrymay occur multiple times.
- Specified by:
-
authenticatein classAuthenticator - Parameters:
-
t- theHttpExchangeupon which authenticate is called - Returns:
- the result
checkCredentials
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- the username from the request -
password- the password from the request - Returns:
-
trueif the credentials are valid,falseotherwise
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/jdk.httpserver/com/sun/net/httpserver/BasicAuthenticator.html