-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomUserDetailService.java
29 lines (25 loc) · 1.09 KB
/
CustomUserDetailService.java
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
package com.fjb.sunrise.config.security;
import com.fjb.sunrise.models.User;
import com.fjb.sunrise.repositories.UserRepository;
import com.fjb.sunrise.utils.Check;
import lombok.RequiredArgsConstructor;
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
@RequiredArgsConstructor
public class CustomUserDetailService implements UserDetailsService {
private final UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByEmailOrPhone(username);
if (user == null) {
throw new UsernameNotFoundException(username);
}
return org.springframework.security.core.userdetails.User.withUsername(user.getEmail())
.password(user.getPassword())
.roles(String.valueOf(user.getRole()))
.build();
}
}