Java Spring
magic links

How to add magic links to a Java Spring application

FusionAuth provides a convenient and quick way to implement magic links in your Java Spring app

How-To > Java Spring > Magic Links

Start implementing magic links by setting up FusionAuth

Magic links are a passwordless method of authentication and make it easier for your users to log in. They work by sending the user a one time code in a link, typically as an email or SMS message. When the user clicks on the link, they are then logged in to the application.

In this doc, you’ll learn how to add magic links functionality to your Java Spring application. You’ll use FusionAuth to easily add magic links.

1. Configure FusionAuth

First, set up FusionAuth and a sample Java Spring application. Please follow this tutorial to get that sample application set up.

All done? Great.

Let’s get magic links set up for your Java Spring application.

Now, let’s enable magic links using FusionAuth. Create another FusionAuth script. To set up magic links, all you need to do is tweak FusionAuth configuration.

Go back to the fusionauth-setup directory:

cd ../setup-fusionauth

You can use the same client library and API key as you used when setting up FusionAuth and the sample Java Spring application. But here, you are configuring a different aspect, namely magic links.

Put the below code in src/main/java/io/fusionauth/example/

package io.fusionauth.example; import java.util.HashMap; import java.util.Map; import java.util.UUID; import com.inversoft.error.Errors; import; import io.fusionauth.client.FusionAuthClient; import io.fusionauth.domain.api.ApplicationResponse; public class AddMagicLink { public static void main(String[] args) { final String apiKey = System.getProperty("fusionauth.api.key"); final FusionAuthClient client = new FusionAuthClient(apiKey, "http://localhost:9011"); // enable magic links UUID clientId = UUID.fromString(Setup.APPLICATION_ID); Map<String, Object> applicationMap = new HashMap<String, Object>(); Map<String, Object> passwordlessConfigurationMap = new HashMap<String, Object>(); Map<String, Object> enableMagicLinkUpdateMap = new HashMap<String, Object>(); passwordlessConfigurationMap.put("enabled",true); applicationMap.put("passwordlessConfiguration", passwordlessConfigurationMap); enableMagicLinkUpdateMap.put("application", applicationMap); ClientResponse<ApplicationResponse, Errors> applicationResponse = client.patchApplication(clientId, enableMagicLinkUpdateMap); if (!applicationResponse.wasSuccessful()) { throw new RuntimeException("couldn't update application"); } } }

Run this code:

mvn compile && mvn exec:java -Dexec.mainClass="io.fusionauth.example.AddMagicLinks" \ -Dfusionauth.api.key=<your API key>

You are all done! With this configuration code, you’ve enabled magic links for your Java Spring application.

You can also configure magic links manually by logging into the FusionAuth admin user interface and navigating to applications, then the application configuration, then the security tab, then the passwordless login section.

Manually setting up magic links.
Manually setting up magic links.

Now, test to see that you have magic links enabled for your application. Close any other incognito windows you have open and open a new one. (This ensures you’ve logged out of the application.)

Visit the Java Spring application and log in again.

Now, on the login page, you should see a magic link button.

What the user sees with magic links enabled.
What the user sees with magic links enabled.

This is the default look and feel, but you can configure branding using themes.

If you want to actually get an email and have log in, make sure you set up your FusionAuth email configuration using a service like Sendgrid or Mailgun.

Learn more by visiting the FusionAuth developer docs

Java Spring How-Tos

Magic Links How-Tos