The OAuthTokenFilter is a JAX-RS client filter that handles OAuth2 authorization. It
- filters your requests to a service,
- adds a OAuth2-Token to your requests,
- stores the token,
- checks the token for validity
- and refreshes the token when necessary.
To include the OAuthTokenFilter in your project, simply add it as a Maven Dependency.
<dependency>
<groupId>de.otto.oauthtokenfilter</groupId>
<artifactId>oauth-token-filter</artifactId>
<version>2.0.1</version>
</dependency>
Upon using your ClientBuilder, call
import de.otto.oauthtokenfilter.OAuthTokenFilter;
import jakarta.ws.rs.client.ClientBuilder;
...
ClientBuilder.newBuilder()
.register(OAuthTokenFilter.builder()
.username("<yourUsername>")
.password("<yourPassword>")
.loginurl("<yourLoginURL>")
.clientId("<yourClientId>")
.clientSecret("<yourClientSecret>")
.tokenLifetimeInSeconds("<yourTokenLifetimeInSeconds>")
.grant_type("<api_grant_type>")
.client(ClientBuilder.newClient());
.build())
.build();
After that, all requests generated by that client will have the proper Authorization Header generated. If the OAuth-server you're working with doesn't need certain values, you can simply leave them out. If you do not specify "grant_type", it will default to "password".
If you'd like to work on the project, you can do so by running:
git clone https://github.com/otto-de/oauth-token-filter.git
If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
Please follow Google Style formatting.
The code in this project is licensed under Apache 2.0 license.