Spring Boot Security : Authentication And Authorization

Gain Java Knowledge
4 min readDec 31, 2022

In this tutorial, we will learn How to make secure rest API’s using basic authentication and authorization.

Okay, Lets create one spring boot application and after that we will implement the spring security to our rest API.

Go to : https://start.spring.io/

and Enter project details and will add two dependencies Spring Web and Spring Security.

Import Project into IDE. I have imported into intellij IDE . Okay Lets use the default security provided by spring boot application. After that we will implement customize security based on Role and URL.

Pom.xml

Okay . So first we will see how to enable spring boot default security and will make secure rest API’s.

First we need to add @EnableWebSecurity annotation at class level.

After enabling spring security. Now we will create rest endpoint in our spring boot application.

Okay, Let me create MessageController.java class inside Controller package.

To use Spring boot default basic security. We need to define username and password in application.properties file.

application.properties

Okay. now lets start our application and will test our rest endpoint.

Console logs

Application has started at port 8080. Now go to browser and call our rest endpoint that we have created in MessageController.java class. http://localhost:8080/message

Gain Java Knowledge

The Java programming language is one of the most popular languages today. Stay up to date with news, certifications, free learning resources and much more.