Information security is one of the most importart concerns facing an increasily connected world. And since APIs are the glue of the digital landscape, API security is more important than ever.

In this scenario, user identity and access management are core concepts to deal with. In API architectures, in particular with the emerging microservices approach, the real challenge is to enable a strong security and identity management support while keeping efficiency and reliability.

In a microservices environment, the services are scoped and deployed in multiple containers in a distributed setup, and the service interactions are frequent and remote, mostly over HTTP. In this distributed and stateless world, even the user identity has to be managed in a distributed and stateless way.

What we need here is a solution that allows reliable user identity management and authorization checking without additional overhead, and using the JSON Web Tokens (JWT for short) can be the answer.

Quoting from the introduction, a JWT can be defined as follows:

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA.

In a world where service-to-service communication security is a real concern, using a JSON Web Token offers many advantages:

  • Performance and easy distribution: The JWT representation is compact, and thanks to its small size, the trasmission of a JWT token is fast and can be effectively performed using the HTTP protocol, for example using a URL, POST parameter, or inside a HTTP header.
  • Efficiency: A JWT token is self-contained, carrying in its payload all the identity information (including authorizations), avoiding the need to perform additional calls (for example a database query) to obtain user information.
  • Simplicity and standardization: JWT is an open standard and uses JSON to represent the identity information, a widely used and supported data format.
  • Granular security: With JWT you can provide fine grained resource access control.
  • Debuggability: JSON Web Tokens can be easily inspected, differently for example from an opaque API key.
  • Expiration control: A JWT supports an expiration time, easy to set and control.
  • OAuth2 compliance: OAuth2 uses an opaque token that relies on a central storage. You can return a JSON Web Token instead, with the allowed scopes and expiration.

Ok, now let’s get our hands dirty

I’ll take the simple API application example of my previous article, Spring Boot, Jersey, and Swagger: Always Happy Together, as a starting point to show you how to use the Holon Platform to secure API operations using JWT. The Holon Platform JWT support relies on the jjwt library to encode, decode and verify the JWT tokens.

The source code of the updated example API application can be found on GitHub, in the jwt branch of the spring-boot-jaxrs-swagger-api-example repository:

In a typical scenario, the JSON Web Tokens emission, interchange and consumption process involves at least three actors: the issuer, the protected resource and the client.

1. The JWT issuer

The issuer is in charge to issue a JSON Web Token as a response to a valid authentication request. This step can include the user account credentials validation.

The produced JSON Web Token can contain detailed user information, including for example user profile information and authorizations, in addition to system and validation related data, such as the issuer name and the expiration time. These information are called claims in the JWT terminology, and they constitute the JWT payload. In addition to the set of reserved claims, you can provide a number of custom claims to represent the user identity and account information.

The issuer role can be easily intepreted by a OAuth2 UAA (User Account and Authentication) server, which returns a JWT instead of an opaque and randomly generated token.

In this example, we’ll simulate the JSON Web Token generation by a UAA server, using the Holon Platform authentication and authorization APIs from a JUnit test class. The Holon Platform authentication architecture relies on the Authentication interface to represent the authenticated principal (which can represent an user but also another entity). The Authentication structure supports permissions to represent the authorization grants and generic parameters to specify the account details.

To build a JSON Web Token from an Authentication, the JwtTokenBuilder class can be used. This builder will translate the Authentication permissions and parameters into JWT claims.

The JWT token builder needs to know which kind of token to build (signed or not), which algorithm has to be used to sign the token and any additional reserved claim (such as the token unique id, the issuer name and the expiration time) to add to the token itself before encoding it. This information can be collected and provided to the builder using the JwtConfiguration interface and the Holon Platform Spring Boot support can be used to build a JwtConfiguration using the application configuration properties.

To enable the Holon Platform JWT support, we just have to add the holon-auth-jwt artifact dependency to the dependecies section of our project’s pom:

<!-- Holon JWT support -->

Now we can use a set of Spring Boot configuration properties, with the holon.jwt prefix, to auto-configure a JwtConfiguration instance which will be made available as a Spring component. The list of all the configuration properties is available here.

