Spring Boot Security Best Practices 2025
Ahmad Sadeddin
CEO at Corgea

Introduction
Spring Boot is widely used for building Java web backends, but it often handles sensitive data and must meet strict compliance requirements. Recent incidents like the Spring4Shell zero-day exploit (2022) showed that even sample Spring code can be dangerously insecure. A breach can damage reputation, incur fines, and can also lead to customers losing trust.
To avoid these risks, software developers must build security from the start. This article outlines key Spring Boot security best practices for 2025. Each section covers a practice with example code, so developers at all levels can quickly apply them.
Enforce HTTPS/TLS
Always use HTTPS (SSL/TLS) to encrypt data in transit. Spring Boot lets you configure an SSL keystore in application.properties
, for example:
Additionally, force all HTTP requests to redirect to HTTPS in Spring Security. For example, extend WebSecurityConfigurerAdapter
:
CSRF Protection
Cross-Site Request Forgery (CSRF) attacks can hijack authenticated user actions. Spring Security enables CSRF protection by default. Do not disable it in production. For example, avoid code like:
Input Validation
Never trust user input. Always validate it thoroughly to prevent injection and other attacks. Use Spring's @Valid
and Bean Validation annotations in your DTOs. For example:
Then in a controller:
Parameterize Your Queries
Avoid building SQL queries via string concatenation. Instead, use Spring Data repositories or JdbcTemplate
with parameters. For example, a JPA repository query safely binds the email:
This example uses named parameters (:email
), so the query is pre-compiled and safe against injection. Alternatively, with JdbcTemplate
you can do:
Always parameterize queries or use Spring Data repositories. Never concatenate raw user input into SQL, which leaves you vulnerable to malicious payloads.
Protect Against XSS
Cross-Site Scripting (XSS) can inject malicious scripts into your pages. To prevent it: 1) set strict security headers and 2) sanitize/encode user content. With Spring Security, configure security headers like Content Security Policy (CSP):
Authentication & Authorization
Use Spring Security to configure authentication providers and ensure only authorized users access endpoints. A typical setup uses UserDetailsService
with a strong password encoder:
This example tells Spring to load users from myUserDetailsService
and validate passwords with BCrypt, a strong hashing algorithm. For modern REST APIs, consider token-based auth. Methods like JWT (JSON Web Tokens) are popular for stateless APIs. Always serve tokens only over HTTPS and avoid exposing credentials in URLs.
Method-Level Security
Protect critical service methods with annotations. Spring allows you to put security checks right on methods via @PreAuthorize
or @RolesAllowed
. For example:
This ensures only users with the ADMIN role can invoke deleteUser()
.
Encrypt Sensitive Data
Even with secure transit (HTTPS), data at rest can be at risk. Encrypt any sensitive properties or database fields. For example, using Jasypt's Spring integration, protect secrets in your config:
Dependency Management and Updates
Keep all libraries and frameworks up-to-date. Many Spring Boot vulnerabilities come from outdated dependencies. Use Maven or Gradle's dependency management to pin secure versions. Tools like OWASP Dependency-Check or Trivy can scan your project for known CVEs. For example, integrate a scan into your CI pipeline:
Logging & Auditing
Maintain detailed logs of security events. Spring Boot's Actuator and Spring Security's auditing support help here. Enable the audit and authentication event listeners:
Logging important security events (login failures, role changes, etc.) enables quick detection of attacks.
Ready be secure?
Harden your software in less than 10 mins'