-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth_provider.java
More file actions
executable file
·47 lines (33 loc) · 1.46 KB
/
auth_provider.java
File metadata and controls
executable file
·47 lines (33 loc) · 1.46 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
37
38
39
40
41
42
43
44
45
46
47
@Slf4j
@ComponentAuthenticationProvid
public class JwtAuthenticationProvider extends AbstractUserDetailser {
@Autowired
private JwtTokenValidator jwtTokenValidator;
@Autowired
private StudentRepository studentRepository;
@Override
public boolean supports(Class<?> authentication) {
return (JwtAuthenticationToken.class.isAssignableFrom(authentication));
}
@Override
protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws JwtAuthenticationException {
}
@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication;
String token = jwtAuthenticationToken.getToken();
String email = jwtTokenValidator.parseToken(token);
if (email == null) {
throw new JwtAuthenticationException("Invalid JWT token");
}
Student student = studentRepository.findByEmail(email);
if(student == null){
throw new JwtAuthenticationException("User not found for JWT token");
}
log.trace("Parsed user from request JWT: {}", student);
return new AuthenticatedUser(
email,
token,
Collections.singletonList(new SimpleGrantedAuthority("ROLE_STUDENT")));
}
}