Basic of Spring Security – Adding Security only with Dependency:

What is 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. Reference here.

Today in this article we are going to use a very basic Spring Security without much of code.

We are going to cover 2 Scenario’s in this article,

  • We are going to add Spring Security dependency and going to login our application with default credentials
  • We are going to customize our own username and password for authentication.

Let us create a basic spring security project,


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
<relativePath/> <!-- lookup parent from repository -->
<description>Spring Security Basic</description>




We have created our main class,


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SecurityApplication {

public static void main(String[] args) {, args);


Now let us create a sample end point which will return a String,


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class SecurityController {
    public String testSecurity() {
        return "This is a message from Spring Security";

Now let us start our application,

application startup

When the application starts up, we can see a default password generated by Spring Security. Now let us access our endpoint,


Default username is user and password copy the code from our springboot start-up console,

Scenario 1 – output

Now let us try to implement scenario 2 – Customize username and password

Let us customize our username and password by configuring in,

Now we can configured our username as javainfinite and password as 12345, now let us restart our application and login with the configured credentials,

application startup

Now we can see spring security has not created any default password that is because we have declared the password in file.

That’s it about basics of spring security in this article. For clarifications kindly post your question in comments section, We will get back to you as soon as we can.

Great day!

By Sri

Leave a Reply

Your email address will not be published.