A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package org.ros.chatto.security;
  2. import java.util.Set;
  3. import org.ros.chatto.model.ChatUser;
  4. import org.ros.chatto.model.UserRole;
  5. import org.ros.chatto.service.UserService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.core.userdetails.User;
  8. import org.springframework.security.core.userdetails.UserDetails;
  9. import org.springframework.security.core.userdetails.UserDetailsService;
  10. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  11. import org.springframework.stereotype.Service;
  12. import lombok.RequiredArgsConstructor;
  13. import lombok.extern.slf4j.Slf4j;
  14. @Service
  15. @Slf4j
  16. @RequiredArgsConstructor
  17. public class MyUserDetailsService implements UserDetailsService {
  18. @Autowired
  19. private final UserService userService;
  20. @Override
  21. public UserDetails loadUserByUsername(final String username) {
  22. log.trace("User Details - loading with username: {}", username);
  23. ChatUser user = userService.getUserWithRole(username);
  24. if (user == null) {
  25. log.warn("Request for unknown user {}", username);
  26. throw new UsernameNotFoundException(username);
  27. }
  28. Set<UserRole> userRoles = user.getUserRoles();
  29. return User.withUsername(user.getUserName())
  30. .password(user.getPassword())
  31. .roles(userRoles.stream().map(ur -> ur.getRole().getName())
  32. .toArray(size -> new String[size]))
  33. .build();
  34. }
  35. }