Friday, September 27, 2024
Google search engine
HomeLanguagesJavaHow to Implement Simple Authentication in Spring Boot?

How to Implement Simple Authentication in Spring Boot?

Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is as it tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, etc. There are several other concepts present in java that increase the user-friendly interaction between the java code and the programmer such as generic, Access specifiers, Annotations, etc these features add an extra property to the class as well as the method of the java program. In this article, we will discuss how to implement simple authentication in the spring boot. 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.

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: 2.4.12
Packaging: JAR
Java: 8
Dependencies: Spring Web, Spring Security

Step 2: Extract the zip file. Now open a suitable IDE and then go to File > New > Project from existing sources > Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync.

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: Go to the src > main > java > com.gfg.Spring.boot.app >SpringBootAppApplication.java

SpringBootAppApplication.java

Java




@SpringBootApplication
public class SpringSecurityApplication {
  
    public static void main(String[] args) {
        SpringApplication.run(SpringSecurityApplication.class, args);
    }
}


This application is now ready to run. Run the SpringBootAppApplication 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.

Terminal Output:

Spring Security dependency automatically generates a password for valid authentication and the default user name is the user. if we want to any API first we have to provide this correct information. A login page is by default provided by the spring security dependencies that act as a gateway between the secured API and the user.

Go to any browser and type http://localhost:8080/login

This is the page that will appear when we try to access any API.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments