The Wayback Machine - https://web.archive.org/web/20240910010026/https://www.geeksforgeeks.org/spring-add-user-name-and-password-in-spring-security/
Open In App

Spring – Add User Name and Password in Spring Security

Last Updated : 26 Nov, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 application.properties file.

spring.security.user.name=Aayush
spring.security.user.password=12

Now 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:

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.


Similar Reads

How to Change Default User and Password in Spring Security?
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 fou
4 min read
Spring - Add Roles in Spring Security
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 fou
4 min read
Spring Security - Password Storage
Password storage is a critical aspect of Spring Security, as it is responsible for ensuring the security of users' sensitive information such as passwords. In a web application, user authentication, and authorization are two essential features that help protect user data and control access to protected resources. In Spring Security, password storag
9 min read
Spring Security - Password Encoder
Spring Security is a framework that allows a programmer to use JEE components to set security limitations on Spring-framework-based Web applications. In a nutshell, it’s a library that can be utilized and customized to suit the demands of the programmer. Because it is a part of the same Spring family as Spring Web MVC, it works well together. The m
7 min read
How to Use Spring Security without Password Encoding?
Spring Security is a framework that allows a programmer to use JEE components to set security limitations on Spring-framework-based Web applications. In a nutshell, it’s a library that can be utilized and customized to suit the demands of the programmer. Because it is a part of the same Spring family as Spring Web MVC, it works well together. The m
8 min read
Spring Security - Get the Current Logged in User Details with Database
Many times there is a requirement to get the currently logged-in user details so that we can display the user details in our view. So in this article, we will discuss how to retrieve the currently logged-in user details in Spring Security. We're going to build on top of the simple Spring MVC example and Get the Current Logged in User Details with t
9 min read
How to Integrate Keycloak with Spring Boot and Spring Security?
Keycloak is Open Source Identity and Access Management (IAM) solution developed by Red Hat. By using this you can add authentication to applications and secure services with minimum effort. No need to deal with storing users or authenticating users. Keycloak provides user federation, strong authentication, user management, fine-grained authorizatio
2 min read
Spring Boot 3.0 - JWT Authentication with Spring Security using MySQL Database
In Spring Security 5.7.0, the spring team deprecated the WebSecurityConfigurerAdapter, as they encourage users to move towards a component-based security configuration. Spring Boot 3.0 has come with many changes in Spring Security . In this article, we'll learn how to implement JWT authentication and authorization in a Spring Boot 3.0 application u
9 min read
How to Change Password of User in Android using Firebase?
In many apps, we got a feature to login using our email and password. Sometimes it happens that we forget the password and most of the time there reset our password. Here we are going to implement the same feature to Reset our password using Firebase Authentication. You may refer to the following article User authentication using Firebase in Androi
3 min read
Myth about the file name and class name in Java
The first lecture note given during java class is “In java file name and class name should be the same”. When the above law is violated a compiler error message will appear as below Java Code /***** File name: Trial.java ******/ public class Geeks { public static void main(String[] args) { System.out.println("Hello world"); } } Output: ja
3 min read
Spring Boot - Spring JDBC vs Spring Data JDBC
Spring JDBC Spring can perform JDBC operations by having connectivity with any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For example, if we are connecting with MySQL, then we need to connect "mysql-connector-java". Let us see how a pom.xml file of a maven project looks like. C/C++ Code <?xml version="1.0" encoding=
4 min read
Difference Between Spring DAO vs Spring ORM vs Spring JDBC
The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring-DAO Spring-DAO is not a spring module. It does not provide interfaces or templates
5 min read
Spring vs Spring Boot vs Spring MVC
Are you ready to dive into the exciting world of Java development? Whether you're a seasoned pro or just starting out, this article is your gateway to mastering the top frameworks and technologies in Java development. We'll explore the Spring framework, known for its versatility and lightweight nature, making it perfect for enterprise-level softwar
8 min read
Using predefined class name as Class or Variable name in Java
In Java, you can use any valid identifier as a class or variable name. However, it is not recommended to use a predefined class name as a class or variable name in Java. The reason is that when you use a predefined class name as a class or variable name, you can potentially create confusion and make your code harder to read and understand. It may a
5 min read
Spring - MVC Password
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. In this example, we will learn about the Spring MVC Passw
8 min read
Spring Security - Role Based and Permission Based Access Control
Spring Security is a popular security framework for Java-based applications. It provides a wide range of security features including authentication and authorization. Authorization is determining whether a user is allowed to perform a specific action or access a specific resource. In Spring Security, authorization is implemented using access contro
11 min read
Spring Security - UserDetailsService and UserDetails with Example
Spring Security is a framework that allows a programmer to use JEE components to set security limitations on Spring-framework-based Web applications. In a nutshell, it’s a library that can be utilized and customized to suit the demands of the programmer. Read more on Spring Security and its Features in this article Introduction to Spring Security a
10 min read
Spring Security - How to Enable and Disable CSRF
Cross Site Request Forgery (CSRF) is one of the most severe vulnerabilities which can be exploited in various ways- from changing the user’s info without his knowledge to gaining full access to the user’s account. Please read more here What is Cross Site Request Forgery (CSRF). In this article, we will see how to enable and disable CS
9 min read
Spring Security - Registration and Login Process
Spring Security is a framework that allows a programmer to use JEE components to set security limitations on Spring-framework-based Web applications. In this article, we are going to explain how to perform the complete registration and login process by using Spring Security and Spring MVC. The main purpose is to add a full registration process that
10 min read
Introduction to Spring Security and its Features
Spring Security is one of the projects by the Spring team that is built using the Spring framework in Java. This project aims to make it easy for developers to secure web applications against common exploits such as Cross-Site Request Forgery (CSRF) attacks. It contains code that can be customized or used as-is, depending on the use case. Its major
4 min read
Some Important Terms in Spring Security
Spring Security secures our web application by default and further, we can customize it as per our needs. So important terminologies in Spring Securit are as follows: AuthenticationAuthorizationFilter Let us discuss each of them individually s follows: Terminology 1: Authentication The identity of users is verified for providing the access to the s
2 min read
Spring Security - Custom Login
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 fou
3 min read
Spring Security - In-Memory Authentication
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 fou
4 min read
Spring Security Project Example using Java Configuration
Spring Security is a standard for security in spring-based applications, it provides authentication as well as authorization to the application. Spring Security is a powerful tool that provides the feature of custom security configuration to the user, security configuration in spring can be customized in two ways as listed below as follows: XML-bas
5 min read
Spring - Security Form-Based Authentication
Prerequisites: Introduction to spring, spring boot Spring security is a powerful security framework that provides authentication and authorization to the application. It is the de-facto standard for securing Spring-based applications and it uses servlet filters to provide authentication and authorization for applications. It can be extended to supp
5 min read
Spring Security XML
Spring Security is a powerful way to provide authentication as well as authorization to Spring application. Spring Security is a powerful tool that provides the feature of custom security configuration to the user, security configuration in spring can be customized in two ways as listed below as follows: XML-based configurationJava configuration. I
4 min read
Testing Spring Security Auth with JUnit
Here we are going to learn how to use InMemoryDaoImpl to verify Spring security authentication using a JUnit test case and how to programmatically create a fully complete authentication object and then utilize it in an application. SecurityContextHolder: Spring security is built on the concept of a security context, which is somewhat static. This s
4 min read
Spring Security - Remember Me
In order to remember the users on the computer while logging into a website, via Spring Security we can maintain that by using a "Remember Me" option. Spring security sends a cookie to the browser. This will have a specific time limit and till that time the cookie is valid and in that timeframe, for the given URL, automatic login is possible. Let u
7 min read
Spring Security - Basic Authentication
Spring Security is a framework that allows a programmer to use JEE components to set security limitations on Spring-framework-based Web applications. In a nutshell, it’s a library that can be utilized and customized to suit the demands of the programmer. Because it is a part of the same Spring family as Spring Web MVC, it works well together. The m
8 min read
Spring Security Architecture
Spring Security framework adds two important capabilities to web applications, Authentication Authorization/Access Control This framework provides protection against popular security issues like SCRF attacks, or Fixation attacks. It provides a secure and standard way to set up user login functionality in web applications and thus provides quick use
2 min read
Article Tags :
Practice Tags :