How to enable default Authorization in your Spring Boot Application?

How to enable default Authorization in your Spring Boot Application?

Welcome back friends, Do you know spring boot is by default an unprotected application. i.e. if you develop any Webservice endpoint, it is by default accessible by everybody in the world. that’s why I am saying that Spring Boot is an unprotected application. 

If you want to make your spring boot an authorized application, you have to customize your application using the spring boot Security starter project. Following artifact, you have to add in your Spring Boot Application. It is called a Spring Boot Security starter Project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

Create a simple Simple Project using https://start.spring.io/ Please look at below the image for creating a simple Spring Boot Project. According to the below input in the start.spring.io. I am creating without Security. It is a simple Web MVC and Rest Application.

Spring Boot start.spring.io

I have imported this simple security project in my IntelliJ and I have written the following endpoint.

@RestController
public class WelcomeController {

    @GetMapping
    public String print() {
        return "This is from Spring Boot";
    }
}

Now I am able to invoke this endpoint by using the following endpoint
http://localhost:8080
Output is…

Spring boot default security

This endpoint I can access without authorization. I want to make this endpoint a protected endpoint using the Spring Boot default security configuration. In order to do that, you have to add the following Artifact in your pom.xml

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

Once you added this Artifact, restart your Spring Boot Application. You can notice one difference from your Server console log as in the below screenshot.
Spring Security

By adding the Spring boot Security starter Project artifact, we are enabling default Security in our Spring Boot Application. While starting the Application, it generates the default password, when you try to access a Webservice, it never allows you to access. Spring boot throws the default login page and asks you to enter the User name and password. The user name you have to enter “User” and password you have to enter whatever password showing in the Server Console. in our case we need to use the password that highlighted red color in the Server. Let try the demo now.

When I try to hit the following endpoint which we tried before
http://localhost:8080/ Now, I am not able to access this endpoint and it throws the following login form which is spring boot default login form.

Spring Boot login form

In order to access our Webservice endpoint, you need to log in with the following credentials.

User Name = User
password = dd8fd9fa-2279–4f73-be16–98001b186d72 

Each time you restart the server, the new password generates and showed in the Server log and the User name remains the same.
Download the code from the following downloadable link
Download the code

Please watch this full video for a complete demo which explains how to enable the Spring Boot Default authorization.
https://youtu.be/WU0B1I8V6G0


Please follow our ITGARDEN YouTube channel for Billing System Project development work using Spring Boot. The Project source code absolutely free. This project is very good for who is looking for real-time project experience in Spring Boot. To understand the Billing System Project code, watch all Billing System related YouTube videos from this link. Click Here
Our YouTube Channel
https://www.youtube.com/channel/UChj5CeuWaHIFr4RkXoG3iJA?view_as=subscriber
Please follow me on Medium.com for all billing system related stories and Java-based technical Articles
https://suresh-stalin.medium.com/
Note: To find all billing System related stories find “billing system itgarden” in medium.com

Close Menu
×
×

Cart