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.
 
 
 
 
 
 

75 lines
2.6 KiB

package org.ros.chatto.security;
import java.util.List;
import javax.annotation.PostConstruct;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserRole;
import org.ros.chatto.repository.RoleRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
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.web.context.WebApplicationContext;
@Service
public class MyUserDetailsService implements UserDetailsService {
// @Autowired
// private WebApplicationContext applicationContext;
@Autowired
private UserRepository userRepository;
// @Autowired
// private RoleRepository roleRepository;
@Autowired
private UserRoleRepository userRoleRepository;
// @PostConstruct
// public void completeSetup() {
// userRepository = applicationContext.getBean(UserRepository.class);
// }
public MyUserDetailsService() {
super();
}
@Override
public UserDetails loadUserByUsername(String username) {
ChatUser user = userRepository.findByUserName(username);
if (user == null) {
throw new UsernameNotFoundException(username);
}
System.out.println("Found useeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer " + user.getUserName() + user.getPassword());
List<UserRole> userRoles = userRoleRepository.findByUser(user.getUserID());
System.out.println("User role iddddddddddddddddd = " + userRoles.get(0).getRole().getName());
// System.out.println(userRoles.);
// return new MyUserPrincipal(user);
return toUserDetails(new UserObject(user.getUserName(), user.getPassword(), userRoles.get(0).getRole().getName()));
}
private UserDetails toUserDetails(UserObject userObject) {
return User.withUsername(userObject.name)
.password(userObject.password)
.roles(userObject.role).build();
}
private static class UserObject {
private String name;
private String password;
private String role;
public UserObject(String name, String password, String role) {
this.name = name;
this.password = password;
this.role = role;
}
}
}