-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathSecurityUserServiceImpl.java
51 lines (47 loc) · 1.82 KB
/
SecurityUserServiceImpl.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.lambdaschool.usermodel.services;
import com.lambdaschool.usermodel.exceptions.ResourceNotFoundException;
import com.lambdaschool.usermodel.models.User;
import com.lambdaschool.usermodel.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
// make sure the user that is in the import be the one from this application, not core security
// import org.springframework.security.core.userdetails.User;
/**
* This implements User Details Service that allows us to authenticate a user.
*/
@Service(value = "securityUserService")
public class SecurityUserServiceImpl
implements UserDetailsService
{
/**
* Ties this implementation to the User Repository so we can find a user in the database.
*/
@Autowired
private UserRepository userrepos;
/**
* Verifies that the user is correct and if so creates the authenticated user
*
* @param s The user name we are look for
* @return a security user detail that is now an authenticated user
* @throws ResourceNotFoundException if the user name is not found
*/
@Transactional
@Override
public UserDetails loadUserByUsername(String s)
throws
ResourceNotFoundException
{
User user = userrepos.findByUsername(s.toLowerCase());
if (user == null)
{
throw new ResourceNotFoundException("Invalid username or password.");
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
user.getAuthority());
}
}