Java Spring Boot & Passkeys: This tutorial shows how to integrate passkeys into Java Spring Boot apps by implementing passkey-first auth & session management.
Nicolai
Created: September 19, 2023
Updated: October 11, 2024
We aim to make the Internet a safer place using passkeys. That's why we want to support developers with tutorials on how to implement passkeys.
In this blog post, we'll be walking through the process of building a sample application with passkey authentication using Java Spring Boot with Thymeleaf template engine. To make passkeys work, we use Corbado's passkey-first UI component that automatically connects to a passkeys backend.
If you want to see the finished code, please have a look at our sample application GitHub repository.
The result looks as follows:
This tutorial assumes basic familiarity with HTML and Java Spring Boot.
You will also need to install the Corbado Java SDK.
Use latest version available. We will take 0.0.1 as an example.
Add it as a dependency to your project's build/pom
file:
implementation "com.corbado:corbado-java:0.0.1"
Add this dependency to your project's pom.xml
:
Let's dive in!
Ben Gould
Head of Engineering
I’ve built hundreds of integrations in my time, including quite a few with identity providers and I’ve never been so impressed with a developer experience as I have been with Corbado.
10,000+ devs trust Corbado & make the Internet safer with passkeys. Got questions? We’ve written 150+ blog posts on passkeys.
Join Passkeys CommunityA Java Spring Boot project contains many files, but the only ones important
for us are in the /complete/src/main folder
. The templates are located under
/resources/templates
while the FrontendController.java
as well as its
JsonReader.java
helper class are located in a separate folder.
├── complete | ├── src/main | | ├── java/com/corbado/springboot | | | ├── FrontendController.java # Main controller which renders the HTML templates | | | └── JsonReader.java # Fetches json from web URLs | | | | | └── resources | | ├── application.properties # Contains the environment variables | | └── templates | | ├── index.html # Login page with the UI component | | └── profile.html # Profile page with user information | └── pom.xml # Contains info about the project and implementation details
Visit the Corbado developer panel to sign up and create your account (you'll see passkey sign-up in action here!).
After sign-up, a project wizard will guide you through the necessary steps to get everything up and running:
Application URL
and Relying Party ID
. The Application URL
is the URL where you embed the Corbado UI component. In this example, we set it to http://localhost:8080
. The Relying Party ID
is the domain (no protocol, no port and no path) where passkeys should be bound to. Here, it's localhost
(you can define both values als in the Settings > General > URLs of the Corbado developer panel).Become part of our Passkeys Community for updates and support.
JoinAfterwards, you'll see the relevant HTML / JavaScript code snippets you need to integrate into the project. The subsequent sections of this article will explain them in detail.
As another step, we create an API secret
which will be needed to request user data from the
Corbado backend. Please create an API secret
in Settings > Credentials > API secrets.
To initialize our project, we clone Java Spring Boot's starter repository with
In the /complete/src/main/java/com/example/springboot
folder, you will find
the HelloController.java
. We rename it to FrontendController.java
and use it
to serve plain HTML files when a user requests a certain path.
We will need the Corbado project ID and API secret in the next steps, so we'll add them as an
environment variable. For this, we create an application.properties
file under /complete/src/main/resources
and add them there:
Under /complete/src/main/resources/templates
create an index.html
file with
the content below. This will be our login page.
Add to the index.html
a script from Corbado as well as the code for the UI component.
To make things work, we modify the index
endpoint in our FrontendController.java
to render our login page template. The Corbado project ID and API secret are taken from the
environment variables, used by SDK and are inserted into the template upon rendering.
Subscribe to our Passkeys Substack for the latest news, insights and strategies.
SubscribeAfter successful authentication, the Corbado UI component redirects the user. This page
displays information about the user and provides a button to log out. In the
templates folder, add a file profile.html
with the following
content:
Next, create a profile()
method with annotation inside the
FrontendController.java
:
We now need to obtain the user information we want to display in the
profile.html
template.
Before we can use information embedded in the session, we need to verify that
the session is valid. We therefore take the cbo_short_session
cookie (the
session) and verify its signature using the session service from the Corbado Java SDK.
This can be done with:
It takes the cbo_short_session
cookie, validates it and returns the UserID
and full name of the user.
The final code for the profile
mapping looks as follows:
To start our application, we head into the /complete
directory of our app and
execute:
When visiting http://localhost:8080, you should see the following screen:
After successful sign up / login, you see the profile page:
Add passkeys to your Java Spring app.
Start For FreeThis tutorial showed how easy it is to add passwordless authentication with passkeys to a Java Spring Boot app using Corbado. Besides the passkey-first authentication, Corbado provides simple session management, that we used for a retrieval of basic user data. If you want to read more about how Corbado's session management please check the docs here. If you want to add Corbado to your existing app with existing users, please see our documentation here.
Table of Contents
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.
🚀 Subscribe to Substack
Get the latest news, strategies, and insights about passkeys sent straight to your inbox.
We provide UI components, SDKs and guides to help you add passkeys to your app in <1 hour
Start for free
Recent Articles
Vercel Passkeys: Deploying Passkeys in Next.js App on Vercel
Nicolai - September 19, 2023
Flask Passkeys: How to Implement Passkeys with Python Flask
Janina - September 15, 2023
Tutorial: How to Add Passkeys to Node.js (Express) App
Lukas - October 16, 2023