Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 39

    Authentication

    Authentication answers who are you?.

    Course progress0%
    Focus
    2 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Authentication answers who are you?. The user proves identity (password, token, certificate) and Spring Security loads a UserDetails + roles into the SecurityContext.

    Informative example

    Custom UserDetailsService backed by your DB:

    ts
    @Service @RequiredArgsConstructor
    public class DbUserDetailsService implements UserDetailsService {
    private final UserRepository users;
    @Override
    public UserDetails loadUserByUsername(String email) {
    return users.findByEmail(email)
    .map(u -> User.withUsername(u.getEmail())
    .password(u.getPasswordHash())
    .authorities(u.getRoles().stream()
    .map(r -> new SimpleGrantedAuthority("ROLE_" + r))
    .toList())
    .build())
    .orElseThrow(() -> new UsernameNotFoundException(email));
    }
    }
    @Bean PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); }
    Ready to mark this lesson complete?Track your journey across the entire course.