Guest User

Untitled

a guest
Feb 4th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. package org.mm.MBlog.security.services;
  2.  
  3. import org.mm.MBlog.security.models.User;
  4. import org.mm.MBlog.security.repository.UserRepository;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.security.core.userdetails.UserDetails;
  7. import org.springframework.security.core.userdetails.UserDetailsService;
  8. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11.  
  12.  
  13. @Service
  14. public class UserDetailsServiceImpl implements UserDetailsService {
  15.     @Autowired
  16.     UserRepository userRepository;
  17.  
  18.     // @Transactional means that if we add something to a DB and its successfull it will be saved, if not successfull it will be roled back
  19.     @Override
  20.     @Transactional
  21.     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  22.         User user = userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("User Not Found with username: " + username));
  23.         return UserDetailsImpl.build(user);
  24.     }
  25.  
  26. }
  27.  
Add Comment
Please, Sign In to add comment