Spring Boot Security Defaults: What One Dependency Gives You for Free
Relay's ticket API answers anybody. Add one starter and Spring Boot locks every URL: a generated user, 401 for programs, a login form for browsers, CSRF protection and security headers, with no security class written.
What you will learn
- Lock every URL by adding spring-boot-starter-security
- Explain the 401 with WWW-Authenticate, the 302 to /login and the CSRF 403
- Find the generated user and password and know they are for development only
- Know when Boot's defaults step back for your own configuration
Before you start
- A Spring Boot REST API
- HTTP status codes
- curl basics
Right now Relay's ticket API answers anybody who asks. A curl call with no name and no password gets every ticket, with customer names and comments:
curl -i http://localhost:8080/api/tickets
HTTP/1.1 200
Content-Type: application/json
[{"id":1,"subject":"Cannot log in after password reset","status":"IN_PROGRESS","priority":"LOW","customer":"Ava Thompson", ...
All 50 tickets, to anyone who can reach the server. There's simply no lock yet. Let's add one, with one dependency and no security code.
One dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
When the app starts, Spring Boot looks for your security beans. It finds no filter chain of yours, so it builds a default one. It finds no user service either, so it creates one user named user with a password made up at startup, and prints it in the log:
WARN .s.a.UserDetailsServiceAutoConfiguration :
Using generated security password: af540915-....-....-....-............
This generated password is for development use only. Your security configuration must be updated before running your application in production.
You wrote no security code. Boot made both the chain and the user.
Where the lock sits
No credentials: 401
curl -i http://localhost:8080/api/tickets
HTTP/1.1 401
WWW-Authenticate: Basic realm="Realm", charset="UTF-8"
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Length: 0
The anonymous filter marks the caller as anonymous, the rule says "must be signed in", and Basic authentication's entry point answers. The controller is never reached. The WWW-Authenticate header tells the client how to sign in: send a name and password with HTTP Basic. A wrong password, or a wrong user name, gets the same 401.
With credentials: 200
curl -u user:<generated password> http://localhost:8080/api/tickets
Now the request carries an Authorization: Basic ... header, the name and password encoded. The Basic authentication filter reads it, checks the password, and stores the result. With security logging on at DEBUG, you see it:
DEBUG o.s.security.web.FilterChainProxy : Securing GET /api/tickets
DEBUG o.s.s.a.dao.DaoAuthenticationProvider : Authenticated user
DEBUG o.s.s.w.a.www.BasicAuthenticationFilter : Set SecurityContextHolder to UsernamePasswordAuthenticationToken [Principal=... [Username=user, Password=[PROTECTED], ...
Only then does the controller run, and the 50 tickets come back with a 200. That Authentication object travels with the request; it's how the rest of your code knows who is calling.
A browser gets a login form
curl -i -H "Accept: text/html" http://localhost:8080/api/tickets
HTTP/1.1 302
Location: http://localhost:8080/login
A browser asks for HTML, and a person is better served by a form than by a Basic prompt, so Boot redirects to /login. It draws that page itself (its title is "Please sign in"). You wrote no HTML. Same user, same password.
CSRF protection is on
Sign in with the form, then send a POST /api/tickets with the session cookie but no CSRF token:
{"timestamp":"2026-10-30T20:43:36.304Z","status":403,"error":"Forbidden","path":"/api/tickets"}
DEBUG o.s.security.web.csrf.CsrfFilter : Invalid CSRF token found for http://localhost:8080/api/tickets
DEBUG o.s.s.w.access.AccessDeniedHandlerImpl : Responding with 403 status code
A signed-in browser that sends a changing request without the token is refused. That stops another website from tricking your logged-in browser into sending a request for you. (For a token-based API, you'll make a different choice later. See CORS, CSRF and the SPA.)
All the defaults on one table
| Default | What you see |
|---|---|
| Every URL locked | 401 for a program, 302 to /login for a browser |
| One user, generated password | user plus the password printed at startup |
| Two ways in | HTTP Basic and the form login |
| CSRF on | a signed-in POST without a token gets 403 |
| Security headers | X-Content-Type-Options: nosniff, X-Frame-Options: DENY, no caching |
The headers tell the browser not to guess file types, not to show your pages inside someone else's frame, and not to keep a copy of the response.
Boot steps back when you take over
These are defaults, not a cage. Declare your own SecurityFilterChain bean and Boot's default configuration "did not match" in the conditions report, so it steps aside. Declare your own users and the generated password line disappears. You replace the defaults one piece at a time, which is exactly what the next article does: The Security Filter Chain and Your Own Rules.
Common mistakes
- Shipping the generated user. The log says it plainly: development use only.
- Reading 401 as "server broken". It means "who are you?". Check the
WWW-Authenticateheader. - Turning CSRF off "because POST fails" while you still log in with a form and a cookie. That removes the protection the form needs.
Try it yourself
Run the project with the fixed-user profile from its README, which defines a fixed development user in application.yaml. Does the "Using generated security password" line still appear in the log? Why not?
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
A Registration Form With Bean Validation in Spring Boot: @Valid, BindingResult and th:errors
A RegisterForm record with three annotations checks the name, email and password at the door. Bad data goes back to the form with a message under each field; good data becomes an Account row. And the password is still stored as plain text, which the next step fixes.
Spring Security 7 Form Login, BCrypt and a UserDetailsService on the Account Table
A guard at the gate and a hash in the table. A SecurityFilterChain lists the open pages and sends everything else to /login, a BCrypt PasswordEncoder bean hashes every password, and a UserDetailsService answers 'who is this email?' from the ACCOUNT table.
Roles and Authorities at Login in Spring Security: From a ROLE Column to hasRole("ADMIN")
Until now every login got .roles("USER") typed into the code. A Role enum on Account, one changed line in the UserDetailsService and one hasRole rule let Chaand open /admin while Emma gets a 403 and a visitor is sent to log in.
Menus per Role With Thymeleaf Security Tags: sec:authorize, sec:authentication and an Edit Button for the Author
One header file, three menus: a visitor sees Register and Login, Emma sees her email and Logout, Chaand also gets Admin. Then each post knows its author through @ManyToOne, and the Edit button shows only for the author or an admin.
