Spring Boot services · Chapter 25 of 41

Users from the database

By the end of this chapter you can let people sign up, keep their accounts in a table, and have Spring Security log them in from it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Security
  • 6 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

The Tapri app is going live in Bengaluru, and customers want accounts. Ravi is not going to type a thousand users into SecurityConfig: they sign up in the app, and Tapri keeps them.

Last chapter, ravi lived in an InMemoryUserDetailsManager built at startup. A new account would need a code change and a restart, and every user's details would be in the source. The accounts belong in a table that signup writes and login reads. HTTP Basic, the 401 and the filter chain stay exactly as they were.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    SecurityConfig.java: hashes that name their algorithm

    Make the PasswordEncoder bean a delegating encoder, so every hash it writes starts with {bcrypt}$2 (the algorithm's name in braces, then the BCrypt hash).

    It must accept the password that made a hash (masala@123) and refuse another (cutting@123).

  2. Task 2

    SecurityConfig.java: load a user and their role by email

    Make the UserDetailsService look the account up by email in the database (AppUserRepository.findByEmail) and return a UserDetails whose username is the email, whose password is the stored hash exactly as saved, and whose only authority is ROLE_ plus the account's role (ravi@tapri.in, CUSTOMER, gives ROLE_CUSTOMER; meera@tapri.in, ADMIN, gives ROLE_ADMIN).

    Look the email up trimmed and in lower case, the way signup stores it.

  3. Task 3

    SecurityConfig.java: an unknown email throws UsernameNotFoundException

    When no account has the email (kabir@tapri.in), make the UserDetailsService throw a UsernameNotFoundException. Spring Security turns it into the same 401 a wrong password gets.

  4. Task 4

    AuthController.java: sign a customer up at POST /auth/signup

    Make POST /auth/signup save a new AppUser with the email trimmed and in lower case, the password hashed by the PasswordEncoder, and the role CUSTOMER. Answer 201 Created with a UserResponse: the new id (a number), the email and the role, and no password or passwordHash field.

    The stored hash must match the password and must not contain it. A request that fails validation (a password shorter than 8 characters) already answers 400 and must save nothing.

  5. Task 5

    AuthController.java: 409 for an email that is taken

    When an account with the email already exists, answer 409 Conflict and save nothing, whatever the email's case: after ravi@tapri.in signs up, both ravi@tapri.in and Ravi@Tapri.IN get 409, and there is still only one account.

  6. Task 6

    AuthController.java: a customer who signed up logs in

    A customer who signed up at POST /auth/signup can log in with HTTP Basic: GET /me as ravi@tapri.in with masala@123 answers 200 with {"email": "ravi@tapri.in"}. A wrong password (cutting@123) or an email with no account (kabir@tapri.in) answers 401.

    This needs tasks 2 and 4: signup must store a hash the encoder accepts, and the UserDetailsService must find the account.

What you will learn

  • UserDetailsService: the one question Spring Security asks about a user, answered from the database.
  • UsernameNotFoundException: how "no such user" becomes a 401.
  • The delegating password encoder: hashes that name their algorithm.
  • Signup: storing only a hash, and 409 Conflict for a taken email.

More in Security

Build "Users from the database" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.