1
|
|
|
package unicon.matthews.security.auth.jwt; |
2
|
|
|
|
3
|
|
|
import java.io.IOException; |
4
|
|
|
|
5
|
|
|
import javax.servlet.FilterChain; |
6
|
|
|
import javax.servlet.ServletException; |
7
|
|
|
import javax.servlet.http.HttpServletRequest; |
8
|
|
|
import javax.servlet.http.HttpServletResponse; |
9
|
|
|
|
10
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
11
|
|
|
import org.springframework.security.core.Authentication; |
12
|
|
|
import org.springframework.security.core.AuthenticationException; |
13
|
|
|
import org.springframework.security.core.context.SecurityContext; |
14
|
|
|
import org.springframework.security.core.context.SecurityContextHolder; |
15
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; |
16
|
|
|
import org.springframework.security.web.authentication.AuthenticationFailureHandler; |
17
|
|
|
import org.springframework.security.web.util.matcher.RequestMatcher; |
18
|
|
|
|
19
|
|
|
import unicon.matthews.security.auth.JwtAuthenticationToken; |
20
|
|
|
import unicon.matthews.security.auth.jwt.extractor.TokenExtractor; |
21
|
|
|
import unicon.matthews.security.config.WebSecurityConfig; |
22
|
|
|
import unicon.matthews.security.model.token.RawAccessJwtToken; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Performs validation of provided JWT Token. |
26
|
|
|
* |
27
|
|
|
* @author vladimir.stankovic |
28
|
|
|
* |
29
|
|
|
* Aug 5, 2016 |
30
|
|
|
*/ |
31
|
|
|
public class JwtTokenAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter { |
32
|
|
|
private final AuthenticationFailureHandler failureHandler; |
33
|
|
|
private final TokenExtractor tokenExtractor; |
34
|
|
|
|
35
|
|
|
@Autowired |
36
|
|
|
public JwtTokenAuthenticationProcessingFilter(AuthenticationFailureHandler failureHandler, |
37
|
|
|
TokenExtractor tokenExtractor, RequestMatcher matcher) { |
38
|
|
|
super(matcher); |
39
|
|
|
this.failureHandler = failureHandler; |
40
|
|
|
this.tokenExtractor = tokenExtractor; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
@Override |
44
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) |
45
|
|
|
throws AuthenticationException, IOException, ServletException { |
46
|
|
|
String tokenPayload = request.getHeader(WebSecurityConfig.JWT_TOKEN_HEADER_PARAM); |
47
|
|
|
RawAccessJwtToken token = new RawAccessJwtToken(tokenExtractor.extract(tokenPayload)); |
48
|
|
|
return getAuthenticationManager().authenticate(new JwtAuthenticationToken(token)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
@Override |
52
|
|
|
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, |
53
|
|
|
Authentication authResult) throws IOException, ServletException { |
54
|
|
|
SecurityContext context = SecurityContextHolder.createEmptyContext(); |
55
|
|
|
context.setAuthentication(authResult); |
56
|
|
|
SecurityContextHolder.setContext(context); |
57
|
|
|
chain.doFilter(request, response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
@Override |
61
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, |
62
|
|
|
AuthenticationException failed) throws IOException, ServletException { |
63
|
|
|
SecurityContextHolder.clearContext(); |
64
|
|
|
failureHandler.onAuthenticationFailure(request, response, failed); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|