420. Database Authentication (UserDetailsService with JPA) - dkkahm/study-springfamework5 GitHub Wiki

JpaUserDetailsService

  • Transactional or to fetch eager for authorities
@RequiredArgsConstructor
@Service
public class JpaUserDetailsService implements UserDetailsService {
    private final UserRepository userRepository;

    @Transactional
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        User user = userRepository.findByUsername(s).orElseThrow(() -> new UsernameNotFoundException("User name: " + s + " not found."));

        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
                user.getEnabled(), user.getAccountNonExpired(), user.getCredentialsNonExpired(),
                user.getAccountNonLocked(), convertToSpringAuthorities(user.getAuthorities()));
    }

    private Collection<? extends GrantedAuthority> convertToSpringAuthorities(Set<Authority> authorities) {
        if(authorities != null && authorities.size() > 0) {
            return authorities.stream()
                    .map(Authority::getRole)
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toSet());
        } else {
            return new HashSet<>();
        }
    }
}

SecurityConfig

  • use default void configure(AuthenticationManagerBuilder auth) for UserDetailsService and PasswordEncoder
  • remove void configure(AuthenticationManagerBuilder auth) for InMemoryAuthentication
⚠️ **GitHub.com Fallback** ⚠️