Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. protected final Log LOGGER = LogFactory.getLog(getClass());
  2.  
  3. @Autowired
  4. private UserRepository userRepository;
  5.  
  6. @Autowired
  7. private PasswordEncoder passwordEncoder;
  8.  
  9. @Autowired
  10. private AuthenticationManager authenticationManager;
  11.  
  12. // Funkcija koja na osnovu username-a iz baze vraca objekat User-a
  13. @Override
  14. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  15. User user = userRepository.findByUsername(username);
  16. if (user == null) {
  17. throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
  18. } else {
  19. return user;
  20. }
  21. }
  22.  
  23. // Funkcija pomocu koje korisnik menja svoju lozinku
  24. public void changePassword(String oldPassword, String newPassword) {
  25.  
  26. Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
  27. String username = currentUser.getName();
  28.  
  29. if (authenticationManager != null) {
  30. LOGGER.debug("Re-authenticating user '" + username + "' for password change request.");
  31.  
  32. authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
  33. } else {
  34. LOGGER.debug("No authentication manager set. can't change Password!");
  35.  
  36. return;
  37. }
  38.  
  39. LOGGER.debug("Changing password for user '" + username + "'");
  40.  
  41. User user = (User) loadUserByUsername(username);
  42.  
  43. // pre nego sto u bazu upisemo novu lozinku, potrebno ju je hesirati
  44. // ne zelimo da u bazi cuvamo lozinke u plain text formatu
  45. user.setPassword(passwordEncoder.encode(newPassword));
  46. userRepository.save(user);
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement