Example: Using Keycloak, MP and OpenLiberty locall
  • Introduction
  • Setup the sample application
    • Step 1 - Start and configure Keycloak
    • Step 2 - Understand and configure the Authors microserice on OpenLiberty
    • Step 3 - Understand the JUnit test implementation
  • Start the Microservice and execute the test
    • Step 1 - Start the OpenLiberty server and execute the JUnit test
    • (Optional) Extract the JWT from a bearer token
  • Additional Resources
    • MicroProfile JWT Authentication with Keycloak and React (Philip Riecks)
    • MicroProfile JWT with Keycloak (Hayri Cicek)
    • MicroProfile Rest Client for RESTful communication (Philip Riecks)
    • JAX-RS - @POST with HTML Form Example (LogicBig.com)
    • Java Code Examples for javax.ws.rs.client.WebTarget (programcreek)
    • Code sample form and entity (Codota)
    • API login and JWT token generation using Keycloak (RedHat Developers)
    • JWT Decoder (JWT.io)
    • Write and execute a JUnit (Thomas Suedbroecker)
  • Known problems
    • Keycloak, not returning access token, if update password action selected
Powered by GitBook
On this page

Was this helpful?

  1. Setup the sample application

Step 3 - Understand the JUnit test implementation

PreviousStep 2 - Understand and configure the Authors microserice on OpenLibertyNextStep 1 - Start the OpenLiberty server and execute the JUnit test

Last updated 4 years ago

Was this helpful?

We need to ...

  1. ... use the two-factor authentication to access the Authors Microservice

  2. ... use a REST a client to send the requests to the Authors Microservice and Keycloak

  3. ... verify Authors Microservice response in the JUnit test

The simplified classdiagram shows an overview of classes of our project for the Authors Microservice and the JUnit test.

  • Test_GetAuthors class implemments the test.

  • AuthorJsonbAdapter class implemets the JSON-B adapter to convert Author objects to JSON format.

  • KeycloakAuthRequestFilter implements to simplify the creation of the REST client to access the Authors Microservice.

The JUnit test is implemented to do following tasks:

  1. Use the two-factor authentication to access the Authors Microservice

    1. Request the bearer token from the Keycloak server. The request is implemented in the private getToken operation of the Test_GetAuthors class

    2. Use the KeycloakAuthRequestFilter interface implementation for a ClientRequestFilter. KeycloakAuthRequestFilter does help to automatically build the needed parameter from bearer-tokon response of the Keycloak server. We need now to invoke the AuthorsMicroservice with a REST Client which contains the bearer token we got from Keycloak.

    3. Invoke the Authors Microservice from the private getAuthorAuthorized operation of the Test_GetAuthors class.

      1. Using a WebTarget to build a REST client to send the request to the Authors Microservice

      2. Create from JSON response of the Authors Microservice an Author Java object, using the JSON-B adapter class AuthorJsonbAdapter.

  2. Verify the Authors Microservice response in the JUnit test

ClientRequestFilter