-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathDaoAuthenticationProvider.java
34 lines (25 loc) · 1.19 KB
/
DaoAuthenticationProvider.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
package nextstep.security.authentication;
import nextstep.app.ui.AuthenticationException;
import nextstep.security.userdetils.UserDetails;
import nextstep.security.userdetils.UserDetailsService;
public class DaoAuthenticationProvider implements AuthenticationProvider {
private final UserDetailsService userDetailsService;
public DaoAuthenticationProvider(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UserDetails userDetails = userDetailsService.loadUserByUsername(authentication.getPrincipal().toString());
if (userDetails == null) {
throw new AuthenticationException();
}
if (!userDetails.getPassword().equals(authentication.getCredentials())) {
throw new AuthenticationException();
}
return UsernamePasswordAuthenticationToken.authenticated(userDetails, userDetails.getPassword());
}
@Override
public boolean supports(Class<?> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
}