[cf9cdbf] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
| 3 | import edu.gjoko.schedlr.entity.Stakeholder;
|
---|
| 4 | import edu.gjoko.schedlr.repositories.StakeholderRepository;
|
---|
| 5 | import org.springframework.security.core.GrantedAuthority;
|
---|
| 6 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
| 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;
|
---|
[763289e] | 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[cf9cdbf] | 12 | import org.springframework.stereotype.Service;
|
---|
| 13 |
|
---|
| 14 | import java.util.HashSet;
|
---|
| 15 | import java.util.Set;
|
---|
| 16 |
|
---|
| 17 | @Service
|
---|
| 18 | public class PostgresUserDetailsService implements UserDetailsService {
|
---|
| 19 |
|
---|
| 20 | private final StakeholderRepository stakeholderRepository;
|
---|
[763289e] | 21 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
[cf9cdbf] | 22 |
|
---|
[763289e] | 23 | public PostgresUserDetailsService(StakeholderRepository stakeholderRepository,
|
---|
| 24 | BCryptPasswordEncoder bCryptPasswordEncoder) {
|
---|
[cf9cdbf] | 25 | this.stakeholderRepository = stakeholderRepository;
|
---|
[763289e] | 26 | this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
---|
[cf9cdbf] | 27 | }
|
---|
| 28 |
|
---|
| 29 | @Override
|
---|
| 30 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
---|
| 31 | Stakeholder user = stakeholderRepository.findStakeholderByUsername(username);
|
---|
| 32 | if(user == null) {
|
---|
| 33 | throw new UsernameNotFoundException("Non existing user");
|
---|
| 34 | }
|
---|
| 35 | Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
---|
| 36 | grantedAuthorities.add(new SimpleGrantedAuthority(user.getStakeholderType().name()));
|
---|
| 37 | return new User(user.getUsername(), user.getPassword(), grantedAuthorities);
|
---|
| 38 | }
|
---|
| 39 | }
|
---|