package eu.franzoni.domain;
import java.util.Collections;
import javax.annotation.PostConstruct;
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.*;
import org.springframework.stereotype.Service;
import eu.franzoni.domain.User;
@Service("userService")
public class UserService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@PostConstruct
protected void initialize() {
userRepository.save(new User("user", "demo", "ROLE_USER"));
userRepository.save(new User("admin", "admin", "ROLE_ADMIN"));
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if(user == null) {
throw new UsernameNotFoundException("user not found");
}
GrantedAuthority authority = new SimpleGrantedAuthority(user.getRole());
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Collections.singleton(authority));
}
}