In this example, we want to setup the issuer name to be used, the expiration time and the signature algorithm to use. For the sake of simplicity, in this example we’ll use a symmetric signing algorithm (HMAC with SHA-256), so we only need a single secret key, which has to be shared between the parties.

So you want to modify the application.yml configuration file adding the following configuration properties:

    signature-algorithm: HS256
    sharedkey-base64: X7Cukx67arP4UQSjPBC8zEkpht8hc4FZZwiIJIz2Fr0=
    expire-hours: 1
    issuer: example-issuer

The expire-hours property is used to specify an expiration time of one hour, but other configuration properties are available to use a different unit of time (for example expire-minutes, expire-seconds, expire-ms).

From now on, we can simply obtain the JwtConfiguration which represents these configuration properties as an injected dependency.

So, let’s see an example about a JSON Web Token generation from an Authentication object (you can find more examples in the ApiTest unit test class of the source repository):

private JwtConfiguration jwtConfiguration;

public void jwtBuilder() {
  Authentication authc = Authentication.builder("testUser") // principal name
    // permissions
    // user details
    .withParameter("firstName", "Test")
    .withParameter("lastName", "User")
    .withParameter("email", "")
    // build the Authentication 

  // Build the JSON Web Token using the provided configuration and Authentication
  String jwtToken = JwtTokenBuilder.buildJwtToken(jwtConfiguration, authc, 
    UUID.randomUUID().toString() // Token id claim (jti)

The JWT produced in the example above will look like this:


This token can be easily transmitted using the HTTP protocol, using for example an Authorization header.

See this example for a simple JWT authorization server which issues JSON Web Tokens as the response of a POST invocation providing username and password using the HTTP Basic scheme authorization header.

2. The protected resource

Now it’s time to protected our API operations using JWT.

We’ll leverage again on the Holon Platform authentication architecture, using the Realm security abstraction and the Holon Platform Spring Boot support to allow API resources access only to authenticated clients.

We’ll use HTTP Authorization header with the Bearer scheme to obtain the JWT provided by the client. The JSON Web Token will be validated according to the JWT configuration properties (checking the sign, the issuer name and the expiration time), then the JWT claims can be used to obtain the required user identity information.

We want to implement a simple authorization control too, so the permission claims will be used to obtain the user roles and check if it was granted to access an API operation.

First of all, we have to setup the Holon Platform security Realm, specifying:

  • An AuthenticationTokenResolver to translate the JWT into an authentication token which can be interpreted by the Realm’s authenticators.
  • The Authenticator which the Realm has to use in order to perform the account authentication.
  • An Authorizer, responsible for authorization control. The default Authorizer relies on the Authentication permissions to check the user authorization against the required permissions/roles.

The Realm is configured as a Spring component in the Application class and will be automatically located and used by the Holon Platform authorization and authentication architecture:

public Realm realm(JwtConfiguration jwtConfiguration) {
  return Realm.builder()
    // HTTP Bearer authorization schema resolver
    // JWT authenticator using the provided JwtConfiguration
    // default authorizer

The JwtConfiguration bean is automatically created using the application confguration properties as seen before.

Finally, we enable the API endpoint protection simply by using the Holon Platform @Authenticate annotation on the JAX-RS resource class:

@Authenticate // require authentication
public class ApiEndpoint {

  // content omitted


The Holon Platform JAX-RS module automatically setup Jersey enabling the following behaviour:

  • A resource method annotated with @Authenticate is only accessible by a valid, JWT authenticated, client request. If the @Authenticate annotation is declared at class level, all the JAX-RS class methods inherits the authentication setup. If the authentication is not present or not valid, a 401 - Unauthorized status error response is returned.
  • The JAX-RS SecurityContext of an authenticated request returns the Authentication instance associated to the request from the getUserPrincipal() method. The Holon Platform translates back the JWT claims into the Authentication permissions and parameters, so they can be simply accessed from the JAX-RS method.
  • The standard* annotations (@PermitAll, @DenyAll, @RolesAllowed) are enabled to perform role-based resource access control, using the  Authentication permissions provided by the JWT. If authorization control is not successfull, a 403 - Forbidden  status error response is returned.
  • For more advanced authorization controls, the Realm component can be injected in the JAX-RS endpoint to leverage on the Realm API operations using the current Authentication.

For example, to declare that no role is required to access the original /ping operation, the @PermitAll annotation can be used:

@PermitAll // no specific role required
public Response ping() {
  return Response.ok("pong").build();

The ROLE2 authorization is required to access the following API operation, and the JAX-RS SecurityContext is used (injected through the standard JAX-RS @Context annotation) to obtain the authenticated principal name:

@RolesAllowed("ROLE2") // ROLE2 is required
public Response userOperation(@Context SecurityContext securityContext) {
  // get the user principal name from the JAX-RS SecurityContext
  String principalName = securityContext.getUserPrincipal().getName();
  return Response.ok(principalName).build();

Finally, we add an API operation which uses the Authentication obtained from the JAX-RS SecurityContext to read the custom JWT claims ("firstName", "lastName", "email") and the Holon Platform Realm for additional authorization control. The example response is the JSON representation of a UserDetails example class:

private Realm realm;

@RolesAllowed("ROLE2") // ROLE2 is required
public UserDetails userDetails(@Context SecurityContext securityContext) {

  // the Holon platform Authentication is available as the JAX-RS SecurityContext user principal
  Authentication auth = (Authentication) securityContext.getUserPrincipal();

  // use Realm to perform additional authorization checks
  boolean hasRole1 = realm.isPermitted(auth, "ROLE1");

  UserDetails userDetails = new UserDetails();

  // read the JWT claims, translated into Authentication parameters
  auth.getParameter("firstName", String.class).ifPresent(p -> userDetails.setFirstName(p));
  auth.getParameter("lastName", String.class).ifPresent(p -> userDetails.setLastName(p));
  auth.getParameter("email", String.class).ifPresent(p -> userDetails.setEmail(p));

  return userDetails;

3. The client

The last actor is the client, which obtains the JSON Web Token from the issuer and uses it to perform the API calls, providing it as HTTP Authorization header Bearer token.

In a service-to-service communication scenario, the JWT obtained from the original API request can be passed around to perform additional inter-service calls. No additional overhead, such as querying the database, is needed to obtain the identity information, which is encapsulated in the JWT payload.

See the ApiTest class to learn how to use the Holon Platform RestClient to perform RESTful API invocations providing the JWT as Authorization header Bearer token.

API Documentation

In the previous post, we’ve seen how to use the Holon Platform Swagger support to create and provide a standard API documentation. Now we want to complete the API documentation, adding the authorization information. Swagger specification version 3 will be used in this example.

Let’s suppose to use OAuth2 to obtain the JWT bearer token. In this example, the OAuth2 authorization server is available at the URL.

Our authorization example roles (ROLE1 and ROLE2) will be represented as OAuth2 scopes.

Using the standard Swagger annotations, we’ll add API authorization definitions in the JAX-RS endpoint class this way:

@SecurityRequirement(name = "jwt-auth", scopes = "ROLE1")
// other annotations omitted
public Response protectedOperation() {
  return Response.ok("protected").build();

Thanks to the Holon platform Swagger auto-configuration, the API documentation can be obtained in JSON format from the URL:


Using the Swagger Editor to display the API documentation, it will appear like this:

The API authorization scheme can be inspected clicking on the lock icons, for example:


We’ve seen how JWT can be a lightweight and versatile alternative to other traditional authentication systems, mostly in the stateless API and microservices world, and how the Holon Platform can make its implementation simple and reliable.

The source code of this example API application can be found on GitHub, in the jwt branch of the spring-boot-jaxrs-swagger-api-example repository:

See the previous post to learn how to create the API example application using Spring Boot, Jersey and the Holon Platform JAX-RS module.

The API world. Graphic from Wikimedia

We are on the cusp of the API economy: APIs are everywhere, boosting the digital transformation and disrupting the way we think. So a shifting is required also in the way we develop.

APIs are expected to enable automation, in turn driving efficiency, consistency and cost savings. API quality matters, both for API consumers and implementers. For these reasons, building an API infrastructure from scratch may not be a good idea. It is much easier and more productive to use a good framework or library, often more than one.

From a development point of view, it’s crucial to achieve target whilst ensuring the fulfillment of the following requirements: timing, costs saving and quality. Standardization is one of important way to achieve those goals: to ensure consistency, speed-up development, enable factorization and simplify maintanance.

Furthermore, a very important factor in APIs is a complete and accurate documentation. And best of all, the documentation should be produced and provided in a standard form, to automate the documentation process and to ensure a great overall experience in API consuming.

Even for the simplest API, achieving all these goals and accomplishing all the required tasks may be not so simple for a developer. A minimal infrastructure must be setted up, a set of components has to be selected, configured and enabled to work together. The API implementation stack configuration can be a complex, repetitive and error-prone process. The developer should not take care about the configuration and components integration details, focusing on the business tasks the API has to face and resolve.

It is where standards, frameworks and libraries come to help, taking charge of the API infrastructure setup and configuration and leaving the developer free to focus on the API goals. In one sentence: focus on what matters and forget the tedious stuff.

The Holon Platform can act both as glue for industry-standard libraries and as a catalyst for API development productivity.

In this post, we’ll start from the beginning, showing how to create a simple API service. We’ll focus on project setup and configuration, leaving out the API business logic concerns. In fact, this API will provide a single “ping” operation, which will respond with a pong message.

The building blocks we’re going to use for this project are:

  1. Spring Boot for auto-configuration and execution using an embedded servlet container
  2. Jersey, the reference JAX-RS implementation, to create the API operations using the RESTful paradigm
  3. JSON as data interchange format
  4. Swagger for API documentation
  5. Spring Boot Actuator for service monitoring and management
  6. And of course, the Holon Platform JAX-RS module, to leverage on some configuration facilities and automate the API documentation creation and provisioning

The code of this example is available on GitHub:

Creating the API application

Maven is used for project setup and dependency management, so let’s start with configuring the project’s pom dependencies.

First of all, we import the Holon Platform BOM (Bill Of Materials), this way we’ll no longer need to specify the Holon Platform artifacts version. The right version of each artifact is declared and provided by the platform BOM.

    <!-- Holon Platform BOM -->

We’ll use the Holon JAX-RS Spring Boot starter with Jersey as implementation, so we declare the following dependency:

<!-- Holon JAX-RS using Jersey -->

Do you prefer to use Resteasy as JAX-RS implementation? With Holon it’s only a matter of configuration, just change the Holon starter to use:

<!-- Holon JAX-RS using Resteasy -->

These Holon Spring Boot starters inherit the standard Spring Boot web starters, and provide embedded servlet container configuration, logging and Holon specific auto-configuration facilities. By default, Tomcat is used as embedded servlet container. Want to use Undertow? Just change the starter name (for example, holon-starter-jersey-undertow). Concerning JSON data format instead, Jackson is automatically setted up. What if Gson is your preferred choice? It’s only about changing a name, again (for example, holon-starter-jersey-gson).

Now, independently from the starter you choosed, we have to create just 2 Java classes. The first one, which we call Application, is the Spring Boot application entry-point, which triggers auto-configuration and provides the main method used to run the application itself, starting the embedded servlet container and deploying our API endpoints:

public class Application {

  public static void main(String[] args) {, args);

A application.yml file can be used to provide application configuration properties. For example, to configure the embedded server port:

  port: 9999

Next it’s time to create the API endpoint, which will provide the API operations listening to the HTTP requests and returning the corresponding responses, using JSON as data format. As said before, this simple API will only provide a ping operation, mapped to the GET request method, returning a pong response encoded as JSON. Finally, we want to map the API operations endpoint to the base “/api” path. Using JAX-RS, this is how the API endpoint can be defined:

public class ApiEndpoint {

    public Response ping() {
        return Response.ok("pong").build();


Note the @Component annotation on the API endpoint class. This is a standard Spring annotation to declare this class as a Spring component, candidate for auto-detection. Relying on the Holon Platform auto-configuration features, this class will be automatically detected and registered as a JAX-RS resource in Jersey (or Resteasy). And of course, will be enabled as a Spring component, allowing, for example, dependency injections.

To run the application, we’ll use the Spring Boot maven plugin, declaring it in the project pom:


This way, a runnable jar will be created by the Spring Boot plugin, with all the required dependencies and able to be execute standalone using a JRE.

To test the application, we can use the run Spring Boot maven goal this way:

mvn spring-boot:run

That’s it. The application can be now started, activating our API service. The ping operation will be available sending an HTTP GET request to an URL like this:


Obtaining a response like the following:

HTTP/1.1 200
Content-Type: application/json
Content-Length: 4

Documenting the API operations

We’ll use Swagger for API documentation. The Swagger specification is now the foudation of the Open API Initiative, which is trying to standardizing on how REST APIs are described.

The Holon Platform provides many configuration facilities for Swagger, perfectly integrated with JAX-RS and the Spring Boot auto-configuration architecture.

Let’s start adding the Holon Platform Swagger depencency to our project pom:


From now on, the Holon Platform auto-configuration services will auto-detect any JAX-RS endpoint annotated with the standard Swagger @Api annotation and automatically create a JAX-RS endpoint to generate and provide the Swagger API documentation. By default, this endpoint will be mapped to the /api-docs path and supports a type query parameter to obtain the Swagger API documentation as JSON (the default behaviour) or YAML.

Standard Swagger annotations are supported to enrich and configure the API documentation, such as @ApiOperation.

Additionaly, the Holon Platform Swagger module provides a set of configuration properties, which can be placed in the default Spring Boot application configuration file (for example application.yml) and used to configure the overall API informations (such the title and the version) and to change the default API documentation endpoint path.
In this example, we want to use the /api/docs path to provide the API documentation:

    version: "v1"
    title: "Example API"
    path: "/api/docs"

So you want to modify the ApiEndpoint class this way:

@Api("Test API")
public class ApiEndpoint {

    @ApiOperation("Ping request")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK: pong", response = String.class) })
    public Response ping() {
        return Response.ok("pong").build();


This way, the Swagger API documentation can be obtained in JSON format from the URL:


Or in the YAML format:


Using the Swagger Editor to display the API documentation, it will appear like this:

Monitoring the API application

As a last step, we want to add application monitoring and management capabilities, using the Spring Boot Actuator, which adds several production grade services to obtain application information, health check, configuration and so on.

To enable the Spring Boot Actuator endpoints auto-configuration, we have to add the following dependencies to the project pom:


Now a set of monitoring endpoints should be registered. For example, the health check endpoint listens to the /health path. So we expect to obtain the health information performing a GET request to an URL like this:


But wait… things do not go as we expected: the server renponds with a 404 (not found) error code! Why?

This is a well-known and annoying problem which occurs when the Jersey servlet is mapped to the root context path. The problem is that Jersey will get all the request. It does not know that it needs to forward to any actuator endpoints.

This can be resolved by change Jersey to be used as filter instead of a servlet. Using Spring Boot we can do this using a configuration property in the application.yml file:

    type: filter

But this is not enough: we also have to set the Jersey property to forward requests for URLs it doesn’t know. With the Holon Platform JAX-RS module this is simple, just another configuration property to set:

    forwardOn404: true

Well done, everything work as expected! The health check endpoint is reachable and provides the following JSON response content:



With 2 Java classes, a pom and a Spring Boot configuration file we setted up a production grade API application, able to be runned standalone, with API documentation and monitoring capabilities. Now it’s time to focus on business tasks, making the API somehow useful!

This this only the first article in a series that shows how to use the Holon Platform to face the API development challenges, including the microservices world, so stay tuned!

The source code of the example API created in this post is available on GitHub:

NOTE: See for the Swagger/OpenAPI Version 3 specification version of this example.

See the Holon Platform examples for other API development related sample projects.

We use cookies to ensure that we give you the best experience on our website.