Securing Quarkus with Elytron Database Realm

Quarkus security includes several extensions from Elytron. In this tutorial we will learn how to use the Database Realm Authentication with a simple REST Service.

Our project will perform authentication against the H2 in-memory database. In order to bootstrap our project we will need the following extensions

mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
    -DprojectGroupId=com.mastertheboss \
    -DprojectArtifactId=quarkus-jdbc-realm \
    -DclassName="com.mastertheboss.SecuredResource" \
    -Dextensions="elytron-security-jdbc,  jdbc-h2, resteasy" \

Now let’s add at first some protected resources in our REST Service. We will add one resource secured against the “admin” Role and one secured against the “guest” Role:

package com.mastertheboss;


public class SecuredResource {

    public String helloAdmin() {
        return "Hello from secured resource!";


    public String helloGuest() {
        return "Hello from secured resource!";

package com.mastertheboss;

import io.quarkus.test.junit.QuarkusTest;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class SecuredResourceTest {

    public void testHelloEndpoint() {

                .auth().preemptive().basic("admin", "admin")

                .auth().preemptive().basic("guest", "guest")



To have Quarkus bootstrap the H2 Database, include the following class which triggers the H2DatabaseTestResource:

package com.mastertheboss;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;

public class TestResources {

As the above class is included in an external JAR file, you have to add its dependency to the pom.xml file:


Configuring Elytron Database Realm with Quarkus

Done with the source code, let’s now add the configuration to bootstrap the Database Realm. First of all, an import.sql script to create the Tables at start is needed. It will also add two users for testing our project:

CREATE TABLE test_user (
  id INT,
  username VARCHAR(255),
  password VARCHAR(255),
  role VARCHAR(255)

INSERT INTO test_user (id, username, password, role) VALUES (1, 'admin', 'admin', 'admin');
INSERT INTO test_user (id, username, password, role) VALUES (2, 'user','user', 'user');

Then, into the file, add the configuration of the Elytron Realm, which points to the Table we have just created:

quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:elytron_jdbc_test;DB_CLOSE_DELAY=-1;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:import.sql'
quarkus.datasource.username=username-default u.password, u.role FROM test_user u WHERE u.username=?

Here is the full project structure:

├── main
│   ├── docker
│   │   ├── Dockerfile.jvm
│   │   └── Dockerfile.native
│   ├── java
│   │   └── com
│   │       └── mastertheboss
│   │           └──
│   └── resources
│       ├──
│       └── META-INF
│           └── resources
│               └── index.html
└── test
    ├── java
    │   └── com
    │       └── mastertheboss
    │           ├──
    │           ├──
    │           └──
    └── resources
        └── import.sql

Running the Test class

Done with the code and configuration, you can run the Test as follows:

mvn install

As you can see from the logs, the test completed successfully and, behind the hoods, the IT started and stopped the Database when done:

[INFO] H2 database started in TCP server mode; server status: TCP server running at tcp:// (only local connections)
2019-11-11 18:20:03,126 INFO  [io.qua.dep.QuarkusAugmentor] (main) Beginning quarkus augmentation
2019-11-11 18:20:04,122 INFO  [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 996ms
2019-11-11 18:20:04,735 INFO  [io.quarkus] (main) Quarkus 1.0.0.CR1 started in 0.585s. Listening on:
2019-11-11 18:20:04,735 INFO  [io.quarkus] (main) Profile test activated. 
2019-11-11 18:20:04,736 INFO  [io.quarkus] (main) Installed features: [agroal, cdi, jdbc-h2, narayana-jta, resteasy, security, security-jdbc]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.258 s - in com.mastertheboss.SecuredResourceTest

That’s all! Source code for this tutorial: