AI Hints
Home›Spring Boot Tutorial›Spring Security Basics, Locked by Default and SecurityFilterChain
Spring Boot · Lesson 92 of 120

Locked by Default

Secure out of the box.

⏱ 2 min read▶ Video at 2:02:39🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

The moment you add the Spring Security dependency, every endpoint is locked. You don't write any code. Spring Boot locks the doors for you.

  • Add the dependency
  • Everything is locked
  • No code needed

Everything is locked

We added spring boot starter security, to the project. Our controller is unchanged. Run it. Even a simple GET now answers four oh one. The WWW Authenticate header asks the client to log in.

BookController.javajava
@RestController
public class BookController {

    @GetMapping("/books")
    public List<String> books() {
        return List.of("Dune", "Circe");
    }
}
Output
HTTP/1.1 401
WWW-Authenticate: Basic realm="Realm", charset="UTF-8"
Also uses _curl.sh
curl -i localhost:8080/books
Also uses _profile
security

The default user

Spring Boot creates one user, called user. Its password is random, and printed in the log at startup, for development only. You can set your own name, and password, in application properties.

keyvalue
User nameuser
Passwordrandom, in the startup log
Set your ownspring.security.user.name / .password
Use fordevelopment only

Logging in

We set the user name to admin, with our own password. Curl dash u sends them, using HTTP basic authentication. Run it. Without credentials, it's four oh one. With them, we get the books.

application.propertiesini
spring.security.user.name=admin
spring.security.user.password=shop-secret
Output
$ curl -i localhost:8080/books | head -1
HTTP/1.1 401
$ curl -u admin:shop-secret localhost:8080/books
["Dune","Circe"]
Also uses BookController.java
@RestController
public class BookController {

    @GetMapping("/books")
    public List<String> books() {
        return List.of("Dune", "Circe");
    }
}
Also uses _curl.sh
curl -i localhost:8080/books | head -1
curl -u admin:shop-secret localhost:8080/books
Also uses _profile
security

Summary

Adding Spring Security locks every endpoint. A default user exists, and you can set its name and password in properties. Next, choosing what's open, and what's locked.

Key takeaways
  • Locked by default
  • curl -u name:password
  • spring.security.user.*
Next up: SecurityFilterChain — your own rules