forked from NeilAlishev/SpringCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonDetailsService.java
More file actions
36 lines (28 loc) · 1.23 KB
/
PersonDetailsService.java
File metadata and controls
36 lines (28 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package ru.alishev.springcourse.FirstSecurityApp.services;
import org.springframework.beans.factory.annotation.Autowired;
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 ru.alishev.springcourse.FirstSecurityApp.models.Person;
import ru.alishev.springcourse.FirstSecurityApp.repositories.PeopleRepository;
import ru.alishev.springcourse.FirstSecurityApp.security.PersonDetails;
import java.util.Optional;
/**
* @author Neil Alishev
*/
@Service
public class PersonDetailsService implements UserDetailsService {
private final PeopleRepository peopleRepository;
@Autowired
public PersonDetailsService(PeopleRepository peopleRepository) {
this.peopleRepository = peopleRepository;
}
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
Optional<Person> person = peopleRepository.findByUsername(s);
if (person.isEmpty())
throw new UsernameNotFoundException("User not found");
return new PersonDetails(person.get());
}
}