Affinidi Login Integration using Java and Spring Boot

Use this guide to learn how to integrate Affinidi Login on your projects.

Affinidi Java Springboot Spring Security

A code sample using Java and SpringBoot framework to enable passwordless login using Affinidi IDP.

Before you begin

  1. Set up Affinidi Vault account. Follow the guide below if you haven’t set it up yet.
Set up Affinidi Vault

Set up an Affinidi Vault account using the Web Vault or install the Mobile Vault (for Android).

The same setup steps for Mobile Vault.

  1. Click on Get started if you are creating a new account, or click on Restore from Backup if you have an existing backup of your Affinidi Vault. Provide the passphrase to secure your Affinidi Vault.

    You have the option to enable Biometrics to unlock your Affinidi Vault easily instead of using passphrase.

GIFAffinidi Vault Setup
  1. Enter your email address to register with the Affinidi Vault. An OTP will be sent to this email for verification.
GIFAffinidi Vault Passphrase
  1. Enter the OTP sent to the email you have provided for verification to complete the setup.
GIFAffinidi Vault Email Verification

After successfully providing the OTP, you are redirected to the Affinidi Vault dashboard.

  1. Install the Affinidi CLI. Follow the guide below if it hasn’t been installed.
Set up Affinidi CLI
  1. Download and install NodeJS on your machine if you haven’t set it up yet.
  1. Install Affinidi CLI using Node Package Manager (npm).
npm install -g @affinidi/cli
  1. Verify that the installation is successful.
affinidi --version
  1. Make sure you have Git installed on your machine. Follow this guide on how to install Git.

  2. Ensure that you have Java JDK version 21 installed on your machine, follow this guide for installation.

  3. Set the JAVA_HOME env variable on your local machine to the installation directory.

export JAVA_HOME="<JAVA_INSTALLATION_DIRECTORY>" #/usr/local/opt/openjdk

or add it to your command line profile: ~/.Bashrc or ~/.zshrc

Download the Application

You can download as ZIP file the code sample from the GitHub Repo or generate it using Affinidi CLI with the following command:

affinidi generate app --provider=affinidi --framework=java --library=springboot --path=affinidi-login-refcodes

Select n when prompted to Automatically configure sample app environment, we will configure it later.

The above command will generate the code sample in the affinidi-login-refcodes directory.

Install Dependencies

After successfully generating the code sample, go to the affinidi-login-refcodes directory and install the required dependencies using the following commands:

sh mvnw clean
sh mvnw install

Create Login Configuration

Name: Affinidi Login Sample

Redirect URIs: http://localhost:8080/login/oauth2/code/javademo

Using Affinidi CLI
  1. Log in to Affinidi CLI by running:
affinidi start
  1. Once you have successfully logged in, create the Login Configuration by running:
affinidi login create-config --name='Affinidi Login Sample' --redirect-uris='http://localhost:8080/login/oauth2/code/javademo'
  • --name is what you want your login configuration to be called.
  • --redirect-uris is the URL on your application where the user gets redirected after the successful authentication.

Learn more on how to manage your Login Configurations using Affinidi CLI.

Using Affinidi Portal
  1. In the Affinidi Elements section of the sidebar, go to Affinidi Login .

  2. In the Configurations section, select Create configuration.

  3. Under Configuration Details, enter the required details:

    • Name: a name for your own reference, shown only in Affinidi Portal.
    • Allowed Redirect URIs: the URLs in your application that users return to after they sign in. Enter each URI on a new line.
Create Login Configuration page
  1. Optionally, expand Additional Configuration to set what users see when they sign in and how your application authenticates:

    • Display Name: the application name shown to users when they sign in. It defaults to the configuration name.
    • Origin: the URL shown to users when they sign in. It defaults to the domain of the first redirect URI.
    • Logo URL: the logo shown to users when they sign in. A default logo is used if you leave it empty.
    • Auth method: how your application authenticates when it requests an access token: Client secret basic, Client secret post, or None.
    • Presentation Definition and ID token mapping: the data users share when they sign in. The default requests the user’s email address. Select Edit to change it. See Edit the presentation definition in Affinidi Portal.
  2. Select Create. The Store client secret dialog shows the Client ID, Client Secret, and Issuer for your configuration.

Store client secret dialog
  1. Copy the Client ID, Client Secret, and Issuer, and store them securely. You use them to integrate your application with Affinidi Login, and the dialog shows the Client Secret only once.

  2. Close the dialog. Affinidi Portal opens the details page of your new configuration.

Login Configuration uses the default Presentation Definition (presentationDefinition) and ID Token Mapping (idTokenMapping) that is used to request the user’s email address during the authentication flow.

Learn more about customising the Presentation Definition and ID Token using this guide.

Set up the Application

Once the Login Configuration is created, set up the client credentials provided to integrate Affinidi Login.

Copy and set up the environment variables:

cp .env.example .env

Set the following variables with the values provided by the Login Configuration:

PROVIDER_CLIENT_ID=<LoginConfig.auth.ClientID>
PROVIDER_CLIENT_SECRET=<LoginConfig.auth.ClientSecret>
PROVIDER_ISSUER=<LoginConfig.auth.Issuer>

The <LoginConfig.auth.*> are values from Login Configuration.

Run the Application

After installing the dependencies and setting up the required details in the application, run the following command to start the app locally:

sh mvnw spring-boot:run

Once it is successfully started, visit the app using the link http://localhost:8080.

To enable a seamless passwordless login experience with Affinidi Login, refer to the following key changes were implemented:

  1. Registered javademo as the oauth2 client provider in the ./src/main/resources/application.yml including the client credentials.
  2. Implemented the following method in the ./java/login/affinidi/client/controller/UserController.java:
    • user method to extract the user claims from the ID Token provided by the Affinidi Login.
    • populateModel method populates the user model from the ID Token to display on UI.

Explore the sample implementation to learn more about how the integration works.