Options

1
Use case

Hello World

2
Security level

Basic Access Control

3
Client type

Single-Page App

4
Client framework

React + JavaScript

5
Server framework

Spring + Java
react logo
javascript logo
spring logo
java logo

Hello World Full-Stack Security: React/JavaScript + Spring/Java

Updated on September 5, 2023

Developers can easily secure a full-stack application using Auth0 by Okta. This code sample demonstrates how to implement authentication in a client application built with React and JavaScript, as well as how to implement authorization in an API server built with Spring and Java. You'll connect the client and server applications to see the full security flow in action!

Let's get started!

Quick Auth0 Set Up

To get started, create an Auth0 account to connect your application with the Auth0 Identity Platform. You can also use any of your existing Auth0 accounts.

Get the Auth0 domain and client ID

  • Open the Applications section of the Auth0 Dashboard.

  • Click on the Create Application button and fill out the form with the following values:

    • Name: Hello World Client

    • Application Type: Single Page Web Applications

  • Click on the Create button.

Visit the "Register Applications" document for more details.

An Auth0 Application page loads up.

As such, click on the "Settings" tab of your Auth0 Application page, locate the "Application URIs " section, and fill in the following values:

  • Allowed Callback URLs: http://localhost:4040/callback

  • Allowed Logout URLs: http://localhost:4040

  • Allowed Web Origins: http://localhost:4040

Scroll down and click the "Save Changes" button.

Next, locate the "Basic Information" section.

Auth0 application settings to enable user authentication

When you enter a value in the input fields present on this page, any code snippet that uses such value updates to reflect it. Using the input fields makes it easy to copy and paste code as you follow along.

As such, enter the "Domain" and "Client ID" values in the following fields to set up your single-page application in the next section:

For security, these configuration values are stored in memory and only used locally. They are gone as soon as you refresh the page! As an extra precaution, you should use values from an Auth0 test application instead of a production one.

Get the Auth0 audience

  • Open the APIs section of the Auth0 Dashboard.

  • Click on the Create API button and fill out the "New API" form with the following values:

    • Name: Hello World Server

    • Identifier: https://hello-world.example.com

  • Click on the Create button.

Visit the "Register APIs" document for more details.

When setting up APIs, we also refer to the API identifier as the Audience value. Store that value in the following field to set up your API server in the next section:

Set Up and Run the Spring Project

Start by cloning the Spring project:

COMMAND
git clone https://github.com/auth0-developer-hub/api_spring_java_hello-world.git

Make the project directory your current working directory:

COMMAND
cd api_spring_java_hello-world

Then, check out the basic-authorization branch, which holds all the code related to implementing token-based authorization to protect resources in a Spring API:

COMMAND
git checkout basic-authorization

Install the Spring project dependencies using Gradle:

COMMAND
./gradlew dependencies --write-locks

Now, create a .env file under the project directory and populate it as follows:

.env
PORT=6060
CLIENT_ORIGIN_URL=http://localhost:4040
OKTA_OAUTH2_ISSUER=https://AUTH0-DOMAIN/
OKTA_OAUTH2_AUDIENCE=AUTH0-AUDIENCE

Execute the following command to run the Spring API server:

COMMAND
./gradlew bootRun

Optional: Enable hot swapping

Spring Boot supports hot swapping. The spring-boot-devtools module includes support for restarting your application automatically as your project code changes.

You have two options to use Spring Boot hot swapping:

Use Spring Tools

You can install Spring Tools in your project. Then, whenever you run the Spring server from your IDE, hot swapping will be enabled.

Spring Tools will handle watching project files, recompiling, and restarting the server as you make code changes in your project.

Using the Gradle Wrapper

You can run the Gradle Wrapper in a terminal application as follows:

  • Open a terminal window to watch and compile the Spring Boot project:
COMMAND
./gradlew compileJava -t
  • Open a second terminal window to start the server:
COMMAND
./gradlew bootRun

Running those Gradle tasks in parallel enables Spring Boot hot swapping. The server will automatically restart when you make code changes in your project.

Set Up and Run the React Project

Start by cloning the project into your local machine:

COMMAND
git clone https://github.com/auth0-developer-hub/spa_react_javascript_hello-world.git

Make the project directory your current working directory:

COMMAND
cd spa_react_javascript_hello-world

Then, check out the basic-authentication-with-api-integration branch, which holds all the React code related to implementing user login with Auth0 and requesting protected resources from an API:

COMMAND
git checkout basic-authentication-with-api-integration

Next, install the React project dependencies:

COMMAND
npm install

Once you have access to the React project, create a .env file under the project directory and populate it as follows:

.env
REACT_APP_AUTH0_DOMAIN=AUTH0-DOMAIN
REACT_APP_AUTH0_CLIENT_ID=AUTH0-CLIENT-ID
REACT_APP_AUTH0_CALLBACK_URL=http://localhost:4040/callback
REACT_APP_API_SERVER_URL=http://localhost:6060
REACT_APP_AUTH0_AUDIENCE=AUTH0-AUDIENCE

Run the React application by issuing the following command:

COMMAND
npm start

You can now visit http://localhost:4040/ to access the application.

There's something missing in this React code sample. There's no login or sign-up forms!

When you use Auth0, you don't need to build any login or sign-up forms! Your users can log in to your application through a page hosted by Auth0, which provides a secure, standards-based login experience. You can customize the Auth0 login page with your branding and enable different authentication methods, such as logging in with a username/password combination or a social provider like Facebook or Google.

Once you log in, visit the protected "Profile" page (http://localhost:4040/profile) to see all the user profile information that Auth0 securely shares with your application using ID tokens.

Then, visit the "Protected" page (http://localhost:4040/protected) or the "Admin" page (http://localhost:4040/admin) to practice requesting protected resources from an external API server using access tokens.