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 found in how easily it can be extended to meet custom requirements. Some of the key features of Spring Security are:
- Comprehensive and extensible support for both Authentication and Authorization
- Protection against attacks like session fixation, clickjacking, cross-site request forgery, etc
- Servlet API integration
- Optional integration with Spring Web MVC
In this article, we will discuss how to change the default user and password in spring security. There are two ways to change the default user and password in the spring security
- Method 1: Changing in the application properties file
- Method 2: Creating custom annotated EnableWebSecurity class
Changing the application properties of the spring project is one of the easiest ways to override the default user name and the password. Let’s discuss
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
Click on Generate which will download the starter project.
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 as pictorially depicted below as follows,
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 src > main > java > com.gfg.Spring.boot.app and run the main application
Method 1: Changing in the application properties file
Spring security generated default passwords in order to override we have to configure our own username and password in the applications.properties file
Now Run the main application
We can see there is no default password is generated in this case because we have already override the default password. Now go to any browser and type localhost:8080 and try to access any local API we cannot access the API first we have to bypass the security.
The user name and password are the same as we mention in the application.properties file.
Method 2: Creating custom annotated EnableWebSecurity class
Go to the src > main > java > com.gfg.Spring.boot.app and create two java files one is controller.java and the other is config.java
The above java file is used to set the controller for handling the incoming request from the client-side. Now we have to configure the request for that we will use the config.java file.
The WebSecurityConfigureAdapter class is used for configuration the incoming requests mainly two methods are used for configurations. The first method is used for adding the roles for a spring application server and the other method is used to distinguish the request according to the roles. Now run the main application of the spring application
Note: There is no default password is generated because we have already used external configuration for handling the user credentials.
Testing the API in Postman
Go to the postman and type localhost:8080/delete
Using the admin roles: