content: package com.alphnology.security;
  
  import com.alphnology.data.User;
  import com.alphnology.data.UserRepository;
  import lombok.RequiredArgsConstructor;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.ArrayList;
  import java.util.List;
  
  @Service
  @RequiredArgsConstructor
  public class UserDetailsServiceImpl implements UserDetailsService {
  
      private final UserRepository userRepository;
  
      @Override
      @Transactional
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          User user = userRepository.findByUsername(username)
                  .orElseThrow(() -> new UsernameNotFoundException("No user present with username: " + username));
  
          return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
                  !user.isLocked(), true, true, true,
                  getAuthorities(user));
      }
  
      private List<GrantedAuthority> getAuthorities(User user) {
          List<GrantedAuthority> authorities = new ArrayList<>();
          authorities.add(new SimpleGrantedAuthority("ROLE_" + user.getRoles()));
          return authorities;
      }
  
  }
  