Skip to content

Instantly share code, notes, and snippets.

@ugurcemozturk
Last active December 6, 2017 17:28
Show Gist options
  • Save ugurcemozturk/eb98193c0a6a606a938de7d78063fbba to your computer and use it in GitHub Desktop.
Save ugurcemozturk/eb98193c0a6a606a938de7d78063fbba to your computer and use it in GitHub Desktop.
public class JWTAuthenticationFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
Authentication auth = TokenAuthenticationService.getAuth((HttpServletRequest) servletRequest);
SecurityContextHolder.getContext().setAuthentication(auth);
filterChain.doFilter(servletRequest, servletResponse);
}
static Authentication getAuthentication(HttpServletRequest request) {
String token = request.getHeader(HEADER_STRING);
if (token != null) {
// parse the token.
String user = Jwts.parser()
.setSigningKey(SECRET)
.parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
.getBody()
.getSubject();
return user != null ?
new UsernamePasswordAuthenticationToken(user, null, emptyList()) :
null;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment