Open In App
Related Articles

Spring – Add User Name and Password in Spring Security

Improve Article
Save Article
Like Article

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications. Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements. Some of the key features of Spring Security are:

  1. Comprehensive and extensible support for both Authentication and Authorization
  2. Protection against attacks like session fixation, clickjacking, cross-site request forgery, etc
  3. Servlet API integration
  4. Optional integration with Spring Web MVC

In this article, we will discuss how to add user names and passwords in the spring security by default the spring security provides a default password and user name.

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,

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: Now we have to set our user name and the password in order to override the default username and the password. So we have to set it inside our file.

Now go to the src > main > java > >


public class SpringSecurityApplication {
    public static void main(String[] args) {, 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 file.

Terminal Output:

We can see there will be no default password is generated when we  provided our username and the password. Now Go to any browser and type http://localhost:8080/login

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

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 26 Nov, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials