Spring Boot - Spring Security - Okta OAuth2 Login - Example

Hello everyone, Today we are going to learn how to integrate the Okta OAuth2 Sign-In by utilizing the Spring Boot application.

Generate Okta OAuth2 credentials 

1. Register Okta developer account -  click here

2. Customize your goals

3. After clicking the Customize Goals, one modal will pop up "Tell Us About Yourself"

Save the details

4. Create a Web Application(Java)

5. Create New App

6. After clicking the Create New App, one modal will pop up "Create a new  app integration"

Click the 'Next' Button

7. Change the Redirect URI to http://localhost:8080/login/oauth2/code/okta 

8. After clicking the Save button, System will generate the Client ID and Client Secret and Okta domain.

Now we have successfully created the auth client in the Okta

Creating a Simple Web Application

Now we are going to develop a simple web application using Spring Security and Okta.

Project Structure:

Project Dependency(pom.xml):

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details utilized by Maven to build the project. It contains default values for most projects. Some of the configurations that can be designated in the POM is the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists, and such can withal be designated.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<relativePath /> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot</description>



Enable Okta Sign-in (Spring Magic):

issuer: https://<domain>/oauth2/default
client-secret: <client-secret>
client-id: <client-id>
cache: 'false'


package com.knf.dev.demo.controller;

import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

public class WebController {

public String index(Principal principal) {
return "index";

Main class:

package com.knf.dev.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringOauth2OktaApplication {

public static void main(String[] args) {
SpringApplication.run(SpringOauth2OktaApplication.class, args);
The @SpringBootApplication annotation is a convenience annotation that combines the @EnableAutoConfiguration, @Configuration and the @ComponentScan annotations.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
<title>Spring Boot OAuth2 Login with Okta - Demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button class="navbar-toggle" data-target="#myNavbar"
data-toggle="collapse" type="button">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<a class="navbar-brand" href="#">Logo</a>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>

<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-2 sidenav">
<div class="col-sm-8 text-left">
<h1>Welcome <span th:text="${#authentication.principal.attributes.name}">
<p>You have been successfully logged in</p>

Run & Test 

Github repository download link is provided at the end of this tutorial

Local Setup:

Step 1: Download or clone the source code to a local machine.

Step 2mvn clean install

Step 3: Run the Spring Boot application
mvn spring-boot:run

Access the URL in  the browser: http://localhost:8080/

The application will redirect the user to the Okta login screen 

If login is successful, the User will be redirected to index view,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Spring Boot + Mockito simple application with 100% code coverage

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

Spring boot video streaming example-HTML5

ReactJS, Spring Boot JWT Authentication Example

Java - Blowfish Encryption and decryption Example

Custom Exception Handling in Quarkus REST API

Spring Boot + OpenCSV Export Data to CSV Example