-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtAuthenticationProvider.java
More file actions
40 lines (30 loc) · 1.46 KB
/
JwtAuthenticationProvider.java
File metadata and controls
40 lines (30 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
package me.pinitnotification.infrastructure.authenticate;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;
public class JwtAuthenticationProvider implements AuthenticationProvider {
private final JwtTokenProvider jwtTokenProvider;
public JwtAuthenticationProvider(JwtTokenProvider jwtTokenProvider) {
this.jwtTokenProvider = jwtTokenProvider;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if(!(authentication instanceof JwtAuthenticationToken)){
return null;
}
String token = (String) authentication.getCredentials();
if(!jwtTokenProvider.validateToken(token)) {
throw new BadCredentialsException("Invalid token");
}
Long memberId = jwtTokenProvider.getMemberId(token);
Collection<? extends GrantedAuthority> authorities = jwtTokenProvider.getAuthorities(token);
return new JwtAuthenticationToken(memberId, token, authorities);
}
@Override
public boolean supports(Class<?> authentication) {
return JwtAuthenticationToken.class.isAssignableFrom(authentication);
}
}