To secure a Spring Boot application, add Spring Security with a deny-by-default authorization policy, enforce HTTPS, hash passwords with a modern algorithm, validate all input, keep dependencies patched and never expose sensitive Actuator endpoints. Secrets belong in the environment or a secrets manager, not in the repository.
This guide targets Spring Boot 3.x with Spring Security 6, where configuration uses a SecurityFilterChain bean instead of the removed WebSecurityConfigurerAdapter.
Add the starter, and every endpoint is protected with HTTP Basic or form login out of the box:
Then define your own chain. Make the final rule anyRequest().authenticated() so that new endpoints are protected unless you explicitly open them.
This chain allows a health check, restricts /admin/** to administrators and requires a valid JWT everywhere else.
For APIs, validate JWTs issued by an identity provider (Keycloak, Auth0, Okta, Entra ID and others) through spring-boot-starter-oauth2-resource-server and set spring.security.oauth2.resourceserver.jwt.issuer-uri. For browser logins, use oauth2Login(). Avoid writing your own token or session scheme. If you do manage users locally, store passwords with PasswordEncoder from PasswordEncoderFactories.createDelegatingPasswordEncoder() (bcrypt by default) or Argon2, never plain text, MD5 or SHA-1.
URL rules are not enough once a service layer exists. Use method security to enforce ownership checks close to the data:
Always check that the caller may access the specific record, not just the endpoint, to prevent insecure direct object reference (IDOR) flaws.
Authorization header.CorsConfigurationSource listing trusted origins; never combine * with credentials.X-Content-Type-Options and Cache-Control by default; add a Content-Security-Policy and HSTS for browser apps.server.ssl.*, and set server.forward-headers-strategy correctly behind a load balancer.Secure, HttpOnly and SameSite with server.servlet.session.cookie.*.Use Bean Validation on request bodies and bind to DTOs, not entities:
Use parameterized queries (Spring Data derived queries, @Param, JdbcClient with named parameters) so user input never concatenates into SQL. Escape output in templates (Thymeleaf does this by default).
Do not commit passwords or API keys. Read them from environment variables, Kubernetes secrets, Vault or a cloud secrets manager. Spring Boot's spring.config.import can load external config. For Actuator, expose only what you need:
Endpoints such as env, heapdump and beans can leak sensitive data, so keep them off the web or behind an admin role, ideally on a separate management port.
Update Spring Boot regularly, as security fixes ship in patch releases. Run mvn dependency-check:check (OWASP Dependency-Check) or enable Dependabot and a software composition analysis scan in CI. Use minimal base images, run containers as a non-root user, and log authentication failures without logging passwords or tokens. Rate-limit login and expensive endpoints at the gateway or with a filter.
Adding Spring Security gives a secure baseline: all endpoints require authentication, CSRF protection and security headers are on. You must still configure authentication, authorization rules and secrets properly.
Only for stateless APIs authenticated by a bearer token header. If you use cookies or sessions, keep CSRF protection enabled.
It was removed in Spring Security 6. Declare a SecurityFilterChain bean and configure HttpSecurity with lambda DSL as shown above.
Use a DelegatingPasswordEncoder or Argon2PasswordEncoder, which add salting and a tunable work factor. Never store reversible or fast-hash passwords.
Secure Spring Boot by using Spring Security with deny-by-default rules, delegating authentication to OAuth 2.0 or OIDC, checking permissions at method level, validating input, and restricting Actuator. Keep secrets out of code, serve over HTTPS and patch dependencies continuously.