Class HttpAuthenticationFeature
- java.lang.Object
-
- org.glassfish.jersey.client.authentication.HttpAuthenticationFeature
-
- All Implemented Interfaces:
javax.ws.rs.core.Feature
public class HttpAuthenticationFeature extends java.lang.Object implements javax.ws.rs.core.Feature
Features that provides Http Basic and Digest client authentication (based on RFC 2617).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 propertiesHTTP_AUTHENTICATION_USERNAME
andHTTP_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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
HttpAuthenticationFeature.BasicBuilder
Extension ofHttpAuthenticationFeature.Builder
that builds the http authentication feature configured for basic authentication.static interface
HttpAuthenticationFeature.Builder
Builder that creates instances ofHttpAuthenticationFeature
.(package private) static class
HttpAuthenticationFeature.BuilderImpl
Implementation of all authentication builders.(package private) static class
HttpAuthenticationFeature.Mode
Feature authentication mode.static interface
HttpAuthenticationFeature.UniversalBuilder
Extension ofHttpAuthenticationFeature.Builder
that builds the http authentication feature configured in universal mode that supports basic and digest authentication.
-
Field Summary
Fields Modifier and Type Field Description private HttpAuthenticationFilter.Credentials
basicCredentials
private HttpAuthenticationFilter.Credentials
digestCredentials
static java.lang.String
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.static java.lang.String
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.static java.lang.String
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.static java.lang.String
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.static java.lang.String
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.static java.lang.String
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.private HttpAuthenticationFeature.Mode
mode
-
Constructor Summary
Constructors Modifier Constructor Description private
HttpAuthenticationFeature(HttpAuthenticationFeature.Mode mode, HttpAuthenticationFilter.Credentials basicCredentials, HttpAuthenticationFilter.Credentials digestCredentials)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static HttpAuthenticationFeature
basic(java.lang.String username, byte[] password)
Create the http authentication feature in basic preemptive authentication mode initialized with credentials.static HttpAuthenticationFeature
basic(java.lang.String username, java.lang.String password)
Create the http authentication feature in basic preemptive authentication mode initialized with credentials.static HttpAuthenticationFeature.BasicBuilder
basicBuilder()
Create the builder of the http authentication feature working in basic authentication mode.private static HttpAuthenticationFeature
build(HttpAuthenticationFeature.Mode mode)
private static HttpAuthenticationFeature
build(HttpAuthenticationFeature.Mode mode, java.lang.String username, byte[] password)
private static HttpAuthenticationFeature
build(HttpAuthenticationFeature.Mode mode, java.lang.String username, java.lang.String password)
boolean
configure(javax.ws.rs.core.FeatureContext context)
static HttpAuthenticationFeature
digest()
Create the http authentication feature in digest authentication mode initialized without default credentials.static HttpAuthenticationFeature
digest(java.lang.String username, byte[] password)
Create the http authentication feature in digest authentication mode initialized with credentials.static HttpAuthenticationFeature
digest(java.lang.String username, java.lang.String password)
Create the http authentication feature in digest authentication mode initialized with credentials.static HttpAuthenticationFeature
universal(java.lang.String username, byte[] password)
Create the http authentication feature in combined mode supporting both, basic and digest authentication.static HttpAuthenticationFeature
universal(java.lang.String username, java.lang.String password)
Create the http authentication feature in combined mode supporting both, basic and digest authentication.static HttpAuthenticationFeature.UniversalBuilder
universalBuilder()
Create the builder that builds http authentication feature in combined mode supporting both, basic and digest authentication.
-
-
-
Field Detail
-
HTTP_AUTHENTICATION_USERNAME
public static final java.lang.String 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:
- Constant Field Values
-
HTTP_AUTHENTICATION_PASSWORD
public static final java.lang.String 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:
- Constant Field Values
-
HTTP_AUTHENTICATION_BASIC_USERNAME
public static final java.lang.String 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:
- Constant Field Values
-
HTTP_AUTHENTICATION_BASIC_PASSWORD
public static final java.lang.String 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:
- Constant Field Values
-
HTTP_AUTHENTICATION_DIGEST_USERNAME
public static final java.lang.String 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:
- Constant Field Values
-
HTTP_AUTHENTICATION_DIGEST_PASSWORD
public static final java.lang.String 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:
- Constant Field Values
-
mode
private final HttpAuthenticationFeature.Mode mode
-
basicCredentials
private final HttpAuthenticationFilter.Credentials basicCredentials
-
digestCredentials
private final HttpAuthenticationFilter.Credentials digestCredentials
-
-
Constructor Detail
-
HttpAuthenticationFeature
private HttpAuthenticationFeature(HttpAuthenticationFeature.Mode mode, HttpAuthenticationFilter.Credentials basicCredentials, HttpAuthenticationFilter.Credentials digestCredentials)
-
-
Method Detail
-
basicBuilder
public static HttpAuthenticationFeature.BasicBuilder 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
public static HttpAuthenticationFeature basic(java.lang.String username, byte[] password)
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
public static HttpAuthenticationFeature basic(java.lang.String username, java.lang.String password)
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
public static HttpAuthenticationFeature 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
public static HttpAuthenticationFeature digest(java.lang.String username, byte[] password)
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
public static HttpAuthenticationFeature digest(java.lang.String username, java.lang.String password)
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
public static HttpAuthenticationFeature.UniversalBuilder universalBuilder()
Create the builder that builds http authentication feature in combined mode supporting both, basic and digest authentication.- Returns:
- Universal builder.
-
universal
public static HttpAuthenticationFeature universal(java.lang.String username, byte[] password)
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
public static HttpAuthenticationFeature universal(java.lang.String username, java.lang.String password)
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.
-
build
private static HttpAuthenticationFeature build(HttpAuthenticationFeature.Mode mode)
-
build
private static HttpAuthenticationFeature build(HttpAuthenticationFeature.Mode mode, java.lang.String username, byte[] password)
-
build
private static HttpAuthenticationFeature build(HttpAuthenticationFeature.Mode mode, java.lang.String username, java.lang.String password)
-
configure
public boolean configure(javax.ws.rs.core.FeatureContext context)
- Specified by:
configure
in interfacejavax.ws.rs.core.Feature
-
-