Open In App

How to Implement Simple Authentication in Spring Boot?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to set up and configure Basic Authentication with Spring. Authentication is one of the major steps in any kind of security. Spring provides dependencies i.e. Spring Security that helps to establish the Authentication on the API. There are so many ways to add Authentication to our Restful Web Services. But here we will discuss the basic authentication process.

For Basic Authentication, we will add spring-boot-starter-security dependency in the pom.xml file. In the application.properties file, we will add username and password properties for login authentication. In the main class, we will add @EnableSpringSecurity annotation to activate Spring Security to Spring Boot.

Note: First we need to establish the spring application in our project.

Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using a spring initializer and then use an IDE to create a sample GET route. Therefore, to do this, the following steps are followed sequentially as follows:

Step 1: Go to Spring Initializr

Fill in the details as per the requirements for this application :

Project: Maven
Language: Java
Spring Boot: 3.1.5
Packaging: JAR
Java: 17
Dependencies: Spring Web, Spring Security
IDE : STS

springinitializr-image

Step 2: Extract the zip file. Now open STS IDE and then go to File > Import> Existing Maven Project > Next > Browse > Select the Project Folder > Finish.

After the project imports successfully, it will look like pictorially depicted below as follows:

Package Explorer

Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.

Step 3: In the pom.xml file spring-boot-starter-security dependency should be added to automatically configure the basic security.

NOTE: During the project creation we have added Spring Security Maven dependency to our application, so it will automatically add the spring-boot-starter-security dependency to our pom.xml file, we need not to configure this manually in XML file.

dependency-image

Below we can refer the starter security dependency:

       <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

Step 4: Go to the src > main > java > com.geeksforgeeks >SpringSecurityApplication.java

SpringSecurityApplication.java

Java




package com.geeksforgeeks;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 
@SpringBootApplication
@EnableWebSecurity
public class SpringSecurityApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringSecurityApplication.class, args);
    }
 
}


By adding @EnableWebSecurity annotation, we have activated the spring security to the Spring Boot.

Step 5: Create a Controller Class, Go to src > main > java > com.geeksforgeeks > ApplicationController

Controllerclass-image

Inside the Controller class, we have used @RequestMapping annotation for handling the URL. Here, the RestController name is ApplicationController. We have mapped the URL by @RequestMapping annotation and the url path is rest/auth. If the request has made with the URL rest/auth and welcome then it will call the method.

Below is the Java code for the ApplicationController class:

Java




package com.geeksforgeeks.controller;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("rest/auth")
public class ApplicationController {
     
    @GetMapping("/welcome")
    public String welcome()
    {
         return "Hey! welcome to GeeksforGeeks";
    }
 
}


Step 6: Now we will assign the username and password to the application.properties file.

applicationproperties-file-image

In the above image we can see the properties required in the application.properties file.

spring.security.user.name=admin
spring.security.user.password=geeksforgeeks

This application is now ready to run. Run the SpringSecurityApplication class and wait for the Tomcat server to start.

Note: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.

Output:

Application Output

Spring Security Application has started

As the request has return with rest/auth URL then it is going to pop-up like a log in page, in which we have to enter the username and password.

Note: The username and password must match the application.properties file

Go to any browser and type http://localhost:8080/rest/auth/welcome

login-page 1

This is the page that will appear when we try to access any API. If it passes then it will return the String or message to our browser as shown below:

String Returning Image in Browser



Last Updated : 08 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads