Introduction to Spring Security and its Features (original) (raw)

Last Updated : 19 May, 2026

Spring Security framework is used to secure Java applications by handling authentication and authorization. It integrates smoothly with Spring Boot, making it easy to apply security configurations with minimal setup. It also protects applications from common security threats.

Application of Spring Security

Spring Security is widely used in web applications to secure user authentication, manage authorization, and protect sensitive resources from unauthorized access and cyber attacks.

applications_of_spring_security.webp

Spring Security provides various security features, including:

Comprehensive Protection Against Common Vulnerabilities

Spring Security offers out-of-the-box protection for many well-known security threats, including:

Seamless Integration with Spring Framework

Spring Security is tightly integrated with Spring Boot and Spring MVC, which means you can enable security features with minimal configuration. For example, adding spring-boot-starter-security to your project automatically applies basic authentication.

Password Management

Method-Level Security

We can secure specific methods in your application using annotations such as:

**Example:

@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long id) {
// only admins can delete users
}

Support for Modern Security Standards

Steps to Implements Basic Authentication in Spring Boot

Follow these steps to Build Basic Authentication in a Spring Boot application.

Step 1. Create a new Spring Boot project

Use Spring Initializr or create manually.

Suggested project coordinates:

Extract the zip to a folder and import project on IDE.

Structure

Structure

**pom.xml:

Java `

4.0.0 org.springframework.boot spring-boot-starter-parent 3.2.4 com.example demo 0.0.1-SNAPSHOT spring-security-basic-auth-demo Spring Boot Basic Auth Demo <java.version>17</java.version> org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

`

Step 2. Add a simple REST controller

Create a java class inside -> src/main/java/com/example/demo/HelloController.java with:

**HelloController.java

Java `

package com.example.demo;

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

@RestController public class HelloController {

@GetMapping("/hello")
public String hello() {
    return "Welcome! You are successfully authenticated."
}

}

`

Set properties inside -> src/main/resources/application.properties and add:

**application.properties

Java `

spring.security.user.name=admin spring.security.user.password=admin123

`

Step 4. Run the application

Open main class and run an application:

**DemoApplication.java

Java `

package com.example.demo;

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

@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

`

Step 5. Test the secured endpoint

**Open browser visit ->

http://localhost:8080/login

We saw login prompt appears with user name and password

**Output:

Structure

output

**Fill details:

And Click Sign in then you can see message.

Structure

output

Advantages of Spring Security

Some major benefits of using Spring Security include: