41 lines
1.5 KiB
Java
41 lines
1.5 KiB
Java
package com.cubetiqs.fusion.security;
|
|
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
import com.cubetiqs.fusion.data.entity.User;
|
|
import com.cubetiqs.fusion.data.service.UserRepository;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
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;
|
|
|
|
@Service
|
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
|
|
@Override
|
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
User user = userRepository.findByUsername(username);
|
|
if (user == null) {
|
|
throw new UsernameNotFoundException("No user present with username: " + username);
|
|
} else {
|
|
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getHashedPassword(),
|
|
getAuthorities(user));
|
|
}
|
|
}
|
|
|
|
private static List<GrantedAuthority> getAuthorities(User user) {
|
|
return user.getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role.getRoleName()))
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
}
|