Class HttpAuthenticationFeature
- All Implemented Interfaces:
Feature
The feature can work in following modes:
- BASIC: Basic preemptive authentication. In preemptive mode the authentication information is send always with each HTTP request. This mode is more usual than the following non-preemptive mode (if you require BASIC authentication you will probably use this preemptive mode). This mode must be combined with usage of SSL/TLS as the password is send only BASE64 encoded.
- BASIC NON-PREEMPTIVE: Basic non-preemptive authentication. In non-preemptive mode the
authentication information is added only when server refuses the request with
401
status code and then the request is repeated with authentication information. This mode has negative impact on the performance. The advantage is that it does not send credentials when they are not needed. This mode must be combined with usage of SSL/TLS as the password is send only BASE64 encoded. Please note that when you use non-preemptive authentication, Jersey client will make 2 requests to a resource, which also means that all registered filters will be invoked twice. - DIGEST: Http digest authentication. Does not require usage of SSL/TLS.
- UNIVERSAL: Combination of basic and digest authentication. The feature works in non-preemptive
mode which means that it sends requests without authentication information. If
401
status code is returned, the request is repeated and an appropriate authentication is used based on the authentication requested in the response (defined inWWW-Authenticate
HTTP header. The feature remembers which authentication requests were successful for given URI and next time tries to preemptively authenticate against this URI with latest successful authentication method.
To initialize the feature use static method of this feature.
Example of building the feature in Basic authentication mode:
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("user", "superSecretPassword");
Example of building the feature in basic non-preemptive mode:
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder() .nonPreemptive().credentials("user", "superSecretPassword").build();
Example of building the feature in universal mode:
HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal("user", "superSecretPassword");
Example of building the feature in universal mode with different credentials for basic and digest:
HttpAuthenticationFeature feature = HttpAuthenticationFeature.universalBuilder() .credentialsForBasic("user", "123456") .credentials("adminuser", "hello") .build();Example of building the feature in basic preemptive mode with no default credentials. Credentials will have to be supplied with each request using request properties (see below):
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().build();
Once the feature is built it needs to be registered into the Client
,
WebTarget
or other client configurable object. Example:
final Client client = ClientBuilder.newClient(); client.register(feature);Then you invoke requests as usual and authentication will be handled by the feature. You can change the credentials for each request using properties
HTTP_AUTHENTICATION_USERNAME
and
HTTP_AUTHENTICATION_PASSWORD
. Example:
final Response response = client.target("http://localhost:8080/rest/homer/contact").request() .property(HTTP_AUTHENTICATION_BASIC_USERNAME, "homer") .property(HTTP_AUTHENTICATION_BASIC_PASSWORD, "p1swd745").get();
This class also contains property key definitions for overriding only specific basic or digest credentials:
- Since:
- 2.5
- Author:
- Miroslav Fuksa
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Extension ofHttpAuthenticationFeature.Builder
that builds the http authentication feature configured for basic authentication.static interface
Builder that creates instances ofHttpAuthenticationFeature
.static interface
Extension ofHttpAuthenticationFeature.Builder
that builds the http authentication feature configured in universal mode that supports basic and digest authentication. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the password for http basic authentication feature for the request.static final String
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the username for http basic authentication feature for the request.static final String
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the password for http digest authentication feature for the request.static final String
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the username for http digest authentication feature for the request.static final String
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the password for http authentication feature for the request.static final String
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the username for http authentication feature for the request. -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpAuthenticationFeature
Create the http authentication feature in basic preemptive authentication mode initialized with credentials.static HttpAuthenticationFeature
Create the http authentication feature in basic preemptive authentication mode initialized with credentials.Create the builder of the http authentication feature working in basic authentication mode.boolean
configure
(FeatureContext context) static HttpAuthenticationFeature
digest()
Create the http authentication feature in digest authentication mode initialized without default credentials.static HttpAuthenticationFeature
Create the http authentication feature in digest authentication mode initialized with credentials.static HttpAuthenticationFeature
Create the http authentication feature in digest authentication mode initialized with credentials.static HttpAuthenticationFeature
Create the http authentication feature in combined mode supporting both, basic and digest authentication.static HttpAuthenticationFeature
Create the http authentication feature in combined mode supporting both, basic and digest authentication.Create the builder that builds http authentication feature in combined mode supporting both, basic and digest authentication.
-
Field Details
-
HTTP_AUTHENTICATION_USERNAME
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the username for http authentication feature for the request.Example:
Response response = client.target("http://localhost:8080/rest/joe/orders").request() .property(HTTP_AUTHENTICATION_USERNAME, "joe") .property(HTTP_AUTHENTICATION_PASSWORD, "p1swd745").get();
The property must be always combined with configuration ofHTTP_AUTHENTICATION_PASSWORD
property (as shown in the example). This property pair overrides all password settings of the authentication feature for the current request.The default value must be instance of
String
.The name of the configuration property is "jersey.config.client.http.auth.username".
- See Also:
-
HTTP_AUTHENTICATION_PASSWORD
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the password for http authentication feature for the request.Example:
Response response = client.target("http://localhost:8080/rest/joe/orders").request() .property(HTTP_AUTHENTICATION_USERNAME, "joe") .property(HTTP_AUTHENTICATION_PASSWORD, "p1swd745").get();
The property must be always combined with configuration ofHTTP_AUTHENTICATION_USERNAME
property (as shown in the example). This property pair overrides all password settings of the authentication feature for the current request.The value must be instance of
String
orbyte
array (byte[]
).The name of the configuration property is "jersey.config.client.http.auth.password".
- See Also:
-
HTTP_AUTHENTICATION_BASIC_USERNAME
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the username for http basic authentication feature for the request.Example:
Response response = client.target("http://localhost:8080/rest/joe/orders").request() .property(HTTP_AUTHENTICATION_BASIC_USERNAME, "joe") .property(HTTP_AUTHENTICATION_BASIC_PASSWORD, "p1swd745").get();
The property must be always combined with configuration ofHTTP_AUTHENTICATION_PASSWORD
property (as shown in the example). The property pair influence only credentials used during basic authentication.The value must be instance of
String
.The name of the configuration property is "jersey.config.client.http.auth.basic.username".
- See Also:
-
HTTP_AUTHENTICATION_BASIC_PASSWORD
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the password for http basic authentication feature for the request.Example:
Response response = client.target("http://localhost:8080/rest/joe/orders").request() .property(HTTP_AUTHENTICATION_BASIC_USERNAME, "joe") .property(HTTP_AUTHENTICATION_BASIC_PASSWORD, "p1swd745").get();
The property must be always combined with configuration ofHTTP_AUTHENTICATION_USERNAME
property (as shown in the example). The property pair influence only credentials used during basic authentication.The value must be instance of
String
orbyte
array (byte[]
).The name of the configuration property is "jersey.config.client.http.auth.basic.password".
- See Also:
-
HTTP_AUTHENTICATION_DIGEST_USERNAME
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the username for http digest authentication feature for the request.Example:
Response response = client.target("http://localhost:8080/rest/joe/orders").request() .property(HTTP_AUTHENTICATION_DIGEST_USERNAME, "joe") .property(HTTP_AUTHENTICATION_DIGEST_PASSWORD, "p1swd745").get();
The property must be always combined with configuration ofHTTP_AUTHENTICATION_PASSWORD
property (as shown in the example). The property pair influence only credentials used during digest authentication.The value must be instance of
String
.The name of the configuration property is "jersey.config.client.http.auth.digest.username".
- See Also:
-
HTTP_AUTHENTICATION_DIGEST_PASSWORD
Key of the property that can be set into theclient request
usingClientRequestContext.setProperty(String, Object)
in order to override the password for http digest authentication feature for the request.Example:
Response response = client.target("http://localhost:8080/rest/joe/orders").request() .property(HTTP_AUTHENTICATION_DIGEST_USERNAME, "joe") .property(HTTP_AUTHENTICATION_DIGEST_PASSWORD, "p1swd745").get();
The property must be always combined with configuration ofHTTP_AUTHENTICATION_PASSWORD
property (as shown in the example). The property pair influence only credentials used during digest authentication.The value must be instance of
String
orbyte
array (byte[]
).The name of the configuration property is "jersey.config.client.http.auth.digest.password".
- See Also:
-
-
Method Details
-
basicBuilder
Create the builder of the http authentication feature working in basic authentication mode. The builder can build preemptive and non-preemptive basic authentication features.- Returns:
- Basic http authentication builder.
-
basic
Create the http authentication feature in basic preemptive authentication mode initialized with credentials.- Parameters:
username
- Username.password
- Password asbyte array
.- Returns:
- Http authentication feature configured in basic mode.
-
basic
Create the http authentication feature in basic preemptive authentication mode initialized with credentials.- Parameters:
username
- Username.password
- Password asString
.- Returns:
- Http authentication feature configured in basic mode.
-
digest
Create the http authentication feature in digest authentication mode initialized without default credentials. Credentials will have to be supplied using request properties for each request.- Returns:
- Http authentication feature configured in digest mode.
-
digest
Create the http authentication feature in digest authentication mode initialized with credentials.- Parameters:
username
- Username.password
- Password asbyte array
.- Returns:
- Http authentication feature configured in digest mode.
-
digest
Create the http authentication feature in digest authentication mode initialized with credentials.- Parameters:
username
- Username.password
- Password asString
.- Returns:
- Http authentication feature configured in digest mode.
-
universalBuilder
Create the builder that builds http authentication feature in combined mode supporting both, basic and digest authentication.- Returns:
- Universal builder.
-
universal
Create the http authentication feature in combined mode supporting both, basic and digest authentication.- Parameters:
username
- Username.password
- Password asbyte array
.- Returns:
- Http authentication feature configured in digest mode.
-
universal
Create the http authentication feature in combined mode supporting both, basic and digest authentication.- Parameters:
username
- Username.password
- Password asString
.- Returns:
- Http authentication feature configured in digest mode.
-
configure
-