Securing A JakartaEE REST API With OIDC

This article shows how to build, containerize, and test a secure Jakarta EE 10 REST API using Open Liberty as both OpenID Connect (OIDC) provider and client. All container operations (build, run, stop, remove) are automated with Maven plugins in the integration-test module. You’ll use Java 21, CDI constructor injection, JPA, Flyway, Podman, and comprehensive unit/integration tests. All code and configuration are included and ready to use.

What is OIDC? Why Use It?

OpenID Connect (OIDC) is a modern authentication protocol built on top of OAuth 2.0, standardized in 2014 by the OpenID Foundation. It provides a simple, secure, and interoperable way for applications to authenticate users via a trusted identity provider. OIDC adds an identity layer to OAuth 2.0, introducing the concept of an ID token (a signed JWT) that securely conveys user identity.

OIDC vs. Other Protocols

ProtocolPurposeStrengthsLimitations
OpenID ConnectAuthN + AuthZSimple, JWT-based, RESTful, SSO, supported by all major IdPsRequires HTTPS, newer
OAuth 2.0AuthorizationWidely adopted, flexibleNo authentication, no ID tokens
SAML 2.0AuthN + AuthZMature, enterprise SSOXML-based, complex, not RESTful
OpenID 2.0AuthenticationEarly SSOObsolete, replaced by OIDC

OIDC Benefits:

  • RESTful and developer-friendly (JSON, JWT)
  • Secure (uses proven cryptography, JWT, HTTPS)
  • Interoperable (works with Google, Microsoft, etc.)
  • Enables SSO and delegated login
  • Simpler than SAML, more modern than OpenID 2.0
  1. Overview
  2. Project Structure
  3. Step 1: Create the Project in IntelliJ
  4. Step 2: Parent and Child POMs
  5. Step 3: Production Code
  6. Step 4: Flyway Migration Script
  7. Step 5: persistence.xml & beans.xml
  8. Step 6: Open Liberty OIDC Provider & Client Config
  9. Step 7: Generate Self-Signed Certificate
  10. Step 8: Containerfile
  11. Step 9: Integration Test Module: Maven Plugins for Podman Lifecycle
  12. Step 10: Comprehensive Unit and Integration Tests
  13. Step 11: Build and Run Commands
  14. Troubleshooting
  15. References

Project Structure

Step 1: Create the Project in IntelliJ

Description:
Use IntelliJ’s “New Project” wizard to create a Maven parent project, then add two modules:

  • restapi-war (packaging: war)
  • integration-test (packaging: jar)

Create the directory structure as above.

Step 2: Parent and Child POMs

restapi/pom.xml (parent)

restapi-war/pom.xml

Step 3: Production Code

Person.java

PersonRepository.java

PersonResource.java

RestApiApplication.java

Step 4: Flyway Migration Script

restapi-war/src/main/resources/db/migration/V1__init.sql:

Step 5: persistence.xml & beans.xml

restapi-war/src/main/resources/META-INF/persistence.xml:

beans.xml

Step 6: Open Liberty OIDC Provider & Client Config

Open Liberty can act as both an OIDC Provider (issuing tokens) and OIDC Client (verifying tokens and authenticating users), making it ideal for end-to-end, standards-based authentication in enterprise Java.

restapi-war/src/main/liberty/config/server.xml

Step 7: Generate Self-Signed Certificate

run the below steps to setup a self signed certificate in a keystore

Step 8: Containerfile

Step 9: Integration Test Module: Maven Plugins for Podman Lifecycle

integration-test/pom.xml

Step 10: Comprehensive Unit and Integration Tests

Unit Tests

PersonRepositoryTest.java

PersonResourceTest.java

Integration Tests

What does the integration test do?

Description:
The integration test (PersonResourceIT.java) provides a full end-to-end verification of the secured REST API and OIDC authentication, as it would run in production. It does the following:

  1. Obtains a real OIDC access token from the local Open Liberty OIDC provider using the Resource Owner Password Credentials Grant (with testuser/testpass).
  2. Calls the secured REST API endpoint (/api/persons) with the access token, ensuring the API returns the expected data (Alice and Bob).
  3. Attempts an unauthorized API call (no token), verifying that the API correctly rejects the request with a 401 Unauthorized response.
  4. Handles HTTPS with a self-signed certificate by configuring the HTTP client to trust all certificates (safe for local testing).

This test ensures:

  • OIDC authentication is fully functional.
  • The Liberty OIDC provider and client are correctly configured.
  • The REST API is protected and only accessible with a valid token.
  • The API returns the correct data from the database.

PersonResourceIT.java

TestUtils.java

Step 11: Build and Run Commands

  • Default build & unit tests only:

Build, containerize, and run integration tests (all Podman steps controlled by Maven):

Troubleshooting

  • Podman plugin errors: Ensure Podman is installed and accessible on your system.
  • SSL/certificate errors: Integration test client trusts all certificates for local testing.
  • Container conflicts: If containers already exist, stop and remove them before running tests.
  • Integration tests fail: Check logs with podman logs libertytest and podman logs pgtest.

References

You now have a fully automated, modern, secure Jakarta EE 10 REST API stack with end-to-end OIDC authentication, containerized and tested with Maven and Podman, and ready for further development or production hardening.

Leave a Comment