package ciai.service; import java.util.LinkedList; import java.util.List; 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.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 ciai.model.Account; import ciai.repository.AccountRepository; import tools.AccountRole; @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired AccountRepository accountRep; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Account ac = accountRep.findByUsername(username); if (ac == null) throw new UsernameNotFoundException("User " + username + " not found"); AccountRole role = ac.getRole().getDescription(); String roleType = "ROLE_" + role; List authorities = new LinkedList<>(); authorities.add(new SimpleGrantedAuthority(roleType)); return new User(username, ac.getPassword(), authorities); } }