AI Hints
Home›Spring Boot Tutorial›Users and Passwords, Password Encoding and Roles
Spring Boot · Lesson 94 of 120

Users and Passwords

More than one user.

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

A real shop has many users, each with their own password. Spring Security finds users through a bean called User Details Service.

  • Many users
  • Each with a password
  • UserDetailsService

UserDetailsService

User Details Service has one job. Given a user name, load that user, with their password, and roles. In memory user details manager keeps users in memory, which is great for learning. Later, you'd load users from a database instead.

keyvalue
Jobload a user by name
Returnsname, password, roles
For learningInMemoryUserDetailsManager
Real appsusers in a database

Two users

We create two users. Sam is a normal user. Alex is a user, and an admin. The noop prefix means, this password is stored as plain text. That's only acceptable while learning. Run it. Sam and Alex each log in, with their own roles. And a wrong password gets four oh one.

SecurityConfig.javajava
@Configuration
public class SecurityConfig {

    @Bean
    SecurityFilterChain api(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
            .httpBasic(Customizer.withDefaults()).csrf(csrf -> csrf.disable());
        return http.build();
    }

    @Bean
    UserDetailsService users() {
        return new InMemoryUserDetailsManager(
            User.withUsername("sam").password("{noop}reader1").roles("USER").build(),
            User.withUsername("alex").password("{noop}boss123")
                .roles("USER", "ADMIN").build());
    }
}
Output
$ curl -u sam:reader1 localhost:8080/me
You are sam, roles: [ROLE_USER]
$ curl -u alex:boss123 localhost:8080/me
You are alex, roles: [ROLE_ADMIN, ROLE_USER]
$ curl -i -u sam:wrong localhost:8080/me | head -1
HTTP/1.1 401
Also uses BookController.java
@RestController
public class BookController {

    @GetMapping("/books")
    public List<String> books() { return List.of("Dune", "Circe"); }

    @DeleteMapping("/books/{id}")
    public String delete(@PathVariable int id) { return "Deleted book " + id; }

    @GetMapping("/me")
    public String me(Authentication auth) {
        return "You are " + auth.getName() + ", roles: " + auth.getAuthorities().stream()
            .map(GrantedAuthority::getAuthority).filter(a -> a.startsWith("ROLE_")).toList();
    }
}
Also uses _curl.sh
curl -u sam:reader1 localhost:8080/me
curl -u alex:boss123 localhost:8080/me
curl -i -u sam:wrong localhost:8080/me | head -1
Also uses _profile
security

Summary

A User Details Service loads users by name. In memory user details manager is perfect for learning. Next, storing passwords safely.

Key takeaways
  • UserDetailsService
  • InMemoryUserDetailsManager
  • roles("USER")
Next up: Password Encoding — never store plain text