Java / Gradle

6. Security Basic Authentication - Spring Boot vs Micronaut Framework - Building a REST API

This guide compares how to secure a REST API with basic authentication in Micronaut and Spring Boot applications.

Sergio del Amo
On this guide
In this section

Sample Project

You can download a sample application with the code examples in this article.

Introduction

This guide is the sixth tutorial of Building a REST API - a series of tutorials comparing how to develop a REST API with Micronaut Framework and Spring Boot.

In this tutorial, we secure the API via basic authentication. Only users with the role SAAS_SUBSCRIPTION_OWNER can access the endpoints. Each subscription is associated with a user. Authenticated users can only access their own subscriptions.

Dependencies

Spring Boot

In the Spring Boot application, add the Spring Boot Starter Security dependency:

build.gradle
implementation("org.springframework.boot:spring-boot-starter-security")

Micronaut Security

In the Micronaut application, add Micronaut Security and Spring Security Crypto. We use the latter to encrypt the user’s password using BCrypt.

build.gradle
implementation("io.micronaut.security:micronaut-security")
implementation("org.springframework.security:spring-security-crypto:@spring-security-cryptoVersion@")

Entity

We add an owner property to the SaasSubscription entity. The owner is the unique identity of the user who created and can manage a subscription.

Spring Boot

springboot/java/src/main/java/example/micronaut/SaasSubscription.java
package example.micronaut;

import org.springframework.data.annotation.Id;

record SaasSubscription(@Id Long id, String name, Integer cents, String owner) {
}

Micronaut Framework

micronautframework/java/src/main/java/example/micronaut/SaasSubscription.java

Repository

We need to modify the repositories to add methods leveraging the owner property.

Spring Boot

springboot/java/src/main/java/example/micronaut/SaasSubscriptionRepository.java

Micronaut Framework

micronautframework/java/src/main/java/example/micronaut/SaasSubscriptionRepository.java

Security Configuration

Two sample users will be able to authenticate: sarah1 and john-owns-no-subscriptions. Only the former has the role SAAS_SUBSCRIPTION_OWNER.

Spring Boot

The following class configures Spring Security for the application:

Note

This tutorial disables CSRF only for stateless APIs used by non-browser clients. If a Basic Auth endpoint can be called from a browser, keep CSRF protection enabled or use a credential strategy that browsers do not attach automatically because browsers can send cached Basic Auth credentials on cross-site requests.

springboot/java/src/main/java/example/micronaut/SecurityConfig.java

Micronaut Framework

In Micronaut Security, Basic authentication is enabled by default. Micronaut Security attempts to authenticate the supplied credentials against every bean of type AuthenticationProvider. Write the following authentication provider singleton which authenticates the same sample users as we did in the Spring Boot application.

micronautframework/java/src/main/java/example/micronaut/AppAuthenticationProvider.java

Controllers

Note
Both frameworks allow you to bind the authenticated user to a controller method parameter of type java.security.Principal.

The controllers use the methods added to the repositories.

Spring Boot

springboot/java/src/main/java/example/micronaut/SaasSubscriptionController.java

Micronaut Framework

Note
The application endpoints are only accessible to users with role SAAS_SUBSCRIPTION_OWNER. The Micronaut application annotates every controller with @Secured("SAAS_SUBSCRIPTION_OWNER"). The Spring Boot application specifies the role requirement in the SecurityConfig.java file.
micronautframework/java/src/main/java/example/micronaut/SaasSubscriptionController.java

Tests

In this tutorial, we use AssertJ in the tests.

Database Schema

The database schema should add owner column.

springboot/java/src/test/resources/schema.sql
CREATE TABLE IF NOT EXISTS saas_subscription
(
    id    BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name  VARCHAR(255) NOT NULL,
    cents NUMBER NOT NULL DEFAULT 0,
    owner VARCHAR(255) NOT NULL
);

Tests Seed Data

The seed data inserts entries with different owners.

springboot/java/src/test/resources/data.sql
INSERT INTO saas_subscription(id, name, cents, owner) VALUES (99, 'Advanced', 2900, 'sarah1');
INSERT INTO saas_subscription(id, name, cents, owner) VALUES (100, 'Essential', 1400, 'sarah1');
INSERT INTO saas_subscription(id, name, cents, owner) VALUES (101, 'Professional', 4900, 'sarah1');
INSERT INTO saas_subscription(id, name, cents, owner) VALUES (102, 'Enterprise', 9900, 'johnsnow');

Spring Boot Test

The following tests show that you can use the method TestRestTemplate::withBasicAuth to supply basic authentication credentials.

springboot/java/src/test/java/example/micronaut/SecurityTest.java

Micronaut Test

The following tests show that you can use the method MutableHttpRequest::basicAuth to supply basic authentication credentials.

micronautframework/java/src/test/java/example/micronaut/SecurityTest.java

Conclusion

As you see in this tutorial, securing a REST API with basic authentication is straightforward in both Micronaut and Spring Boot. Security configuration differs between the frameworks, but the coding experience of accessing the authenticated user as a controller method parameter of type java.security.Principal is identical.

Next Steps

Learn more about Micronaut Security

License

Note
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).